1

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:

  • In WCF WebHttp Services I have a global.asax just like in MVC, where I can register routes and stuff.
  • This Global inherits from HttpApplication like the good ol' SpringMvcApplication does.
  • A route looks a bit different though:

    RouteTable.Routes.Add(new ServiceRoute("MyService", new WebServiceHostFactory(), typeof(MyService)));
    

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?

Rajesh
  • 7,766
  • 5
  • 22
  • 35
Jan
  • 2,747
  • 27
  • 35

3 Answers3

3

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>
Yoann
  • 106
  • 2
  • That's exactly what I had in mind when talking about WCF WebHttp Services. Now since WCF Web API it seems to be proven. – Jan Feb 01 '12 at 16:31
  • I'm a newcomer to this topic, and it seems to me that there must be stuff in your spring-config.xml or elsewhere in the project that are needed to make this work. I'd love to see a simple example of a RESTful service using IIS, WFC and Spring. An explanation of how the pieces actually fit together would be a tremendous bonus, but I realize all this is a lot to ask. Please help. – Michael May 19 '14 at 09:43
2

Try something like that and let me know:

RouteTable.Routes.Add(new ServiceRoute("MyService", new WebServiceHostFactory(), 
new ServiceProxyTypeBuilder("MyServiceNameInSpringConfigFile", ContextRegistry.GetContext(), true).BuildProxyType()));

(You need a reference to Spring.Services to use Spring.ServiceModel.Support.ServiceProxyTypeBuilder class)

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
bbaia
  • 1,193
  • 7
  • 8
  • I will try that and come back to you soon. I'm currently buried under other tasks, but I'll give it a try. Thanks! – Jan Dec 01 '11 at 21:37
  • I didn't try that because WCF WebHttp Services is discontinued anyway and will soon replaced by `WCF Web API`, which supports Spring.NET automaticially via MVC3. – Jan Jan 18 '12 at 11:49
0

Since WCF WebHttp Services seems to be discontinued, I did no further investigation.

Successor will be WCF Web API which automaticially allows Spring.NET integration using a plain old MVC3 application as the main project. WCF Web API is currently in Preview state and is expected to get a release candidate around February 2012.

I would suggest the use of this brandnew and very powerful REST framework. But be aware of the risk when integrating with experimental software! It is still in BETA and no one can surely predict, when it will be finally released and which of the current features will still be part of the release. Working upon a BETA framework may affect your project's time schedule.

Jan
  • 2,747
  • 27
  • 35
  • WCF Web API is also discontinued and will be integrated into the upcoming release of ASP.NET as "MVC4 WebApi". – Jan Apr 05 '12 at 12:26