Spring.NET and WCF WebHttp Services
-
17-04-2021 - |
Domanda
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 aglobal.asax
just like in MVC, where I can register routes and stuff. - This
Global
inherits fromHttpApplication
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:
- Let
Global
inherit from some Spring class instead ofHttpApplication
- When registering the route, use a custom
ServiceHostFactory
provided bySpring.NET
Does anyone know a ressource or some additional documentation on achieving this? Has anyone done this already?
Soluzione
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>
Altri suggerimenti
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)
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.