Analysis of WCF Technology four: the implementation of WCF service boarding (Hosting) based on IIS revelation

Source: Internet
Author: User
Tags datetime iis hosting

Through the introduction of IIS and ASP.net pipeline, I believe that readers have a general understanding of the request processing pipeline of IIS and ASP.net, on this basis, it is relatively easy to understand the implementation mechanism based on the IIS service boarding. In a nutshell, an IIS based service boarding relies on two important objects: System.ServiceModel.Activation.HttpModule and System. ServiceModel.Activation.HttpHandler.

First, through HttpModule to achieve service boarding

By default, the service boarding based on IIS is implemented through a special HttpModule, The type is System.ServiceModel.Activation.HttpModule and is an internal type defined in the System.ServiceModel assembly. The definition of HttpModule is largely as shown in the following code, and we see very clearly the rationale for its implementation: registering logic to implement WCF service request processing into the HttpApplication postauthenticationrequest event.

   1:internal class Httpmodule:ihttpmodule
2: {
3: //other Members
4: Public void Init (HttpApplication context)
5: {
6: Context . Postauthenticaterequest + = new EventHandler (httpmodule.processrequest);
7: }
8: private static void ProcessRequest (object sender, EventArgs e)
9: {
: //Service Request processing implementation
One: }
12:}

System.ServiceModel.Activation.HttpModule is a special httpmodule that says it especially because when HttpModule registers to the HttpApplication postauthenticaterequest event Place After the program executes, the request is not further distributed to subsequent request processing steps. In other words, for HttpApplication from BeginRequest to endrequest the entire request processing lifecycle, the request based on the. svc file extends only to the postauthenticaterequest phase. We can prove this in a simple way.

Suppose we have a WCF service that needs to be hosted through IIS, and the corresponding. svc file for the WCF service is defined in a asp.net website that corresponds to an IIS virtual directory. Now we add a global.asax to the Global.asax, and I register the first three events of the HttpApplication processing request through the following code: BeginRequest, AuthenticateRequest and Postauthenticaterequest, when these 3 events are triggered, write a section of the name representing the current event into the EventLog.

1: <%@ application language= "C #"%>
2: <%@ Import namespace= "System.Diagnostics"%>
3: <script runat= "Server" >
4:
5:void Application_BeginRequest (object sender, EventArgs e)
6: {
7:string message = string. Format ("BeginRequest Event is raised at {0}", DateTime.Now);
8:eventlog.writeentry ("Application", message, eventlogentrytype.information);
9:}
10:
11:void Application_AuthenticateRequest (object sender, EventArgs e)
12: {
13:string message = string. Format ("Authenticaterequst Event is raised at {0}", DateTime.Now);
14:eventlog.writeentry ("Application", message, eventlogentrytype.information);
15:}
16:
17:void Application_postauthenticaterequest (object sender, EventArgs e)
18: {
19:string message = string. Format ("Postauthenticaterequest Event is raised at {0}", DateTime.Now);
20:eventlog.writeentry ("Application", message, eventlogentrytype.information);
21:}
</script>

If the above statement is true, only the first 3 events of HttpApplication are triggered. In addition, the HttpModule registration operation is performed prior to the Application_postauthenticaterequest method defined in Global.asax, and only in the entire service invocation process is the Application_ BeginRequest and Application_AuthenticateRequest These two methods will be executed. We can confirm this from EventLog. After we have executed the client application on behalf of Case 7-2, there are two more log entries in the application group of Windowslog in EventLog (previously emptied the log), as shown in Figure 1.

Figure 1 Viewing the added event Log through the Event Viewer

The contents of the log are exactly the log texts we defined in the Application_BeginRequest and Application_AuthenticateRequest methods. Visible only these two methods were successfully executed, the Application_postauthenticaterequest method was not executed. As you can imagine, subsequent events are unlikely to be triggered, as shown in Figure 2.

Figure 2 Details of Event log

So far, we've only described how to handle requests based on the. svc file, and there is no indication of how the WCF service corresponding to the. svc file is hosted. The service's lodging takes place on the first visit to the service. svc file, the concrete implementation is simple: Servicemode loads the corresponding. svc file according to the destination address of the request, defined in <%ServiceHost%> by resolution The factory and service properties of the directive get the type of servicehostfactory and service (factory default is System.ServiceMode.ServiceHostFactory), which is inherited from the base class by reflection system.se The ServiceHostFactory object of the RviceModel.Activation.ServiceHostFactoryBase. Finally, the Serivce is hosted by the Serviehost object created by ServiceHostFactory that inherits from the base class System.ServiceModel.ServiceHostBase.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.