简体   繁体   English

如何将AutoMapper 5.2与SimpleInjector结合用于IIS中托管的WCF服务

[英]How to combine AutoMapper 5.2 with SimpleInjector for WCF service hosted in IIS

So far we've used the SimpleInjectorServiceHostFactory in the SimpleInjector.Integration.Wcf for our WCF services. 到目前为止,我们已经将SimpleInjector.Integration.Wcf中的SimpleInjectorServiceHostFactory用于我们的WCF服务。 This allowed us to avoid the typical "No parameterless constructor defined", when we have interfaces as parameters that SimpleInjector should resolve. 当我们使用接口作为SimpleInjector应该解析的参数时,这可以避免典型的“未定义无参数的构造函数”。

In the global.asax: 在global.asax中:

var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.Register<IOurBusinessService, OurBusinessService>();
container.Verify();
SimpleInjectorServiceHostFactory.SetContainer(container);

To configure/register AutoMapper we would call some code to register it in the Global.asax, like so: 要配置/注册AutoMapper,我们将调用一些代码将其注册到Global.asax中,如下所示:

var cfg = new MapperConfigurationExpression();
cfg.CreateMap<SomeObject, SomeObjectDTO>();
Mapper.Initialize(cfg);
Mapper.Configuration.AssertConfigurationIsValid();

However it seems that when our Web-Services are called directly with a net.tcp endpoint, there is sometimes no more AutoMapper registered. 但是,似乎当使用net.tcp端点直接调用我们的Web服务时,有时不再注册AutoMapper。 This seems to be the case as the code in Global.asax in Application_Start is never executed, when the WCF service is requested directly. 似乎是这种情况,因为直接请求WCF服务时,从未执行Application_Start中Global.asax中的代码。

We currently try to derive from ServiceHostFactory and register both AutoMapper and SimpleInjector in our overridden CreateServiceHost method. 当前,我们尝试从ServiceHostFactory派生并在覆盖的CreateServiceHost方法中注册AutoMapper和SimpleInjector。 This however does give us again the "No parameterless constructor defined" error. 但是,这确实再次给了我们“未定义无参数的构造函数”错误。

Do you have any solution or best practices? 您有任何解决方案或最佳做法吗?

Is your configuration correct? 您的配置正确吗?

You can create the Mapper at startup and then inject it as a singleton dependency: 您可以在启动时创建Mapper ,然后将其作为单例依赖项注入:

Create the Mapper ( code found here ): 创建Mapper (可在此处找到代码 ):

var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<AppProfile>();
    cfg.CreateMap<Source, Dest>();
});

var mapper = config.CreateMapper();
// or
IMapper mapper = new Mapper(config);

Register as singleton: 注册为单身人士:

var container = new Container();

// Registrations
container.RegisterSingleton(typeof(IMapper), mapper);

To inject: 注入:

public class MyClass 
{
    private readonly IMapper mapper;
    public MyClass(IMapper mapper)
    {
        this.mapper = mapper;
    }
}

I am not sure if this will solve you "No parameterless constructor" issue, but this is a good way to handle the AutoMapper injection. 我不确定这是否可以解决“无参数构造函数”问题,但这是处理AutoMapper注入的好方法。 If It does not solve the problem, let me know. 如果它不能解决问题,请告诉我。

Let me know if you have questions. 如果您有任何问题,请告诉我。

One way to make the above scenario work was to derive from SimpleInjectorServiceHostFactory and do the override there. 使上述方案起作用的一种方法是从SimpleInjectorServiceHostFactory派生并在那里进行覆盖。

public class OurServiceHostFactory : SimpleInjectorServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        // ConfigInit is a static class with a simple check, to see if the configuration was already initialized
        // the same method ConfigInig.Configure() is also called in the Global.asax in Application_Start
        ConfigInit.Configure();
        return base.CreateServiceHost(serviceType, baseAddresses);
    }
}

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

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