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(
    typeof(WeatherService),
    new Uri("http://localhost:8000/WeatherService"));

host.AddServiceEndpoint(
    typeof(IWeatherService),
    new WebHttpBinding(),
    "");

host.Description.Endpoints[0].Behaviors.Add(
    new System.ServiceModel.Description.WebHttpBehavior());

host.Open();

Here is the equivalent via configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <system.serviceModel>
      <behaviors>
         <endpointBehaviors>
            <behavior name="WebBehavior">
               <webHttp />
            </behavior>
         </endpointBehaviors>
      </behaviors>
      <services>
         <service name="ServiceApplication.WeatherService">
            <endpoint 
               name="WebEndpoint"
               behavior="WebBehavior"
               address="http://localhost:8000/WeatherService"
               binding="webHttpBinding"
               contract="ServiceApplication.IWeatherService" />
         </service>
      </services>
   </system.serviceModel>
</configuration>

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(
    typeof(WeatherService),
    new Uri("http://localhost:8000/WeatherService"));

host.Open();

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

<%@ 
   ServiceHostFactory="System.ServiceModel.Activation.WebServiceHostFactory"
   Language="C#"
   Service="ServiceApplication.WeatherService"
   CodeBehind="~/App_Code/WeatherService.cs" %>

Comments

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

http://

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

Matt

http:// |

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

http://

Matt,

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 #

http://

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

Disclaimer

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.