In the discovery of the IIS based WCF service boarding (Hosting) implementation, we talked about two distinct modes of operation in the adoption of the IIS (or asp.net) WCF service homestay: asp.net parallelism (Side by Side) mode and asp.net compatibility mode. For the former, WCF implements service boarding through HttpModule, while for the latter, WCF service boarding is implemented through a HttpHandler. Only in the ASP.net compatibility mode, we are familiar with some of the asp.net mechanisms can be used by us, such as through the HttpContext request context, based on file or URL authorization, HttpModule extension, identity Simulation (impersonation) and so on.
Because in asp.net compatibility mode, ASP.net uses the. aspx Page handles requests based on. Svc in exactly the same way, in other words, we can maintain session state with the help of the current HttpContext sessionstate to create a WCF Service that supports sessions. Next, we create this session service Step-by-step through a simple example. This case uses the 3-tier structure as shown in Figure 1 .
Figure 1 ASP. NET compatibility mode case Application structure
Step one, define the service contract: ICalculator
The case still follows the example of computing services, however, in order to reflect the existence of the session state, we define the WCF service for this case as the cumulative calculation service, which is the result of the previous operation, and is the operand of the subsequent operation. For this purpose, the following interface is defined as a service contract: the first 4 operations represent the basic addition, subtraction, multiplication, and addition operations, and the results are obtained by GetResult method.
1:using System.ServiceModel;
2:namespace Artech.AspCompatibleServices.Contracts
3: {
4: [ServiceContract]
5: Public interface ICalculator
6: {
7: [OperationContract]
8: void Add (double x);
9: [OperationContract]
A: void subtract (double x);
One: [OperationContract]
: void Multiply (double x);
[OperationContract]
: void Divide (double x);
: [OperationContract]
: double GetResult ();
: }
18:}
Step two, realize the service: CalculatorService
Both the implementation of the service and the. svc are defined in a asp.net web site project. For each operation defined in CalculatorService, the result of the last operation is taken out of the sessionstate by HttpContext, and the new result is saved to the sessionstate after the operation is completed. Support for asp.net compatibility mode is implemented by applying aspnetcompatibilityrequirementsattribute on CalculatorService.
1:using System.ServiceModel.Activation;
2:using system.web;
3:using Artech.AspCompatibleServices.Contracts;
4: [Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
5:public class Calculatorservice:icalculator
6: {
7:public void Add (Double x)
8: {
9:httpcontext.current.session["__result"] = GetResult () + x;
10:}
11:public void Subtract (double x)
12: {
13:httpcontext.current.session["__result"] = GetResult ()-X;
14:}
15:public void Multiply (Double x)
16: {
17:httpcontext.current.session["__result"] = GetResult () * x;
18:}
19:public void Divide (Double x)
20: {
21:httpcontext.current.session["__result"] = GetResult ()/x;
22:}
23:public Double GetResult ()
24: {
25:if (httpcontext.current.session["__result"] = = null)
26: {
27:httpcontext.current.session["__result"] = 0.0;
28:}
29:return (double) httpcontext.current.session["__result"];
30:}
31:}
The following is the definition and web.config of calculatorservice corresponding. svc. For simplicity, in the < @ServiceHost%> directive, simply set a required property service. Configuring <servicehostingenvironment aspnetcompatibilityenabled= "True"/> is essential for support for asp.net compatibility mode.
1: <?xml version= "1.0"?>
2: <configuration>
3: <system.serviceModel>
4: <servicehostingenvironment aspnetcompatibilityenabled= "true"/>
5: <services>
6: <service name= "CalculatorService" >
7: <endpoint binding= "Wshttpbinding" contract= "Artech.AspCompatibleServices.Contracts.ICalculator"/>
8: </service>
9: </services>
: </system.serviceModel>