[Honestly learn WCF] The third chapter in IIS hosting Services

Source: Internet
Author: User
Tags hosting msmq visual studio 2010

Learn WCF Honestly

Third in IIS Homestay services

With the first two studies, we learned how to build the simplest WCF communication model, including defining and implementing service contracts, configuring services, hosting services, configuring clients by adding service references, and accessing services. We have a basic understanding of the programming life cycle of WCF.

In the first two examples demonstrated, must strive to carry out, including the source program, configuration files have to carry a line of handwriting down, so as to have a profound experience. The knowledge of WCF is fragmented and complex and must be studied and practiced in a solid. If you haven't been able to get a better look at your chest, now turn back and do the example again.

Let's go a little deeper today and learn some new information about boarding: Boarding services in IIS.

In the first two examples, we built a console application to host the service, which is called "self-hosting", that is, WCF services and applications are one. This boarding style has some advantages, he needs the least framework support (just one console application is available, anywhere, anywhere), so configuration and use is the simplest, and through the console program can also monitor the operation of WCF service errors, in the development service phase, This way can provide debugging convenience.

However, if the self-hosted boarding method is less appropriate as a final product deployment, the application is not stable compared to the framework (IIS, Windows services, etc.), WCF and the application share life cycle, and WCF stops after the application shuts down. There are also many features such as process recycling, idle shutdown, and so on, which are not supported by self-hosting. Therefore, in order for our WCF to meet the product-level requirements, a more stable, more scalable host should be chosen for it.

In addition to self-hosting, WCF can also be hosted in IIS, Windows Services, Windows Process Activation Services (WAS). It is more popular to host IIS and Windows Process activation services.

Hosted in IIS, requires IIS5.1 or later support, IIS manages ServiceHost for us (remember him, look at the code in the first article), while providing us with feature support for process recycling, idle shutdown, process health monitoring, We just need to put the service-related files into the IIS hosting in a certain organization way (just like building a Web site application or virtual directory), and 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, he also has shortcomings, it can only accept the HTTP protocol binding, for TCP, pipeline, MSMQ is not supported.

Starting with IIS7, the functionality of the so-called Windows Process Activation Service (was) is provided, and if WCF is hosted in was, it can support all binding protocols (TCP, etc.), such as MSMQ, in the intranet and. NET programming model has a lot of performance advantages, so was should be the main way of future WCF homestay, but IIS7 requires more than Windows Vista version of the system to support, his popularity may still take time.

Let's learn to host IIS today, and remember that IIS homestay only supports bindings for HTTP protocols.

The experimental environment illustrates:

Windows 7 Home Premium SP1

IIS7

Visual Studio 2010 Ultimate Edition SP1

To learn honestly, we do not use the IDE to help build the project today, fully handwritten a service hosted in IIS.

1. Establishing a physical location for IIS applications

The IIS application needs to be mapped to a physical path on the local drive, so let's build it up first.

I set up this folder under the C:\WCF\, named Iisservice. (HELLOWCF was established in the first two articles, remember?)

2. Building an IIS Application

The physical path has been built, and now we are building an application in this position. Click Start, Control Panel, locate the administrative tools in your project, and then open IIS.

Expand the node on the left, right-click on the Default Web site node and select "Add Application"

Specify an alias for the application, this can be arbitrary, this name will become part of the future service address, I take it as iisservice, the physical path to select the folder we just created.

Click OK, the IIS application is built, and we can see that the application is more in the default site, but there is nothing in it.

3. Create a service file

We need to set up several files to be placed in IIS as required by the IIS host in order to host our services, and the information about the services is described in these files as well.

(1) Svc file.

Svc is the meaning of the service, we need to first create a xxx.svc file into the IIS application directory, this file is the service portal, the client needs the address of this file to access the service (including, of course, the original data exchange).

Let's create this file manually, open VS2010, and select New file, File menu. In the General section, select a text file and click on the "Open" button.

This should be a SVC file, not a. txt file, so let's save it and save it with the option to select all files. I named this file hellowcfservice.svc, which can be used as an optional name and will be part of the service address. The save location is where we just built the IIS application.

Now let's edit the contents of this file, it's simple, just one line of code.

[HTML]View Plaincopyprint?
    1. <% @ServiceHost language=C # debug="true" service="learnwcf.hellowcfservice"%>
<% @ServiceHost language=c# debug= "true" service= "Learnwcf.hellowcfservice"%>

Be <%%> framed This is a server-side inclusion, @ServiceHost tag indicates that this is a WCF service, Lenovo ServiceHost objects in the first two code. language=c# that we write code in the C # language, debug=true as the name implies, the main thing is the service this attribute, he said that the implementation of the services of the class is what, here to use the fully qualified name, that is, to include the namespace. I have a namespace name LEARNWCF, we put the service definition in this namespace, and the latter hellowcfservice is the implementation class of the service. We are going to refine the content of this class next.

As you can see, the Svc file is equivalent to a wizard that helps us find the location of the service in the IIS host, and we can write the code in another place (which can actually be written in the Svc file, not recommended).

Save the contents of the writing and we move on.

Next we will write the class file that defines the service. But before we do this, we first set up a location for the class file, and the IIS code file should be stored in the App_Code subdirectory of the IIS application directory, so let's set up this folder first.

You can see that the hellowcfservice.svc we just established has been identified as a WCF service.

(2) CS file

Back to VS2010, or new file---file, select text file.

This time we are going to set up a class file, whose name is HelloWCFService.cs, note that when saving as the save type is selected as all files, the path to select the App_Code folder we just created

Write this file, we define and implement service contract here, should be familiar with it, try to write it down.

[CSharp]View Plaincopyprint?
  1. Using System;
  2. Using System.ServiceModel;
  3. Namespace LEARNWCF
  4. {
  5. [ServiceContract]
  6. public interface IHELLOWCF
  7. {
  8. [OperationContract]
  9. string HELLOWCF ();
  10. }
  11. public class HELLOWCFSERVICE:IHELLOWCF
  12. {
  13. public string HELLOWCF ()
  14. {
  15. return "Hello wcf!";
  16. }
  17. }
  18. }
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 skilled to play, if there is nothing to understand this code place, quickly turn back to the first review.

Save it, let's move on.

(3) Web. config file

We already know that we also need a configuration file in which to configure the information for endpoints, services, behaviors, and so on. This configuration file is roughly the same as the one we built earlier.

Or go back to VS2010, or create a new text file, save as Web. config. This file name cannot be changed, the save path is the directory where we built the IIS application Iisservice (and the SVC is saved together)

Write him down first, then explain:

[HTML]View Plaincopyprint?
  1. <configuration>
  2. <system.servicemodel>
  3. <services>
  4. <service name="Learnwcf.hellowcfservice" behaviorconfiguration= "metadataexchange ">
  5. <endpoint address="" binding="Wshttpbinding" contract= "learnwcf.ihellowcf " />
  6. <endpoint address="Mex" binding="mexhttpbinding" contract=" IMetadataExchange "/>
  7. </Service>
  8. </Services>
  9. <behaviors>
  10. <servicebehaviors>
  11. <behavior name="MetadataExchange">
  12. <servicemetadata httpgetenabled="true"/>
  13. </behavior>
  14. </servicebehaviors>
  15. </Behaviors>
  16. </system.servicemodel>
  17. </configuration>
<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>

There are some differences between this configuration file and what we wrote earlier:

1) The file name of the configuration file is Web. config, not app. Config

2) The <Service> tag does not have a description of the base address, and in IIS hosting, the service base site is assigned by IIS. For example, the base address of the service in this example is

[HTML]View Plaincopyprint?
    1. Http://localhost/IISService/HelloWCFService.svc
Http://localhost/IISService/HelloWCFService.svc

3) The address of the endpoint is specified in order to be empty, indicating that the service base address is used as the endpoint, of course, you can specify a relative address, but you cannot specify an absolute address, and you must obey the base address specified by IIS.

There is no difference in the rest of the world, especially when specifying the service implementation class and the Contract interface class must take the namespace, this is a very easy mistake.

Save, done

4. Complete

Here, the homestay in IIS is complete, very simple, an IIS application, 3 files. This is, of course, the simplest case.

As for operating services, IIS will do it for us as long as the IIS application (or website) is online and the service is running online.

The old way, look inside the browser, is not successful.

IIS hosted service address format:

[HTML] view Plaincopyprint?
  1. HTTP//Machine name/IIS application name/xxx.svc
HTTP//Machine name/IIS application name/xxx.svc

So our service address for this example should be:

[HTML] view Plaincopyprint?
  1. Http://localhost/IISService/HelloWCFService.svc
Http://localhost/IISService/HelloWCFService.svc

No accident, the situation

Because of IIS, the system automatically gives the machine address for exchanging metadata without using localhost.

5. Summary.

In this article we learned how to host a WCF service in IIS, and the essential elements are summarized in the following points:

(1) Creating IIS applications and physical paths

(2) Create a xxx.svc file under the application path to declare WCF portals and service address navigation

(3) Create a XXX.cs file under the subdirectory of the application path App_Code to define and implement the service contract

(4) Create a Web. config in the application path to configure the service.

(5) Keep IIS up-to-date.

[Honestly learn WCF] The third chapter in IIS hosting Services

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.