Hosting of the WCF series (iis-http)

Source: Internet
Author: User
Tags hosting

IIS hosts

If you deploy to IIS, you need to activate the WCF Http Activation feature component.

Let's look at a simple example, let's take a look at the directory structure of the project

This example implements the parameters for passing in two integers, returning their and. (It's surface Pro4, maybe the font is a bit large)

To define a contract:

Implementation services:

Then look at the configuration of the service:

So far, the definition of the service has been implemented, and here's a look at how to host it in IIS.

First, we compile the project, copy the bin directory and the Svc and config file to a folder.

Create a Web site in IIS, map the directory to the directory above, and start the Web site.

Below to verify that the publication is successful, enter the website address in the browser, we can see that the listed directory has an SVC file, we click to open this file.

After opening, we get the following interface, which indicates that the hosting of our service has been successful.

The service has been released, so how to use it, we continue the following operation.

Opens the command-line tool for VS provided by the administrator, generating the client proxy class and configuration for the call with SvcUtil.exe.

Now let's create a console application that will generate two copies to the project and get the following directory structure.

OK, the project is finished, first look at the generated code

[System.CodeDom.Compiler.GeneratedCodeAttribute ("System.ServiceModel", "4.0.0.0")] [System.ServiceModel.ServiceContractAttribute (configurationname= "icalculate")]public interface icalculate{[ System.ServiceModel.OperationContractAttribute (action= "Http://tempuri.org/ICalculate/Add", replyaction= "/http        Tempuri.org/icalculate/addresponse ")] int Add (int a, int b); [System.ServiceModel.OperationContractAttribute (action= "Http://tempuri.org/ICalculate/Add", replyaction= "http:/ /tempuri.org/icalculate/addresponse ")] system.threading.tasks.task<int> addasync (int a, int b);} [System.CodeDom.Compiler.GeneratedCodeAttribute ("System.ServiceModel", "4.0.0.0")]public interface Icalculatechannel:icalculate, system.servicemodel.iclientchannel{}[ System.Diagnostics.DebuggerStepThroughAttribute ()][system.codedom.compiler.generatedcodeattribute (" System.ServiceModel "," 4.0.0.0 ")]public partial class Calculateclient:system.servicemodel.clientbase<icalculate     , icalculate{   Public calculateclient () {} public calculateclient (String endpointconfigurationname): Base (E Ndpointconfigurationname) {} public calculateclient (string endpointconfigurationname, String remoteaddress) : Base (Endpointconfigurationname, remoteaddress) {} public calculateclient (String endpointconfig Urationname, System.ServiceModel.EndpointAddress remoteaddress): Base (Endpointconfigurationname, Remoteaddres s) {} public calculateclient (System.ServiceModel.Channels.Binding Binding, System.ServiceModel.EndpointAddre SS remoteaddress): Base (binding, Remoteaddress) {} public int Add (int a, int b) {ret Urn base.    Channel.add (A, b); } public system.threading.tasks.task<int> Addasync (int a, int b) {return base.    Channel.addasync (A, b); }}

As we can see, two simultaneous and asynchronous methods are generated.

Calculateclient:system.servicemodel.clientbase<icalculate>, icalculate
Service invocation, using the framework of the channel to invoke the remote service.

Here's a look at the configuration of the client

Let's take a look at the configuration of these two endpoints

<!--provides service endpoints--
<endpoint address= "Wcfservice" binding= "Wshttpbinding" contract= "Wcfservice.icalculate"/>
<!--provides the end point of metadata--
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>

The first endpoint is for service delivery, the second is used for endpoint metadata publishing, then this address can generate proxy code for the client, so let's take a look at what happens when the first comment is dropped.

Then take a look at the second endpoint, and still access the metadata normally, using the default provided endpoint.

Then set the Servermetadata's httpgetenabled to false or delete it directly, then this will happen,

Then, with this configuration removed, the second endpoint is configured to have normal access to the metadata.

But that's the way the metadata is addressed.

, MEX is not fixed and can be arbitrarily named

Then the corresponding address is:

The client's call is consistent with the way local code is called.

Results from running:

Well, now it's done. The WCF service is hosted on IIS.

Hosting of the WCF series (iis-http)

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.