Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将AutoMapper 5.2与托管在IIS中的SimpleInjector for WCF服务相结合_C#_Wcf_Automapper_Simple Injector - Fatal编程技术网

C# 如何将AutoMapper 5.2与托管在IIS中的SimpleInjector for WCF服务相结合

C# 如何将AutoMapper 5.2与托管在IIS中的SimpleInjector for WCF服务相结合,c#,wcf,automapper,simple-injector,C#,Wcf,Automapper,Simple Injector,到目前为止,我们已经将SimpleInjector.Integration.Wcf中的SimpleInjectorServiceHostFactory用于我们的Wcf服务。 当我们将接口作为SimpleInjector应该解析的参数时,这允许我们避免典型的“无参数构造函数定义” 在global.asax中: var container = new Container(); container.Options.DefaultScopedLifestyle = new WcfOperationLif

到目前为止,我们已经将SimpleInjector.Integration.Wcf中的SimpleInjectorServiceHostFactory用于我们的Wcf服务。 当我们将接口作为SimpleInjector应该解析的参数时,这允许我们避免典型的“无参数构造函数定义”

在global.asax中:

var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.Register<IOurBusinessService, OurBusinessService>();
container.Verify();
SimpleInjectorServiceHostFactory.SetContainer(container);
var container=newcontainer();
container.Options.DefaultScopedLifestyle=new wcfoOperationLifestyle();
container.Register();
container.Verify();
SimpleInjectorServiceHostFactory.SetContainer(容器);
要配置/注册AutoMapper,我们将调用一些代码在Global.asax中注册它,如下所示:

var cfg = new MapperConfigurationExpression();
cfg.CreateMap<SomeObject, SomeObjectDTO>();
Mapper.Initialize(cfg);
Mapper.Configuration.AssertConfigurationIsValid();
var cfg=new-MapperConfigurationExpression();
CreateMap();
初始化映射器(cfg);
Mapper.Configuration.AssertConfigurationsValid();
然而,当使用net.tcp端点直接调用我们的Web服务时,有时似乎不再注册AutoMapper。当直接请求WCF服务时,应用程序_Start中的Global.asax中的代码似乎从未执行过

我们目前尝试从ServiceHostFactory派生,并在重写的CreateServiceHost方法中注册AutoMapper和SimpleInjector。 然而,这确实再次给我们带来了“无参数构造函数定义”错误


您有任何解决方案或最佳做法吗?

您的配置正确吗

您可以在启动时创建
Mapper
,然后将其作为单例依赖项注入:

创建
映射器
():

注入:

public class MyClass 
{
    private readonly IMapper mapper;
    public MyClass(IMapper mapper)
    {
        this.mapper = mapper;
    }
}
我不确定这是否能解决“无参数构造函数”的问题,但这是处理AutoMapper注入的好方法。如果不能解决问题,请告诉我


如果您有任何问题,请告诉我。

使上述场景正常工作的一种方法是从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);
    }
}

感谢您的回答,我认为在需要的地方将IMapper传递给所有构造函数是有意义的。目前我们直接使用Mapper.Map(…)。也许这是一种反模式?我认为按照您的指定在SimpleInjector中初始化AutoMapper是有意义的,但要使它在上述场景中工作,似乎需要从SimpleInjectorServiceHostFactory继承。我想我会把我自己的答案记下来。是的,似乎还有更多的问题。我确实认为直接调用有点反模式,但老实说,它并不重要。我不会担心重构,除非你遇到一些集成问题。
public class MyClass 
{
    private readonly IMapper mapper;
    public MyClass(IMapper mapper)
    {
        this.mapper = mapper;
    }
}
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);
    }
}