Analysis of WCF Technology three: How to do a non-HTTP-based IIS service boarding

Source: Internet
Author: User
Tags http request root directory msmq

In the previous article, we conducted a detailed and in-depth analysis of different versions of IIS, as well as asp.net implementation mechanisms. When introducing the IIS7.0, we talked about that Http.sys+w3svc realized the request listening based on HTTP, on the basis of which the following three sets of network listeners (Listener) and the Listening adapter (Adapter) were introduced to realize the TCP, Named Pipes and MSMQ network monitoring, figure 1 reveals the overall structure of the IIS7.

tcplistener| TCP Listener Adapter

Namedpipes listener| Named Pipes Listener Adapter

MSMQ listener| MSMQ Listener Adapter

Figure 1 IIS 7 Overall architecture

Since IIS 7 provides listener support based on a non-HTTP network protocol, this means that non-HTTP communication can be used when we host a WCF service (Hosting) via IIS. In this article, we'll use a simple example to host a non-HTTP IIS service hosted by the Source code download washostingdemo.zip.

Because IIS 7 implements non-HTTP request sniffing essentially through was (Windows Process activation Service), we can also call this way of service boarding as a service boarding based on was. In this example, we implement TCP based service boarding through IIS 7, and Figure 2 represents the solution structure that the instance applies to the VS2008 species. Where the Class library type project contracts is used to define the service contract, and services is used to define the specific service, and the console Application project client simulates clients. In addition, the services counterpart directory is mapped to a Web application under the IIS corresponding site, and the virtual directory name is Washostingdemo.

Figure 2 A TCP-based IIS service hosted instance solution structure in VS2008

Step one: Define service contracts and services

This example still uses the familiar example of computing services, under the Contracts project, defines the interface ICalculator the service contract that represents the computing service.

1:using System.ServiceModel;
2:
3:namespace Artech.WasHostingDemo.Contracts
4: {
5: [ServiceContract (namespace= "http://www.artech.com/")]
6: Public interface ICalculator
7: {
8: [OperationContract]
9: Double Add (double x, double y);
0: }
1:}

In the Services project, the ICalculator interface is implemented, providing the implementation of the service:

1:using Artech.WasHostingDemo.Contracts;
2:
3:namespace Artech.WasHostingDemo.Services
4: {
5: Public class Calculatorservice:icalculator
6: {
7: #region ICalculator Members
8:
9: Public double Add (double x, double y)
0: {
1: Return x + y;
2: }
3:
4: #endregion
5: }
6:}

Like the normal HTTP based IIS service boarding, we need to create a corresponding. svc text file for the WCF service, which typically contains only one <%@ servicehost%> instruction. For simplicity's sake, I just added the only required service attribute. I named the file Calculatorservice.svc, and the following is the entire contents of the. svc:

<%@ ServiceHost service= "artech.washostingdemo.services.calculatorservice,artech.washostingdemo.services"% >

Then, the directory where services resides is mapped to a virtual directory under IIS. In this example, under the default Web site sites of IIS 7, a Web application named Washostingdemo is created and its physical address is specified as the directory where the Services project resides. Then create a web.config under the root directory to configure the settings associated with the WCF service boarding. The entire WCF configuration is as follows, and the binding type is specified as NetTcpBinding.

1: <?xml version= "1.0" encoding= "Utf-8"?>
2: <configuration>
3: <system.serviceModel>
4: <services>
5: <service name= "Artech.WasHostingDemo.Services.CalculatorService" >
6: <endpoint address= "" binding= "nettcpbinding" bindingconfiguration= ""
7: contract= "Artech.WasHostingDemo.Contracts.ICalculator"/>
8: </service>
9: </services>
0: </system.serviceModel>
1: </configuration>

Note: Because the ASP.net application loads the assembly from the bin subdirectory under the root directory at run time, the target directory for the Services project's default compilation is Bin\debug| Release, so we need to set the compiled target directory to bin by modifying the project properties.

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.