Friday, August 03, 2007

* WCF : Hosting WCF service in IIS

Previous Post <- WCF Tool : WCF IIS Registration Tool (ServiceModelReg.exe)

  • Pls start from the project created in following post : Developing a basic WCF client and service
  • Add a class which will be used to store two numbers.
        [DataContract]
    public class Numbers
    {
    private int firstNumber;
    private int secondNumber;
    [DataMember]
    public int FirstNumber
    {
    set
    {
    firstNumber = value;
    }
    get
    {
    return firstNumber;
    }
    }
    [DataMember]
    public int SecondNumber
    {
    set
    {
    secondNumber = value;
    }
    get
    {
    return secondNumber;
    }
    }
    }

  • Add another method to the contract which takes instance of Numbers and return the total of the two numbers stored in object.
            [OperationContract]
    int AddNum(Numbers numbers);

  • Create a Virtual Directory in IIS say with name as MathService.
  • Create a text file in this vd with name as MathService.svc
    <% @ServiceHost Service="MathUtility.MathService" %>

  • Create a bin folder in this directory. Add Service library to this folder.
  • Add web.config to vd where we will add our wcf service config details.
    <configuration>
    <!-- This Section is exclusive for WCF configuration for both service and client -->
    <system.serviceModel>
    <!-- This section is to declare services hosted using this cofig file -->
    <services>
    <!-- This section declares individual service. name=type name which implements service contract -->
    <!-- behaviorConfiguration = name of bahavior config which we will define later -->
    <service name="MathUtility.MathService" behaviorConfiguration="MathServiceBehavior">
    <!-- the actual address where service is exposed -->
    <endpoint address="" binding="wsHttpBinding" contract="MathUtility.IMath"></endpoint>
    <!-- The address where metadata is exposed and will be used by Svcutil.exe to generate client classes -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <!-- The behavior which we mentioned above in <service> section-->
    <behavior name="MathServiceBehavior">
    <!-- Exposing metadata on http. Not mandatory for fuctioning of service if client already has metadata-->
    <serviceMetadata httpGetEnabled="True"/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    </configuration>

  • Note there is no <host> tag as we mentioned in self-hosted service because IIS with the virtual directory defines the base address of the service.
  • In this case our service will be hosted at following url : http://<My_Machine>/mathservice/mathservice.svc
  • Access the above url and you will see a default page generated with information on service.
  • You can use svcutil to generate the client code as explained in earlier post to build client and test your service.
  • The address which can be provided to svcutil can be one of following :

    • http://<My_Machine>/mathservice/mathservice.svc?wsdl


    • http://<My_Machine>/mathservice/mathservice.svc


    • http://<My_Machine>/mathservice/mathservice.svc/mex

  • My console app code which uses generated  client.
    using System;
    using System.Collections.Generic;
    using System.Text;
    using MathUtility;

    namespace ClientToIIS
    {
    class Program
    {
    static void Main(string[] args)
    {
    MathClient mathClient = new MathClient();
    Numbers number = new Numbers();
    number.FirstNumber = 1;
    number.SecondNumber = 2;
    Console.WriteLine(mathClient.AddNum(number));
    }
    }
    }

  • For simplicity you can trim the generated client side app.config to only following mandatory elements.
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.serviceModel>
    <client>
    <endpoint address="http://<My_Machine>/MathService/MathService.svc"
    binding="wsHttpBinding"
    contract="IMath" >
    </endpoint>
    </client>
    </system.serviceModel>
    </configuration>

Next Post ->> WCF : Diagnostics Features

 
 




1 comment:

  1. That was helpful.

    One thing that wasn't mentioned here or in other tutorials I've seen was that the IIS virtual directory seems to default to ASP.NET version 1.1. It needs to be set to 2.0, or nothing will work. That caused me a lot of frustration before I figured it out.

    Andrew

    ReplyDelete