Chapter 4: WCF hosting (1)

Original: http://www.wcftutorial.net/Introduction-to-WCF.aspx

IIS 5/6 hosting

The biggest advantage of hosting the service in IIS is that when he receives the first request from the client, it will automatically Start the hosting process. It uses many features of IIS, such as process recycling, idle shutdown, process health monitoring and message-driven. The biggest disadvantage is that it only supports the HTTP protocol.

Let's do something random and create a service hosted on IIS.

Step 1: Start VS2008, File->New->Web Site, select WCF Service and store it in the path http. This will host the service directly on IIS, then select OK.



Step 2: Create a HelloWorld service that will accept a name as a parameter and return Hello name. The interface and implementation are shown below.

IMyService.cs
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string HelloWorld(string name);
}


MyService.cs
public class MyService : IMyService
{
    #region IMyService Members
    public string HelloWorld(string name)
    {
        return "Hello " + name;
    }
    #endregion
}


Step 3: The Service file (.svc) contains the service name and background program code. This file is used to advertise the service.

MyService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>


Step 4: It needs to be configured in the config file of the server. Here we only configure one endpoint of wsHttpBinding, we can also configure multiple endpoints of different binding protocols. Because we are hosting in IIS. We can only use Http binding. We'll learn more about endpoints and configuration in later tutorials.

Web.Config

<system.serviceModel>
    <services>
        <service behaviorConfiguration="ServiceBehavior" name="MyService">
            <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService">
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <!-- To avoid disclosing metadata information,
                set the value below to 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 to true.  
                Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>


Note:

You need to configure the service name and service address in the configuration file.



when we run the program.



Step 5: We have now successfully hosted the service on IIS, the next step is to create a client application to call the service. Before creating the client application, we need to create a proxy for the service. These proxies are used in client programs to interact with the service. To create the proxy, run the VS2008 command line tool and use the service tool to create the proxy class and its configuration information.

svcutil http://localhost/IISHostedService/MyService.svc



Step 6: Now we start to create the console application for VS2008 client.



Step 7: Add a System.ServiceModel reference



Step 8: Create an object for the proxy class and call the HelloWorld method

static void Main(string[] args)
{
    //Creating Proxy for the MyService
     MyServiceClient client = new MyServiceClient();
     Console.WriteLine("Client calling the service...");
     Console.WriteLine(client.HelloWorld("Ram"));
     Console.Read();
}


Step 9: Run the program, the output is as follows



I hope you will enjoy hosting the service on IIS, now let's see how to host the program on self-host.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326802562&siteId=291194637