WCF Service-Oriented Application series 13: hosting-IIS hosting

Source: Internet
Author: User
Tags hosting visual studio 2010

The previous chapter describes the basic knowledge of hosting. This chapter describes IIS hosting. Hosting services in IIS is similar to hosting classic asmx Web Services. You need to create a virtual directory under IIS and provide a. SVC file .. The SVC file is similar to the. asmx file and is mainly used to identify hidden files and services behind classes.Code. If you are using IIS, the base address of the service must be the same as the address of the. SVC file. In addition to enabling the was activation service, IIS only supports the HTTP/HTTPS transmission protocol. According to the protocol supported by IIS, We can bind the Protocol (for details about binding, refer: the following methods are available in the WCF Service-Oriented Application series: binding-basic knowledge:Basichttpbinding,Wshttpbinding,Wsdualhttpbinding. here we can use a demo to introduce IIS hosting.

Development Environment: Visual Studio 2010 + Net Framework 4.0.

1. Create a WCF Service library with the following code:

NamespaceServicelibrary
{
//Servicecontract is a service contract
[Servicecontract]
Public InterfaceIhellowcf
{
//Operationcontract is a method contract
[Operationcontract]
StringGetmessage (StringMSG );
}
}

 

  Namespace  Servicelibrary
{
Public Class Hellowcf: ihellowcf
{
Public String Getmessage ( String MSG)
{
Return String . Format ( " The server received ed message is: {0} " , MSG );
}
}
}

2. Create a WCF Service website, delete the automatically generated code, add the above project reference servicelibrary, add a hellowcfservice. SVC file, and modify the Web. config file.

The content of hellowcfservice. SVC is as follows:

  <%@ Servicehost Service="Servicelibrary. hellowcf"%>

The content of web. config is as follows:

  <?  XML version  =  "  1.0 "  ?>  
< Configuration >

< System. Web >
< Compilation debug = " False " Targetframework = " 4.0 " />
</ System. Web >
< System. servicemodel >
< Services >
< Service name = " Servicelibrary. hellowcf " >
< Endpoint address = " Basichttp " Binding = " Basichttpbinding " Contract = " Servicelibrary. ihellowcf " />
< Endpoint address = " MEX " Binding = " Mexico httpbinding " Contract = " Imetadataexchange " />
</ Service >
</ Services >
< Behaviors >
< Servicebehaviors >
< Behavior >
<! -- To avoid disclosing metadata information, Set The value below False And remove the metadata endpoint above before deployment -->
< Servicemetadata httpgetenabled = " True " />
<! -- To receive exception details In Faults For Debugging purposes, Set The value below True . Set False Before deployment to avoid disclosing exception information -->
< Servicedebug includeexceptiondetailinfaults = " False " />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
< Servicehostingenvironment multiplesitebindingsenabled = " True " />
</ System. servicemodel >
< System. Webserver >
< Modules runallmanagedmodulesforallrequests = " True " />
</ System. Webserver >

</ Configuration >

3. Configure IIS, add a Website: www.hellowcf.com, specify the directory as the newly created WCF Service website directory, and modify the hosts file (c: \ windows \ system32 \ drivers \ etc) add item: 127.0.0.1 www.hellowcf.com to facilitate debugging by setting the server on which the website is started in Visual Studio as: http://www.hellowcf.com, such:

At this point, the server has been compiled. to verify whether the server is available, type "success" in the browser. Because metadata visibility is enabled in the web. config file ( < Servicemetadata httpgetenabled = " True " /> And<Endpoint address="MEX"Binding="Mexico httpbinding"Contract="Imetadataexchange" /> ) Then, by typing in the browser: http://www.hellowcf.com/HelloWCFService.svc? WSDL. We can see the description of this service. If you do not want to expose our service metadata, you can setServicemetadata: < Servicemetadata httpgetenabled = " False " /> And delete or comment out<Endpoint address="MEX"Binding="Mexico httpbinding"Contract="Imetadataexchange" />.

4. Create a client project and add service references. The main code is as follows:

    Class  Program
{
Static Void Main ( String [] ARGs)
{
Using (Server. hellowcfclient Client = New Server. hellowcfclient ())
{
Console. writeline ( " ------------ Begin ------------- " );
Console. writeline (client. getmessage ( " Hello WCF! " ));
Console. writeline ( " ------------ End --------------- " );
}

Console. Readline ();
}
}

5. Compile the clientproject and set it as a startup Item. Then, ctrlw.f5(run client.exe) start the project (previously, we started the server and client through F5, and our server was hosted in IIS. When we accessed the service address, the service automatically started ), you can see the following input:

 
  ------------Begin-------------
The server received ed messageIs: Hello WCF!
------------End---------------

At this point, the introduction of hosting (hosting)-IIS hosting is complete. In the next chapter, we will introduce self-hosting ).

Click here to download the demo.

  Author: Xinhai julan
Source: http: xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

 

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.