Migrate an existing custom web service to .NET
You can migrate a .NET Framework custom web service that retrieves the scope without inheriting the Terrasoft.Web.Common.BaseService base class to .NET. To do this, adapt the custom web service.
The HttpContextAccessor property of the Terrasoft.Web.Common.BaseService provides unified access to context (HttpContext) both in .NET Framework and .NET. The UserConnection and AppConnection properties let you retrieve the user connection object and the connection object on the application level. This lets you omit the HttpContext.Current property of the System.Web library.
namespace Terrasoft.Configuration.UsrCustomNamespace {
using Terrasoft.Web.Common;
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class UsrCustomConfigurationService: BaseService {
/* The web service method. */
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
public void SomeMethod() {
...
/* UserConnection is the BaseService property. */
var currentUser = UserConnection.CurrentUser;
/* AppConnection is the BaseService property. */
var sdkHelpUrl = AppConnection.SdkHelpUrl;
/* HttpContextAccessor is the BaseService property. */
var httpContext = HttpContextAccessor.GetInstance();
...
}
}
}
Creatio supports the following scope retrieval options for web services developed without inheriting the Terrasoft.Web.Common.BaseService class:
-
via the
IHttpContextAccessorinterface registered inDI(ClassFactory).This option lets you view the explicit class dependencies for thorough automated testing and debugging. Learn more about using the class factory: Replacing class factory.
-
via the
HttpContext.Currentstatic property.Add the
Terrasoft.Web.Http.Abstractionsnamespace to the source code using theusingdirective. TheHttpContext.Currentstatic property implements unified access toHttpContext. To adapt the web service code to .NET, replace theSystem.Webnamespace usingTerrasoft.Web.Http.Abstractions.
Do not use specific access implementations to request context peculiar to .NET Framework (the System.Web library) or .NET (the Microsoft.AspNetCore.Http namespace) in the configuration.
namespace Terrasoft.Configuration.UsrCustomNamespace {
/* Use instead of System.Web. */
using Terrasoft.Web.Http.Abstractions;
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class UsrCustomConfigurationService {
/* The web service method. */
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
public void SomeMethod() {
...
var httpContext = HttpContext.Current;
...
}
}
}