wcfrestspring.netwebhttp

Spring.NET and WCF WebHttp Services


I'm investigating in using Microsoft's WCF WebHttp Services for creating a RESTful API. In the past there has been the WCF REST Starter Kit for .NET 3.5, which now seem to have been replaced by the WCF REST Service Template 40 in .NET 4.

Of course I want to use Spring.NET's DI, but I can't seem to find any ressources on the web explaining how to successfully integrate Spring.NET into WCF WebHttp Services.

I do know the quite "interesting" way to get Spring into my conventional WCF Services, but does anyone know how to integrate Spring.NET with WCF WebHttp Services?

Some details, to whose are interested:

I would assume two possible ways for hooking Spring into that:

  1. Let Global inherit from some Spring class instead of HttpApplication
  2. When registering the route, use a custom ServiceHostFactory provided by Spring.NET

Does anyone know a ressource or some additional documentation on achieving this? Has anyone done this already?


Solution

  • I have just had to do the same thing myself, I had to extend the web api http host factory.

    find the details here: Spring.NET WCF Web API

    But in short do this:

    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }
    
        private void RegisterRoutes()
        {
            RouteTable.Routes.Add(new ServiceRoute("Catalog", new SpringHttpServiceHostFactory(), typeof(UnitysCatalogService)));
        }
    }
    
    public class SpringHttpServiceHostFactory : HttpServiceHostFactory
    {
        private IApplicationContext _applicationContext;
    
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            Configuration.CreateInstance = GetInstance;
            Configuration.ReleaseInstance = ReleaseInstance;
    
            return base.CreateServiceHost(serviceType, baseAddresses);
        }
    
        private object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
        {
            return GetApplicationContext().GetObject(serviceType.Name);
        }
    
        private void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
    
        }
    
        private IApplicationContext GetApplicationContext()
        {
            return _applicationContext ?? (_applicationContext = ContextRegistry.GetContext());
        }
    }
    

    and in your web.config:

    <configuration>
      <configSections>
        <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
        </sectionGroup>
      </configSections>
    
      <spring>
        <context>
          <resource uri="file://~/Config/spring-config.xml" />
        </context>
      </spring>
      <!-- .... -->
    </configuration>