我只需要自己做同样的事情,我必须扩展 web api http 主机工厂。
在此处找到详细信息:Spring.NET WCF Web API
但简而言之,这样做:
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());
}
}
在你的 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>