When it comes to creating a new Service running on SharePoint 2010, people seem to usually use the Sharepoint MultipleBaseAddressBasicHttpBindingServiceHostFactory.
However, I would like to use the standard .net/WCF WebScriptServiceHostFactory instead because this gives me JavaScript code by calling the Service URL with /js.
My Service Class itself is still decorated with the required Attributes:
[BasicHttpBindingServiceMetadataExchangeEndpoint]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(Namespace = "http://mycompany/namespace")]
public class MyService : IMyServiceContract
The whole Service actually works fine, but I just wonder what the real differences are? What would the SharePoint ServiceHostFactory give me?
This is a great question! My curiosity finally got the better of me, and I started poking around in Reflector. It’s not the authoritative final answer, but I think you’ll be interested in what I learned.
First off:
MultipleBaseAddressBasicHttpBindingServiceHostFactorysimply generatesMultipleBaseAddressBasicHttpBindingServiceHostobjects;WebScriptServiceHostFactorysimply generatesWebScriptServiceHostobjects.Both hosts inherit
System.ServiceModel.ServiceHost, so they have a healthy base of common ground. Lucky for us, this reduces the size of the code footprint we must inspect to get a fair comparison.Now, though there’s not much new code in each class, they quickly branch off in different directions. Take a look at the
OnOpening()implementations.In
WebScriptServiceHostwe have:Unless I am mistaken, the line reading
endpoint.Behaviors.Add(new WebScriptEnablingBehavior());is responsible for the URL behavior (/js) you want.Now, in
MultipleBaseAddressBasicHttpBindingServiceHost, we have (abbreviated):The differences between
WebServiceHost.AddAutomaticWebHttpBindingEndpoints()andthis.CreateEndpoints()were not abundantly clear to me. I got the impression that the SharePoint host contained more automatic configuration support for different authentication models.ClientServiceHost.CreateServiceHost()is a factory method that creates aClientServiceHostobject based on a type listed in the web.config sectionmicrosoft.sharepoint.client/serverRuntime.The
OnServiceHostOpeningInternal()method simply forwards the call to the host’sOnServiceHostOpening()method. Looking at the web.config of a default install, we find the assembly qualified name for the service host type, from which we can inspect the method:Here’s where it gets interesting. The
OnServiceHostOpening()method looks like this:Drilling in further, we see a metric boatload of logic surrounding configuring a host for claims-based authentication.
Aha! There is a difference worth noting. Unless I’m mistaken, there’s no claims-based authentication support in
WebScriptServiceHost.Conclusion
Perhaps you are not using claims-based authentication. If so, you might find that using
WebScriptServiceHostis fine. However, were it me writing the service, I’d create a new host and host factory inheriting the Microsoft types, and see if I could create the /js extension by usingendpoint.Behaviors.Add(new WebScriptEnablingBehavior(). The worst thing that could happen is that it wouldn’t work. On the other hand, if it does work, you could expect to have a highly-SP-compatible service host factory that supports your endpoint preferences.I hope this helps.