• Feeds

    Subscribe in a reader

  • Ads

A Brief Aside: WebServiceHostFactory

Ouch, it looks like Jef ran headlong into a bit of a brick wall using the Web programming model inside of ASP.NET. 

That one's my fault for not getting the time to write an ASP.NET sample before the SDK shipped. Had I done that, I could have saved Jef some pain and a long blog post.

So Jef, I apologize. I owe you a beer next time I see you. Let me try to make it up to you.

The basic gist of Jef's post is that "there's too much *@#%!^ config required to use your stuff!". I agree. That's why we added two types to the Web programming model for making this easier -- System.ServiceModel.Web.WebServiceHost and System.ServiceModel.Web.WebServiceHostFactory. Both of these guys ship in the BizTalk Services SDK.

I like to think of them as the 'do the right thing' service host for Web-style services. Absent any configuration, they will auto-configure a default Web endpoint at the service's base address. The WebServiceHostFactory knows about IIS metabase settings so it can properly configure the endpoint's binding for HTTPS. It will also add the WebHttpBehavior to the endpoint to enable web-style dispatching.

Net/net, all you have to do to use the Web programming model in ASP.NET is to create a web site project that references the Microsoft.ServiceModel.Web assembly. Once you've done that, you can write the service in one file (e.g. Service.svc):

<% @ServiceHost Factory="System.ServiceModel.Web.WebServiceHostFactory"Service="Service" Language="C#" %>using System;using System.Collections.Generic;using System.ServiceModel;using System.ServiceModel.Description;using System.ServiceModel.Web;using System.Text;[ServiceContract]public class Service{//you can hit this at http://<app>/Service.svc/EchoWithGet?s=hello, world[OperationContract][WebGet()]public string EchoWithGet(string s){return "In EchoWithGet: You said " + s;}}

Note the Factory attribute in the @ServiceHost directive. That's the key to the whole thing. Once you have that, you don't need any service or endpoint config. The only thing you need in web.config is the reference to Microsoft.ServiceModel.Web.

Hope that helps with keeping simple things simple.

Note: in the SDK build, we're a bit aggressive about requiring a trailing slash in some cases (particularly if you have UriTemplate="" or UriTemplate="*"). That will be much more usable by RTM, but for now just a heads up.

Technorati Tags: ,
#1 Jef Newsom on 5.08.2007 at 11:47 AM

Woo Hoo :)

#2 Erik Johnson on 5.08.2007 at 4:25 PM

Steve, I whined along with Mike Parson's on Don's Orcas post about the value of the WCF programming model on straight HTTP.Mike and I work together (even though we are on opposite corners of the continent), so we tend to think alike in these situations.I did a project once with WSE where I used derived a class from SoapReceiver.I thought it worked really well because it split SOAP from non-SOAP HTTP requests and gave me direct access to the input and output streams for POX-style operations.Have you thought about developing a couple of examples to help some of us coming from this direction see the WCF light?Your example here is definately helpful, but what if don't want to necessarily bind an operation to my URL?How do I get the content of an arbitrary POST?Thanks!-Erik

#3 Steve Maine on 5.08.2007 at 7:08 PM

Ah, I see. Thanks Erik. There's a lot of ways you can go -- the simplest probably being a single Message/Message operation with a [WebInvoke( UriTemplate="*" )]. I've got a post in the queue about how to do a Stream/Stream contract a la IHttpHandler or servlets on top of this.But if you don't want to do any of that, integrating the UriTemplate/UriTemplateTable stuff with IHttpHandler/HttpListener/Socket is trivial, since those two classes have no dependencies on WCF and are indended for stand-alone use if that's what you want to do.The Syndication stuff is stand-alone, too, although it has a nice "better together" story with WCF.Our goal is to make Web customers happy on our (Microsoft's) platform, not crush their skulls under the jackboot of WCF. We certainly think that WCF has a ton of value in these scenarios, but if all you need is IHttpHandler and UriTemplates then by all means, go in peace.