简体   繁体   English

WCF:没有为此对象依赖注入Simple Injector定义的无参数构造函数

[英]WCF: no parameterless constructor defined for this object dependency injection Simple Injector

I have configured simple injector as my DI container. 我已将简单的进样器配置为我的DI容器。 I have followed this . 我跟着这个 I'm guessing my issue has got to do with Registering the container to the SimpleInjectorServiceHostFactory . 我猜我的问题与将容器注册到SimpleInjectorServiceHostFactory I'm using class library for wcf services hosted on web application. 我正在使用类库来托管Web应用程序上的wcf服务。 I have no .svc files but I have configured relative addresses. 我没有.svc文件,但我配置了相对地址。 Where do I do the container registration? 我在哪里进行集装箱登记?

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: 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();
}

Custom ServiceFactory 自定义ServiceFactory

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

In WebHost web.config 在WebHost web.config中

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

You should add a factory entry under the serviceActivation element in the web.config file. 您应该在web.config文件中的serviceActivation元素下添加工厂条目。 This ensures that the SimpleInjectorServiceHostFactory is used to activate the service. 这可确保使用SimpleInjectorServiceHostFactory来激活服务。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 依赖注入:没有为此定义无参数构造函数 object - Dependency Injection: No parameterless constructor defined for this object 添加Owin后,依赖注入中断,没有为此对象定义的无参数构造函数 - Dependency Injection broken after adding Owin, no parameterless constructor defined for this object 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object 使用简单注入器的 MVC 中没有带模型绑定的无参数构造函数 - No parameterless constructor with Model Binding in MVC with Simple Injector 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency 依赖注入,带有自定义和简单对象的构造函数 - Dependency Injection, constructor with custom and simple object automapper 中没有为此 object 定义无参数构造函数 - No parameterless constructor defined for this object in automapper 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object 在mvc中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in mvc MissingMethodException:没有为此对象定义无参数构造函数 - MissingMethodException: no parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM