WCF 3.5: Hosting Services for WebGet

13. September 2007 10:00

In my last post, I provided an overview of using the new WebGet attribute to map an HTTP GET operation to a specific method.  However, I didn't cover the the necessities for hosting a service that uses this functionality. 

If you intend to expose multiple endpoints that use more than one binding, it is necessary to use the traditional approach of ServiceHost.  In order for WebGet to work correctly, an endpoint that uses the new binding named WebHttpBinding is required.  As usual this can be done programmatically or via configuration.  Regardless of the approach that is used, the WebHttpBehavior must be applied to the endpoint that is using the WebHttpBinding.  This is a special endpoint behavior that enables the web programming model.

ServiceHost host = new ServiceHost(
    new Uri("http://localhost:8000/WeatherService"));

    new WebHttpBinding(),

    new System.ServiceModel.Description.WebHttpBehavior());


Here is the equivalent via configuration:

<?xml version="1.0" encoding="utf-8" ?>
            <behavior name="WebBehavior">
               <webHttp />
         <service name="ServiceApplication.WeatherService">
               contract="ServiceApplication.IWeatherService" />

If you only intend to use the WebHttpBinding, there is a much easier alternative.  There is a specialized implementation of ServiceHost named WebServiceHost that does not require any configuration.  You simply specify the service type and the base uri.  It will automatically use the WebHttpBinding and WebHttpBehavior. 

WebServiceHost host = new WebServiceHost(
    new Uri("http://localhost:8000/WeatherService"));


To use this host in IIS, the following .svc file could be used:

   CodeBehind="~/App_Code/WeatherService.cs" %>


10/15/2007 1:10:40 AM #


Hi Jeff,
I'm having trouble getting this to work.  Would it be possible to post this sample website?


http:// |

10/17/2007 3:28:51 AM #



Take a look at the following file: jeffbarnes.net/.../WhatsNewInWCF35.zip.  This contains the demos that I used for my latest Alabama Code Camp presentation.  It contains code that is very similar to the sample in this post.

http:// |

10/24/2007 4:45:25 PM #

Hi Jeff,

Great set of blogs posts on WCF - thanks.

I downloaded your samples and everything works except the programmable web client application. The call to GetCurrentTemperature throws an InvalidOperationException with the message: "Manual addressing is enabled on this factory, so all messages must be pre-addressed".

Any ideas?

Rob Windsor |

10/25/2007 7:06:38 AM #


Thanks for the feedback Rob.  I will take a look at that and get an update posted.

http:// |

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.

Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn


Shared Items


Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.