2016-04-21 39 views
3

我已將簡單注入器配置爲我的DI容器。我跟着this。 我猜我的問題與註冊容器到SimpleInjectorServiceHostFactory有關。 我正在使用Web應用程序上託管的wcf服務的類庫。我沒有.svc文件,但我配置了相對地址。我在哪裏做容器註冊?WCF:爲此對象依賴注入定義無參數構造函數簡單注入器

public static class ContainerBootstrap 
{ 
    public static Container Container { get; set; } 

    static ContainerBootstrap() 
    { 
     var container = new Container(); 

     container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle(); 

     container.RegisterSingleton<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(); 

     container.Register<IDepartmentService, DepartmentService>(); 

     var typeAdapterFactory = container.GetInstance<ITypeAdapterFactory>(); 
     TypeAdapterFactory.SetCurrent(typeAdapterFactory); 

     Container.Verify(); 

     Container = container; 
    } 
} 

AutomapperTypeAdapterFactory:

public class AutomapperTypeAdapterFactory : ITypeAdapterFactory 
{ 
    public AutomapperTypeAdapterFactory() 
    { 
     var profiles = AppDomain.CurrentDomain 
           .GetAssemblies() 
           .SelectMany(a => a.GetTypes()) 
           .Where(t => t.BaseType == typeof(Profile)); 

     var config = new MapperConfiguration(cfg => 
     { 
      foreach (var profile in profiles) 
      { 
       if (profile.FullName != "AutoMapper.SelfProfiler`2") 
        cfg.AddProfile(Activator.CreateInstance(profile) as Profile); 
      } 
     }); 

     ContainerBootstrap.Container.Register<MapperConfiguration>(() => config); 
     ContainerBootstrap.Container.Register<IMapper>(() => 
      ContainerBootstrap.Container.GetInstance<MapperConfiguration>() 
       .CreateMapper()); 
    } 

    public ITypeAdapter Create() => new AutomapperTypeAdapter(); 
} 

定製服務工廠

public class WcfServiceFactory : SimpleInjectorServiceHostFactory 
{ 
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     return new SimpleInjectorServiceHost(ContainerBootstrap.Container, serviceType, 
      baseAddresses); 
    } 
} 

在虛擬主機提供商的web.config

<serviceActivations> 
    <add factory="Department.InstanceProviders.WcfServiceFactory" 
     service="Department.DepartmentService" relativeAddress="DepartmentService.svc" /> 
</serviceActivations> 
+0

你能解釋爲什麼你有自定義工廠?爲什麼不使用默認的簡單噴油器供應? –

+0

這是一個設計決定,除非你想告訴我這是[email protected]的過程 – mkaris

+0

在這一點上,我真的不知道這是否是例外的原因。但我會試着從默認開始看它是否是。 –

回答

4

你應該在serviceActivation元素下添加入廠web.config文件。這確保SimpleInjectorServiceHostFactory用於激活服務。

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
    multipleSiteBindingsEnabled="true"> 
    <serviceActivations> 
     <add factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory, 
SimpleInjector.Integration.Wcf" 
      relativeAddress="~/Service1.svc" 
      service="TestService.Service1" /> 
    </serviceActivations> 
</serviceHostingEnvironment> 
+0

添加一個工廠條目,意思是這個' mkaris

+0

我已經有一個自定義的web.config中定義的條目。我沒有看到什麼,也許@史蒂文 – mkaris

相關問題