Gayan
Gayan

Reputation: 2871

dependency injection on wcf throwing exception

i use simple injector. as their documentation explained i have implemented 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; }

}

i changed my TrackerService.svc to this

<%@ ServiceHost Language="C#" Debug="true" Service="TimeTrackerService.TrackerService" CodeBehind="TrackerService.svc.cs" Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,SimpleInjector.Integration.Wcf" %>

and my 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>

when i try to run the service it throws exception:

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.

enter image description here

i have no idea where to call SetContainer(container) method. did i do any mistake ?

Upvotes: 1

Views: 1136

Answers (1)

Steven
Steven

Reputation: 172835

You are not using your custom WcfServiceFactory, since your WCF service simply references the SimpleInjectorServiceHostFactory. So you should either change your TrackerService.svc to:

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

or you should remove the WcfServiceFactory completely and SimpleInjectorServiceHostFactory.SetContainer(Container) in the start-up path of your application, which will typically be the Application_Start method of your Global.asax file:

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

Upvotes: 2

Related Questions