5-day service hosting

Source: Internet
Author: User

Today is the final part of the series. Of course, I would like to share my knowledge about the hosting of WCF.

 

There are four types of hosting services in WCF:

 

Console HOST: it is conducive to development and debugging, but not the best practice in the production environment.

Winform HOST: it is convenient to interact with users. Users can enable winform when they want to enable it, but if the machine restarts, they have to manually enable it, which is dangerous and troublesome.

Iis host: This host has been widely used in practical projects.

Benefits: Start and Stop with the system.

IIS has a large number of management policies to manage it.

That is, you want to use the functions of WCF and also access the functions of Asp.net.

Window service host: This host is also widely used in practice and is also enabled or stopped with the system.

 

Okay. I believe that the first two types of boarding will be shared by everyone. I will not introduce them here. I will mainly introduce the last two types of boarding.

 

Iis host: First, you need to know the three conditions of the host on IIS: ApplicationProgramConfiguration section of the domain (equivalent to servicehost) + SVC file + config

Thank you very much for the vs template, which already contains the template used to host IIS. OK ,:

The following process is:

Click OK --> right-click the WCF Service Project --> click "publish" --> select "file deployment" in "Release Method" --> select "Save location" in "target location ""--> click" release ".

 

Then we open the local IIS, create a website, modify the port to 1111, and then click OK, as shown below:

 

 

We have already mentioned three conditions for boarding in IIS,

First, check the "application domain". By default, the version of the. NET Framework of the newly created website in the application domain is 2.0,

Therefore, we must change it to 4.0.

As follows:

 

Next, let's take a look at the "SVC file": one feature of IIS hosting is that you do not need to specify the address in the endpoint, because the address of the SVC file is the "endpoint" address,

That is to say, if you want to preview the file, you need to enter the file address in the URL.

Let's take a look at "http: // localhost: 1111/service1.svc? The metadata in the WSDL "shows that the binding is" basichttpbinding ", that is, if

To change this low-end binding, you must modify the last of the three conditions.

As follows:

Finally, let's take a look at the "Config File", which is the file generated by the template.CodeAs follows:

 1 <? XML version = "  1.0  " Encoding = "  UTF-8  " ?>
2 <Configuration>
3
4 <System. Web>
5 <Compilation DEBUG = " True " Targetframework = " 4.0 " />
6 </System. Web>
7 <System. servicemodel>
8 <Behaviors>
9 <Servicebehaviors>
10 <Behavior>
11 <! -- To avoid metadata leakage, set the following values before deployment: False And delete the above metadata endpoint -->
12 <Servicemetadata httpgetenabled = " True " />
13 <! -- To receive fault exception details for debugging, set the following value True . SetFalse To avoid leakage of abnormal information -->
14 <Servicedebug includeexceptiondetailinfaults = " False " />
15 </Behavior>
16 </Servicebehaviors>
17 </Behaviors>
18 <Servicehostingenvironment multiplesitebindingsenabled = " True " />
19 </System. servicemodel>
20 <System. webserver>
21 <Modules runallmanagedmodulesforallrequests = " True " />
22 </System. webserver>
23
24 </Configuration>

In other words, there is nothing. You need to change the binding to wshttpbinding and add the endpoint:

 1 <? XML version = "  1.0  " Encoding = "  UTF-8  " ?>
2 <Configuration>
3
4 <System. Web>
5 <Compilation DEBUG =" True " Targetframework = " 4.0 " />
6 </System. Web>
7 <System. servicemodel>
8 <Services>
9 <Service name = " Wcfservice1.service1 " >
10 <Endpoint address = "" Binding = " Wshttpbinding " Contract = " Wcfservice1.iservice1 " > </Endpoint>
11 </Service>
12 </Services>
13 <Behaviors>
14 <Servicebehaviors>
15 <Behavior>
16 <! -- To avoid metadata leakage, set the following values before deployment: False And delete the above metadata endpoint -->
17 <Servicemetadata httpgetenabled = " True " />
18 <! -- To receive fault exception details for debugging, set the following value True . SetFalse To avoid leakage of abnormal information -->
19 <Servicedebug includeexceptiondetailinfaults = " False " />
20 </Behavior>
21 </Servicebehaviors>
22 </Behaviors>
23 <Servicehostingenvironment multiplesitebindingsenabled = " True " />
24 </System. servicemodel>
25 <System. webserver>
26 <Modules runallmanagedmodulesforallrequests = " True " />
27 </System. webserver>
28
29 </Configuration>

 

Client call:

 

 

Window Server Host:

To implement window server boarding, we are also very grateful to vs for providing the "window service" template for us.

The procedure is as follows: "file"-> "new"-> "project"-> "WCF Service library" template-> "Click OK"-> right-click the service library project and add "new item"-> find "window service"-> "click Add ".

To facilitate the change of "wcfservicelibrary1" to the console application, and then create a new main entry.

Then, click"Click here to switch to the Code view"Button to go to the Code view,

Then we can add code in the "onstart" and "onstop" methods.

 1   Using System;
2 Using System. Collections. Generic;
3 Using System. componentmodel;
4 Using System. Data;
5 Using System. diagnostics;
6 Using System. LINQ;
7 Using System. serviceprocess;
8 Using System. text;
9 Using System. servicemodel;
10
11 Namespace Wcfservicelibrary1
12 {
13 Partial Class Service2: servicebase
14 {
15 Public Service2 ()
16 {
17 Initializecomponent ();
18 }
19
20 Servicehost host;
21
22 Protected Override Void Onstart (String [] ARGs)
23 {
24 // Todo: Add code here to start the service.
25 Host = New Servicehost ( Typeof (Service1 ));
26
27 Host. open ();
28 }
29
30 Protected Override Void Onstop ()
31 {
32 // Todo: Add code here to stop the service.
33 Host. Close ();
34 }
35 }
36 }

Then, right-click the "Design Interface", find "add installer", and click.

 

 

After adding, components of "servceprocessinstaller1" and "servcieinstaller1" are displayed.

Right-click "serviceprocessinstaller1", open the Properties window, and change the account to LocalSystem;

 

Right-click the "servcieinstaller1" component and open the Properties window.

Modify servciename to myservicehost, which defines the service name.

Modify starttype to: automatic.

 

The last step is to register the EXE to the window Server:

 1   Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. serviceprocess;
6
7 Namespace Wcfservicelibrary1
8 {
9 Public Class Program
10 {
11 Public static Void Main ()
12 {
13 Servicebase. Run ( New Service2 ());
14 }
15 }
16 }

Now, we can compile the project and install the deployment. vsprovides us with an installutil.exe tool,

This tool truly sends our exe program to the window server.

 

Open the dialog box to find installutil.exe "and our website wcfservicelibrary1.exe".

 

OK. The installation is complete. Now we are doing a test.

Look, I found my window Server service.

 

Enable the service, and then preview the WCF address to see if it is actually started.

Haha, you can do it. Window service boarding is done.

 

So far, the wcf5 series has been successfully completed. Finally, I would like to thank my friends who have been paying attention to this series. I wish you a better understanding of WCF.

 

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.