依赖注入wcf抛出异常

时间:2015-10-26 11:50:03

标签: c# wcf simple-injector

我使用简单的注射器。正如他们的文档解释我已实施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>

当我尝试运行该服务时,它会引发异常:

  

操作失败。请调用SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory.SetContainer(Container)方法,在应用程序启动期间提供应用程序的SimpleInjector.Container实例(例如,在Global.asax的Application_Start事件中)。如果您在Windows激活服务(WAS)支持的非HTTP协议(如net.tcp和net.pipe)上运行,请参阅WCF集成文档:https://simpleinjector.org/wcf

enter image description here

我不知道在哪里调用SetContainer(container)方法。我做错了吗?

1 个答案:

答案 0 :(得分:1)

您没有使用自定义WcfServiceFactory,因为您的WCF服务只是引用SimpleInjectorServiceHostFactory。因此,您应该将TrackerService.svc更​​改为:

<%@ ServiceHost Language="C#" Debug="true"
    Service="TimeTrackerService.TrackerService"
    CodeBehind="TrackerService.svc.cs"
    Factory="YourNamespace.WcfServiceFactory" %

或者您应该在应用程序的启动路径中完全删除WcfServiceFactorySimpleInjectorServiceHostFactory.SetContainer(Container),这通常是Global.asax文件的Application_Start方法:

public class Global : System.Web.HttpApplication
{       
    protected void Application_Start(object sender, EventArgs e)
    {
        SimpleInjectorServiceHostFactory.SetContainer(DependencyConfig.Container);
    }
}