WCF Study Notes (2) -- use IIS to host the WCF Service

Source: Internet
Author: User
ArticleDirectory
    • 3. Configure the SVC File
    • 4. Configure the Web. config file
    • 5. Deploy to IIS and set ASP. NET to access the WCF Service Directory
    • 6. Access the WCF Service deployed on IIS

PassPrevious notesWe know that the WCF Service cannot exist independently and must be "hosted" on other applications.ProgramThe application that carries the WCF Service is called a "host ". Multiple optional hosts of WCF, typically hosted in the IIS service. Here we will learn how to use IIS to host the WCF Service.

InPrevious notesIn the example, we use a self-created console application to host the WCF Service. Now we will use IIS as the host of this sample program. To host IIS, you only need to correctly configure IIS. At the same time, you do not need to write anyCode.

Note:

Use IIS(IIS 5.1 and IIS 6.0)As the host of the WCF Service, there is a restriction: You can only use the IIS host with the HTTP transport protocol.

 

1. Create an SVC file and a web. config file

First, openSample program(Click to download) Solution: create two files under the service project:Calculatorservice. SVCAndWeb. configFile, as follows:

 

2. Add system. servicemodel Assembly reference and change program generation directory

In the sample code solution in the previous note, the service project does not reference the system. servicemodel assembly. here we need to add references to it. At the same time, since our WCF Service is deployed on IIS as a web applicationThe Web application will only load the Assembly from the bin subdirectory under the root directoryIn Visual Studio 2010, the compiled assembly is stored in the debug or release directory under the bin directory by default, we need to change the Service Project output to the bin directory:

 

3. Configure the SVC File

Each WCF Service has a text file with the extension SVC. When the program file of the WCF Service is deployed in IIS, accessing the WCF Service accesses the corresponding SVC file.For client programs, the SVC file address is the service endpoint address.The SVC file only contains one<% @ Servicehost %> commandAnd a series of optional attributes(Click to view details).

Now we can openCalculatorservice. SVCFile, add the following command:

 
1 //Service is the namespace, and calculatorservice is the class name that provides the service2<% @ Servicehost service ="Service. calculatorservice"%>

 

4. Configure the Web. config file

we learned from the previous note: endpoints of the service. "> all communication of the WCF Service is performed through the endpoint of the Service endpoints of the service. "> . the endpoint is address, binding, and contract . The preceding SVC file already declares the endpoint address. Now we Configure the web. .

Manual ConfigurationWeb. configFiles are troublesome and error-prone. You can use the configuration tool class provided by Visual Studio 2010 to simplify this process. In the Visual Studio 2010 menu, select:Tools --> WCF Service configuration Editor.

4.1 Use the WCF Service configuration editor to open the created web. config file

 

4.2 Use the WCF Service configuration editor to configure the Web. config file

1.> Create a service

Choose create service> browse> selectService ProjectGeneratedService. dll file(Detailed configuration operations will not be described here)

 

2.> Create a service behavior and bind the behavior to the corresponding service

Finally,Web. config configuration fileAs follows:

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Configuration  >  3      <  System. servicemodel  >  4           <  Behaviors  >  5               <  Servicebehaviors  >  6                   <  Behavior  Name  = "Metadatabehavior"  > 7                       <  Servicemetadata  Httpgetenabled  = "True"   />  8                   </  Behavior  >  9               </  Servicebehaviors  >  10           </  Behaviors >  11           <  Services  >  12               <  Service  Behaviorconfiguration  = "Metadatabehavior"  Name  = "Service. calculatorservice"  >  13                   <  Endpoint Address  = ""  Binding  = "Wshttpbinding"  Bindingconfiguration  = ""  14   Contract  = "Contractservice. icalculator"   />  15               </  Service  >  16           </ Services  >  17       </  System. servicemodel  >  18   </  Configuration  > 

 

5. Deploy to IIS and set ASP. NET to access the WCF Service Directory

1.> in IIS, create a virtual directory on the default website, name the alias hellowcf, and set it as an application.

 

2.> set ASP. NET to access the directory where the WCF Service is located.

Open the solution directory, select the folder where the service project is located, right-click, andSelect "security" option --> click "add" --> click "advanced" --> click "Search now" and select "ASP. NET Machine Account" (in Windows XP)Click "OK" to save the settings.

If yesIn Windows 7MiddleAdd"Iis_iusrs"The account is as follows:

Tip: If "security" is not selected on the file Properties tab

Open "my computer" --> select "Tools" option --> select "Folder Options", and select "use simple file sharing.

 

6. Access the WCF Service deployed on IIS

Steps andPrevious notesIn the same way, you can access the SVC file in the browser to check whether the WCF Service is successfully deployed. In the browser, enter:

Http: // localhost/hellowcf/calculatorservice. SVC, We can see that the service has been successfully started:

 

Download with a click: sample source code

References & further reading

Bearer service

Best practices for carrying Internet Information Services

Deploy the WCF Service hosted in Internet Information Service

Servicehost command

Comprehensive Analysis of WCF

Author:Sunny pig

Source:Http://www.cnblogs.com/IPrograming

The copyright of this article is shared by the author and the blog Park. For more information, see the source.

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.