WCF Simplified Part 6: Hosting a WCF service using IIS

We’ve been self hosting the WCF service library WcfServiceLibrary1.dll in our own console application. Today we’ll see how to host the the service library using IIS.

1. Open the host solution we built in the first post (as admin), and add a new web site to the solution,

image

Then add a WCF Service,

image

2. Now click on the web site project and add a project reference to the WcfServiceLibrary1 project.

image

3. Note that inside the App_Code folder, there is already a template to create a WCF service. But it’s recommended to keep the WCF service library separate from the hosting process, which is why we added the project reference to our WcfServiceLibrary1 and we’ll delete the two files in App_Code, IService.cs and Service.cs. Now, you solution should look like this,

image

4. Now open up the Service.svc file and change it to,

<%@ ServiceHost Service="WcfServiceLibrary1.Service1" %>

 

5. Now you should be able to run the website and see the Visual Studio auto-magically created a host for you,

image

6. Now we can run our client against this IIS-hosted endpoint. By default, this endpoint uses BasicHttpBinding, so change the client to point to the .svc file using BasicHttpBinding should work.

EndpointAddress epoint = new EndpointAddress("http://localhost:60883/WCFService1/Service.svc");
ServiceReference1.IService1 proxy = ChannelFactory<ServiceReference1.IService1>.CreateChannel(new BasicHttpBinding(), epoint);

7 If you want more control over the endpoint, say change the protocol to WSHttpBinding, change the Web.config to,

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="WcfServiceLibrary1.Service1">
        <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

8. Now if you run the client you’ll see a protocol exception,

image

9. Changing the client to use WSHttpBinding will work,

EndpointAddress epoint = new EndpointAddress("http://localhost:60883/WCFService1/Service.svc");
ServiceReference1.IService1 proxy = ChannelFactory<ServiceReference1.IService1>.CreateChannel(new WSHttpBinding(), epoint);

Advertisements

About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

3 Responses to WCF Simplified Part 6: Hosting a WCF service using IIS

  1. Pingback: WCF Simplified Series « I.Net

  2. Pingback: WCF Simplified Part 7: Hosting a WCF service using WPF « I.Net

  3. Aw, this was a very good post. Finding the
    time and actual effort to make a good article… but what can I say… I put things off a
    lot and never manage to get anything done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: