.NET WCF服务中的Automapper 8.0问题 [英] Problem with Automapper 8.0 in a .NET WCF Service

查看:76
本文介绍了.NET WCF服务中的Automapper 8.0问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将WCF Service项目中的Automapper升级到了最新版本8.0.0.通过WCF测试客户端调用服务时出现以下错误:

I have just upgraded Automapper in my WCF Service project to the newest version 8.0.0. I get following error when calling the service via WCF Test client:

System.InvalidOperationException:'映射器已经初始化.您必须为每个应用程序域/进程调用一次Initialize."

System.InvalidOperationException: 'Mapper already initialized. You must call Initialize once per application domain/process.'

使用旧的Automapper版本6.0.2可以正常工作,并且不会引发异常.我用单独的服务行为类初始化了Mapper.

With the old Automapper version 6.0.2 it's working and no exception is thrown. I initialzed the Mapper with a seperate Service Behaviour class.

服务类别 EdiPartners.cs :

[AutomapServiceBehavior]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = 
InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class EdiPartners : IPartners
{
    [FaultContract(typeof(ExceptionObject))]
    public Supplier GetPartnerData(string SupplierNumber)
    {
       // Code
    }
}

服务行为类 AutomapServiceBehavior :

    public sealed class AutomapServiceBehavior : Attribute, IServiceBehavior
    {
    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, 
    ServiceHostBase serviceHostBase,
    Collection<ServiceEndpoint> endpoints, BindingParameterCollection 
    bindingParameters)
    {
        AutomapBootstrap.InitializeMap();
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    #endregion
}

静态自动映射器初始化类 AutomapBootstrap :

Static Automapper Initializing Class AutomapBootstrap:

   public class AutomapBootstrap
{
    public static void InitializeMap()
    {
        Mapper.Initialize(config =>
        {
            config.CreateMap<Supplier, IEdiPartnerData>();
            config.CreateMap<IEdiPartnerData, Supplier>();
            config.CreateMap<IEdiPartner, EdiPartner>();

        });
    }
}

也许你们可以帮助我.我不明白为什么它可以与旧版本一起使用,但不能与新版本一起使用.有没有更好的方法可以在WCF服务中初始化Automapper?

Maybe you guys can help me. I don't understand why it is working with the old but not with the new version. Is there a better way for initializing Automapper in a WCF Service?

提前,桑德罗

推荐答案

解决方案是实现自定义服务主机工厂类:

The solution is to implement a custom service host factory class:

public class EdiPartnersFactory : ServiceHostFactory
{
   protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
   {
    // Initialize Automapper
    AutomapBootstrap.InitializeMap();

    var host = new ServiceHost(serviceType, baseAddresses);

    return host;
    }
}

然后我更改了服务合同和绑定的web.config:

And I changed the web.config for the service contract and the binding:

    <system.serviceModel>
  <services>
    <service name="Company.Sap.EdiPartners"
             behaviorConfiguration="MEXServiceTypeBehavior">
      <endpoint address=""
                binding="basicHttpBinding"
                contract="Company.Sap.IPartners" />
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MEXServiceTypeBehavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

更改.svc文件:

<%@ ServiceHost Language="C#" Debug="true" Service="Company.Sap.EdiPartners" Factory="Company.Sap.EdiPartnersFactory" CodeBehind="EdiPartners.svc.cs" %>

因此,在服务类中不使用服务绑定(看起来更加简洁):

So no use of a service binding in the service class (looks much more cleaner):

public class EdiPartners : IPartners
{
    [FaultContract(typeof(ExceptionObject))]
    public Supplier GetPartnerData(string SupplierNumber)
    {
       // Code
    }
}

这篇关于.NET WCF服务中的Automapper 8.0问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆