Step by step teach you to create. Net 4 services and host them in IIS 7.5

Source: Internet
Author: User

This article is translated from walkthrough on creating WCF 4.0 Service and hosting in IIS 7.5

Recently, I was learning about WCF. This part of the host iis7.5 is always uncertain. I searched for a long time. There are also many discoveriesArticleIt's also cloud. It does not work. Then I combined the keywords and searched for an English article. It's done.

Target

This article will teach you step by step

    1. How to create a basic WCF 4.0 Service?
    2. How do I host the WCF Service on IIS 7.5?
    3. How to test service availability on the client
Create a WCF Service

Create a WCF Service, open vs, select a new project, and then select the WCF Service application from the tab of WCF.ProgramTo create a new WCF Service.

InIservice1. CS andRemove the defaultCode.

A. Therefore, the data contract is deleted.
B. Modify the service contract as follows. Write only one method contract and return one string.

Iservice1.cs

Usingsystem; usingsystem. collections. generic; usingsystem. LINQ; usingsystem. runtime. serialization; usingsystem. servicemodel; usingsystem. servicemodel. web; usingsystem. text; namespacewcfservice5 {[servicecontract] publicinterfaceiservice1 {[operationcontract] stringgetmessage ();}}

 

Service1.svc. CS

Usingsystem; usingsystem. collections. generic; usingsystem. LINQ; usingsystem. runtime. serialization; usingsystem. servicemodel; usingsystem. servicemodel. web; usingsystem. text; namespacewcfservice5 {publicclassservice1: iservice1 {publicstringgetmessage (){Return"Hello from WCF Service";}}}
The configuration file will be retained as the default WCF configuration file.

 

Web. config

<? XML version = "  1.0  " ?> <Configuration> <system. Web> <compilation DEBUG ="  True  " Targetframework = "  4.0  " /> </System. Web> <system. servicemodel> <behaviors> <servicebehaviors> <behavior> <servicemetadata httpgetenabled = "  True  " /> <Servicedebug includeexceptiondetailinfaults = "  False  " /> </Behavior> </servicebehaviors> </behaviors> <servicehostingenvironment multiplesitebindingsenabled = "  True  " /> </System. servicemodel> <system. webserver> <modules runallmanagedmodulesforallrequests = "  True  " /> </System. webserver> </configuration>

 

The hosted WCF Service is in iis7.5

Open IIS

A. Use the Administrator permission to open the command line. Click Start-> Run and enter "inetmgr" to open IIS.

Then you will go to the following place.

B. Right-click "website" and click "Add new website"

C. A new window is opened.

Enter the website name .. I gaveHostedwcfservice name

Click "select" and select ASP. NET v4.0 in the application pool. (PS: if not, IIS is not fully installed)

Now, in the physical path, we need to fill in the physical path of the service. In order to know the physical path, right-click our WCF project in resource management view in, select to open in Windows Resource Manager. Copy the address from the address bar of the resource manager. Or you can find it in your browser if you know it clearly.

Then bind it here. Select HTTP. give any port number. I will write 4567

You can leave the host name empty.

Select the option to start the website now.

Click OK.

D. Run the Visual Studio command prompt (2010) as the administrator ).

 

Enter the following command

Aspnet_regiis.exe/IRU

You will be notified of ASP installation soon. net 4.0 (note that some people may not have the block in the previous application pool.. Net 4 option. You can perform this step before executing the previous step .)

E. GoIISAnd the website is ready.

Publish a WCF Service

Return to the WCF Service Project in Vs and right-click to select release.

On clicking of publish a window will come.

You can give a service URL at will, as long as the name is uniquely identified, the service name ends with SVC.

Set the name of the previously created websiteThe hostedwcfservice is written below..

The certificate is not required.

Now click onPublish.

Click Publish to prompt that the release is successful.

Browse services in IIS

Open IIS, and thenHostedwcfservice. Right-click the website and choose manage website from the shortcut menu.

When you click browse. The following situations may occur:

The above error is returned. But this is not the case. Add service1.svc to the URL to enable it.

HTTP: /localhost: 4567/service1.svc

In the browser, you can see that the service is running properly.

Now you have successfully created the WCF 4.0 Service and hosted it in IIS 7.5.

Test the WCF Service on the client

A.Create a console Project

B.Right-click to add service reference

C.Provide the service address, click "go", or click "Discover" to select the service, and click "OK ".

D.Use the following services:

Program. CS

Usingsystem; usingsystem. Collections. Generic; usingsystem. LINQ; usingsystem. Text; usingconsoleapplication1.servicereference1; namespaceconsoleapplication1 {classprogram {staticvoidmain (String[] ARGs) {service1client proxy=Newservice1client (); console. writeline (proxy. getmessage (); console. Read ();}}}

 

Output result.

Link: http://leaver.me/archives/1245.html

 

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.