简体   繁体   中英

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

I have configured simple injector as my DI container. I have followed this . I'm guessing my issue has got to do with Registering the container to the SimpleInjectorServiceHostFactory . I'm using class library for wcf services hosted on web application. I have no .svc files but I have configured relative addresses. 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:

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

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

In 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. This ensures that the SimpleInjectorServiceHostFactory is used to activate the service.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM