First WCF3,

Source: Internet
Author: User
Tags msmq

First WCF3,

Http://www.cnblogs.com/xiangchangdong/p/3924030.html

Article 3 boarding services in IIS

Through the first two articles, we learned how to build the simplest WCF communication model, this includes defining and implementing service agreements, configuring services, hosting services, and configuring clients and accessing services by adding service references. We have a basic understanding of the programming lifecycle of WCF.

The examples demonstrated in the previous two articles must be carried by hand, including the source program and configuration file, with a line of handwriting. In this way, we can have a profound understanding. The knowledge of WCF is scattered and complex, so you must study and practice it in a down-to-earth manner. If you haven't done it, go back and try again.

Today, let's take a deeper look at some new boarding knowledge: Boarding services in IIS.

In the first two examples, we have created a console application to host the service. This boarding method is called "Self-hosting", that is, the WCF Service and the application are integrated. This boarding method has some advantages. It requires the least framework support (only one console application is required, and can be created and run everywhere). Therefore, configuration and use are the simplest, in addition, the console program can monitor errors that occur during the running of the WCF Service. In the development service stage, this method can facilitate debugging.

However, as a final product deployment, the self-hosted host mode is not suitable, and the applications are unstable compared with the frameworks (such as IIS and Windows Services, the lifecycle is shared between the WCF and the application. After the application is closed, the WCF also stops. Many other features such as process recycling and idle shutdown are not supported. Therefore, to make our WCF meet product-level requirements, we should select a more stable and scalable host for it.

In addition to self-hosting, WCF can also host on IIS, Windows Services, and Windows Process activation services (WAS. It is popular to host on IIS and Windows Process activation services.

To host in IIS, IIS5.1 or a later version is required. IIS manages ServiceHost for US (remember him, check the Code in article 1 ), it also provides features such as process recycling, idle shutdown, and process running status monitoring, we only need to put service-related files into IIS hosting according to certain organization methods (just like creating a website application or virtual directory), IIS will manage everything for us. This kind of hosting is supported by many systems, from Windows XP SP2 to WIndows Server 2008, so it is very popular. However, it also has its disadvantages. It can only accept http binding, but does not support tcp, MPs queue, and MSMQ.

Since IIS7, the so-called Windows Process activation Service (WAS) function is provided. If you store WCF in WAS, all binding protocols (such as TCP) can be supported ), protocols such as MSMQ, in the intranet and. net programming model has a great performance advantage, so WAS should become the main way of WCF boarding in the future, but IIS7 requires Windows Vista and later versions to be supported, it may take a few days for him to become popular.

We will first learn how to host in IIS. Remember that iis host only supports http binding.

The following describes the experiment environment:

Windows 7 family advanced Edition SP1

IIS7

Visual Studio 2010 ultimate SP1

Honestly, we did not use IDE to help build a project today. We wrote a Service hosted on IIS by hand.

1. Create a physical location for the IIS application

The IIS application needs to map to a physical path of the local drive. We should first build it.

I created this folder under C: \ WCF \ and named it IISService. (HelloWCF was created in the first two articles. Do you remember)

 

2. Create an IIS application

The physical path has been created. Now we create an application on this location. Click Start> control panel, find the management tool in the project, and then open IIS.

Expand the node on the left, right-click the default website node, and select "add application"

Specify an alias for the application. This can be set as needed. This name will be part of the future service address. I will start it as IISService and select the folder we just created as the physical path.

Click OK. the IIS application is created. We can see that this application is added to the default website, but there is nothing in it.

3. Create a service file

We need to create several files according to the requirements of the IIS host and put them in IIS to host our services. Of course, the service information is also described in these files.

(1) svc file.

Svc is the meaning of service. We need to create a XXX first. put the svc file in the IIS application directory. This file is the service entry. The client needs the file address to access the service (including the original data exchange, of course ).

Create the file manually, open VS2010, and choose file menu> New> file. In the regular section, select a text file and click "open ".

This should be a SVC file, but not a. txt file. Therefore, save it as another file. When saving it as another file, select the storage type as all files. I name this file HelloWCFService. svc, which can be used as part of the service address. The storage location is the location where we just created the IIS application.

Now let's edit the content of this file. It's very simple. There is only one line of code.

<%@ServiceHost language=c# Debug="true" Service="LearnWCF.HelloWCFService"%>  

If it is framed by <%>, it indicates that it is contained on a server, and the @ ServiceHost tag indicates that it is a WCF Service. Think of the ServiceHost object in the previous two codes. Language = c # indicates that we use the C # language to write code. Debug = true, as the name suggests, indicates the Service attribute. It indicates the implementation class of the Service, you must use a fully qualified name to include a namespace. I started a namespace named LearnWCF. We put the service definitions under this namespace, And the HelloWCFService is the service implementation class. We will improve the content of this class.

We can see that .. The svc file is equivalent to a wizard, which helps us find the service location in the IIS host. The specific code can be written in another place (in fact, it can also be written in the svc file, which is not recommended ).

 

Save the written content and proceed.

Next, we will write the class file that defines the service. But before that, we should first create a storage location for the class files. the IIS code files should be stored in the App_Code subdirectory of the IIS application directory, so we should first create this folder.

 

 

We can see that the HelloWCFService. svc we just created has been recognized as a WCF Service.

 

(2) cs files

Return to VS2010, choose File> New> file, and select a text file.

This time we will create a class file named HelloWCFService. cs. Note that we should select the storage type as all files when saving the file, and select the created App_Code folder as the path.

To write this file, we should be familiar with defining and implementing the service agreement here. Let's try to write it down on your back.

    using System;      using System.ServiceModel;            namespace LearnWCF      {          [ServiceContract]          public interface IHelloWCF          {              [OperationContract]              string HelloWCF();          }                public class HelloWCFService : IHelloWCF          {              public string HelloWCF()              {                  return "Hello WCF!";              }          }      }  

This code should be very skillful. If you have any questions about this code, go back to the first article and review it.

 

Save it. Let's proceed.

 

(3) web. config file

We know that we also need a configuration file to configure the endpoint, service, behavior, and so on. This configuration file is roughly the same as what we have previously created.

Return to VS2010, or create a text file and save it as web. config. This file name cannot be changed. The save path is under the IISService directory of the IIS application we created (stored together with svc)

    <configuration>        <system.serviceModel>          <services>            <service name="LearnWCF.HelloWCFService" behaviorConfiguration="metadataExchange">              <endpoint address="" binding="wsHttpBinding" contract="LearnWCF.IHelloWCF"/>              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>            </service>          </services>          <behaviors>            <serviceBehaviors>              <behavior name="metadataExchange">                <serviceMetadata httpGetEnabled="true"/>              </behavior>            </serviceBehaviors>          </behaviors>        </system.serviceModel>      </configuration>  

 

 

This configuration file is different from what we previously wrote:

1) the configuration file name is web. config, not app. config.

2) The <Service> label does not have a base address description. in IIS host, the base address of the Service is specified by IIS. For example, the base address of the Service in this example is http: // localhost/IISService/HelloWCFService. svc.

3) if the endpoint address is null, the Service base address is used as the endpoint address. Of course, a relative address can be specified here, but no absolute address can be specified, the base address specified by IIS must be used.

There is no difference in other places. It is very easy to make a namespace when specifying service implementation classes and Protocol Interface classes.

 

Save, success

4. Complete

Here, the boarding in IIS is complete. It is very simple, one IIS Application and three files. Of course, this is only the simplest case.

As for the Operation Service, IIS will work for us. As long as the IIS application (or website) is online, the service will run online.

In the old way, check the browser to see if it is successful.

Service address format of iis host:

Http: // machine name/IIS Application name/XXX. svc

Therefore, the service address in this example should be: http: // localhost/IISService/HelloWCFService. svc

Because IIS is used, the system automatically provides the address of the machine for metadata exchange, instead of localhost.

 

5. Summary.

In this article, we learned how to host the WCF Service in IIS. The necessary elements are summarized as follows:

(1) Create an IIS application and physical path

(2) create a XXX. svc file in the Application Path to declare the WCF portal and service address navigation

(3) create the XXX. cs file under the App_Code subdirectory of the Application Path to define and implement the service agreement.

(4) Create web. config in the Application Path to configure the service.

(5) Keep IIS started.

 

Related Article

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.