对 wcf 抛出异常的依赖注入
dependency injection on wcf throwing exception
我使用简单的注射器。正如他们的文档所解释的那样,我已经实施了 WcfServiceFactory
public class WcfServiceFactory : SimpleInjectorServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses)
{
var host = new SimpleInjectorServiceHost(DependencyConfig.Container, serviceType, baseAddresses);
return host;
}
}
public class DependencyConfig
{
static DependencyConfig()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.Register(typeof (IRepositoryAsync<>), typeof (Repository<>));
container.Verify();
Container = container;
}
public static Container Container { get; set; }
}
我把我的TrackerService.svc改成了这个
<%@ ServiceHost Language="C#" Debug="true" Service="TimeTrackerService.TrackerService" CodeBehind="TrackerService.svc.cs" Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,SimpleInjector.Integration.Wcf" %>
和我的web.config
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHBBinding" />
</wsHttpBinding>
</bindings>
<services>
<service name="TimeTrackerService.TrackerService">
<endpoint address="" binding="basicHttpBinding" contract="TimeTrackerService.ITrackerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8878/TimeTrackerService/TrackerService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我尝试 运行 服务时它抛出异常:
The operation failed. Please call the SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory.SetContainer(Container) method supplying the application's SimpleInjector.Container instance during application startup (for instance inside the Application_Start event of the Global.asax). In case you're running on non-HTTP protocols such as net.tcp and net.pipe that is supported by the Windows Activation Service (WAS), please see the WCF integration documentation: https://simpleinjector.org/wcf.
我不知道在哪里调用 SetContainer(container)
方法。我做错了吗?
您没有使用您的自定义 WcfServiceFactory
,因为您的 WCF 服务只是引用 SimpleInjectorServiceHostFactory
。因此,您应该将 TrackerService.svc 更改为:
<%@ ServiceHost Language="C#" Debug="true"
Service="TimeTrackerService.TrackerService"
CodeBehind="TrackerService.svc.cs"
Factory="YourNamespace.WcfServiceFactory" %
或者您应该完全删除 WcfServiceFactory
和应用程序启动路径中的 SimpleInjectorServiceHostFactory.SetContainer(Container)
,这通常是 Global.asax 的 Application_Start
方法文件:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
SimpleInjectorServiceHostFactory.SetContainer(DependencyConfig.Container);
}
}
我使用简单的注射器。正如他们的文档所解释的那样,我已经实施了 WcfServiceFactory
public class WcfServiceFactory : SimpleInjectorServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses)
{
var host = new SimpleInjectorServiceHost(DependencyConfig.Container, serviceType, baseAddresses);
return host;
}
}
public class DependencyConfig
{
static DependencyConfig()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.Register(typeof (IRepositoryAsync<>), typeof (Repository<>));
container.Verify();
Container = container;
}
public static Container Container { get; set; }
}
我把我的TrackerService.svc改成了这个
<%@ ServiceHost Language="C#" Debug="true" Service="TimeTrackerService.TrackerService" CodeBehind="TrackerService.svc.cs" Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,SimpleInjector.Integration.Wcf" %>
和我的web.config
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHBBinding" />
</wsHttpBinding>
</bindings>
<services>
<service name="TimeTrackerService.TrackerService">
<endpoint address="" binding="basicHttpBinding" contract="TimeTrackerService.ITrackerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8878/TimeTrackerService/TrackerService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我尝试 运行 服务时它抛出异常:
The operation failed. Please call the SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory.SetContainer(Container) method supplying the application's SimpleInjector.Container instance during application startup (for instance inside the Application_Start event of the Global.asax). In case you're running on non-HTTP protocols such as net.tcp and net.pipe that is supported by the Windows Activation Service (WAS), please see the WCF integration documentation: https://simpleinjector.org/wcf.
我不知道在哪里调用 SetContainer(container)
方法。我做错了吗?
您没有使用您的自定义 WcfServiceFactory
,因为您的 WCF 服务只是引用 SimpleInjectorServiceHostFactory
。因此,您应该将 TrackerService.svc 更改为:
<%@ ServiceHost Language="C#" Debug="true"
Service="TimeTrackerService.TrackerService"
CodeBehind="TrackerService.svc.cs"
Factory="YourNamespace.WcfServiceFactory" %
或者您应该完全删除 WcfServiceFactory
和应用程序启动路径中的 SimpleInjectorServiceHostFactory.SetContainer(Container)
,这通常是 Global.asax 的 Application_Start
方法文件:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
SimpleInjectorServiceHostFactory.SetContainer(DependencyConfig.Container);
}
}