Http://blog.csdn.net/songyefei/article/details/7381595
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.
[HTML]View plaincopy
- <% @ 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.
[CSHARP]View plaincopy
- 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)
Write it down first and explain it again:
[HTML]View plaincopy
- <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
[HTML]View plaincopy
- 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:
[HTML]View plaincopy
- Http: // machine name/Iis Application name/xxx. SVC
Therefore, the service address in this example should be:
[HTML]View plaincopy
- Http: // localhost/iisservice/hellowcfservice. SVC
No surprises
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.