Migrate an existing custom web service to .NET Core or .NET 6
You can migrate a .NET Framework custom web service that retrieves the scope without inheriting the Terrasoft.Web.Common.BaseService
base class to .NET Core or .NET 6. 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 Core or .NET 6. 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
IHttpContextAccessor
interface 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 in a separate article: Replacing class factory.
-
via the
HttpContext.Current
static property.Add the
Terrasoft.Web.Http.Abstractions
namespace to the source code using theusing
directive. TheHttpContext.Current
static property implements unified access toHttpContext
. To adapt the web service code to .NET Core or .NET 6, replace theSystem.Web
namespace usingTerrasoft.Web.Http.Abstractions
.
Do not use specific access implementations to request context peculiar to .NET Framework (the System.Web
library) or .NET Core/.NET 6 (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;
...
}
}
}