将 CreateMap 和 Map 的实例版本与 WCF 服务一起使用? [英] Using the instance version of CreateMap and Map with a WCF service?

查看:26
本文介绍了将 CreateMap 和 Map 的实例版本与 WCF 服务一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用自动映射器时遇到了一些实际问题.我想我已经找到了解决方案,但不确定如何实施.

Been having some real issues with automapper. I think I have found the solution but unsure of how to implement it.

基本上,我使用带有 ResolveUsing 和 ConstructedBy 的自定义映射将参数传递给构造函数,我知道大多数人在 global.asax 中设置过一次就忘记了.

basically I am using a custom mapping with ResolveUsing and ConstructedBy to pass in params to the constructor, I understand that most people set this up in the global.asax once and forget about it.

但问题是我的方法(在 wcf 上)将不同的参数传递给 ResolveUsing 的构造函数......

But the problem is that my method (on a wcf) passes in different params to the constructor of a ResolveUsing ......

在我使用静态方法 Mapper.CreateMap 和 Mapper.Map 之前,当不同的请求通过方法(多用户)进入 wcf 服务时,它们似乎相互冲突.

Before I was using the Mapper.CreateMap and Mapper.Map which are static methods and it appears that when different petitions come into the wcf service via methods (multi -user) they are conflicting with each other.

在阅读一些看起来似乎可以使用实例版本的 CreateMap 和 Map 以便每个单独的请求获得自己的地图并可以传递自己的参数.

After reading something it appears I can use the instance version of CreateMap and Map so that each individual petition gets its own map and can pass in its own params.

但我似乎无法找到如何去做.谁能解释一下?我真的卡住了...

But I can't seem to find how to do it. Can anyone explain please? I am really stuck...

以前我会一次又一次地收到重复的键错误,并且我还在构造函数上添加了日志跟踪,看来 1 个请求正在覆盖另一个 - 因此是 Mapper 的静态版本.

Before now and again I would get duplicate key errors and also I put in a log trace on the constructor and it appears that 1 petition is overwriting the other - hence the static versions of Mapper.

好吧,我希望我是对的,但我找不到其他任何东西...

Well I hope I am correct, but I can't find anything else...

已编辑 - 我所拥有的示例

基本上所有映射都正常工作,因为我在大多数情况下都使用 MapFrom.

Basically all mapping is working as it should, as I am using MapFrom in most cases.

然后我创建我的解析器的一个实例,并传入一个 URL.我在传入之前检查了 url 并且它是正确的.但是一旦它返回,它就会返回错误的 URL.

Then I create an instance of my Resolver which I pass in a URL. I have checked the url before I pass it in and its correct. But once it returns it returns the wrong URL.

我需要传入 URL 的原因是它在那里有变量,所以我需要替换这些变量......基本上有 2 个 url,具体取决于办公室,我到处都有日志,我可以看到我正在传递的内容但是一旦我传入它-它不是我传入的那个,如果这是有道理的,这很奇怪!!

The reason I need pass in the URL is that it has variables in there so I need to replaced the variables... Basically there are 2 urls depending on the office and I have logs everywhere and I can see what I am passing in but once I pass it in - it isn't the one I passed in, if that makes sense, this is weird!!

它是一个 WCF 服务,一个客户端调用了该方法两次,传入了 2 个不同的办公室,因此有 2 个不同的 URL.但它们总是返回相同的 URL.就像一个会话正在覆盖另一个...

Its a WCF service and a client has called the method twice passing in 2 different offices hence 2 different URLs. But they always return the same URL. It's like one session is overwriting the other...

我希望这是有道理的.

  SalesPointResolver newSalesPointResolver = new SalesPointResolver(returnReservationUrl, reservationSite.ReservationUrl, startDate, endDate, officeCode);


        Mapper.CreateMap<Models.Custom.House, DTO.House>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.TaxIncluded,
                       opt => opt.MapFrom(src => src.Segments.FirstOrDefault().TaxIncluded))
            .ForMember(dest => dest.TaxPercentage,
                       opt => opt.MapFrom(src => src.Segments.FirstOrDefault().TaxPercentage))

            .ForMember(dest => dest.SalesPoints,
                       opt =>
                       opt.ResolveUsing(newSalesPointResolver))
            ;

找出失败的地方 - 但不知道为什么

在代码中查看我的评论.在 urlTemplate 到达的构造函数中,我将它保存在一个私有变量中,然后在覆盖的 ResolveCore 中它是别的东西:-)

See my comments inline with code. In the constructor the urlTemplate arrives, I save it in a private var and then in the overridden ResolveCore it's something else :-)

我在那里放置了一些 log4net 日志,所以我可以看到发生了什么.

I have placed some log4net logs on there, so I can see whats happening.

[Log]
public class SalesPointResolver : ValueResolver<Models.Custom.House, IList<DTO.SalesPoint>>
{
    private readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    private string urlTemplate;

    public SalesPointResolver (bool returnReservationUrl, string urlTemplate, DateTime startDate, DateTime endDate, string officeCode)
    {
        this.urlTemplate = urlTemplate;

        log.Error("passed in " + urlTemplate); // THIS IS PERFECT
        log.Error("I am now " + this.urlTemplate); // THIS IS PERFECT
    }

    protected override IList<DTO.SalesPoint> ResolveCore(House source)
    {
        this.house = source;

        log.Error("in  resolveCore :" + this.urlTemplate); // THIS IS RETURNING THE WRONG VALUE

临时解决方案

我已经做了一个临时解决方案,但它真的很糟糕.我确信 automapper 可以做我正在尝试的事情,但我显然做错了什么.

I have done a temporary solution but it's really bad. I am sure automapper can do what I am trying, but I am obviously doing something wrong.

基本上,我通过 LINQ 返回了一组记录(这是我的来源),因此我在每条记录上都输入了一个新字段,该字段上有正确的 URL 模板.然后,我没有(通过构造函数)传入 url 模板,而是将其作为集合(源)中每条记录的一个属性使用......并且它完美无缺.

Basically I return via LINQ a collection of records (THIS IS MY SOURCE) so I entered a new field on every record that has the correct URL template on there. And then, instead of passing in (via constructor) the url template, I have it available as a property on EVERY record on the collection (THE SOURCE) ... and it works perfect.

当然,这确实是补丁,并不理想,但它让我运行起来.

Of course, this really is patch and not ideal but it gets me running.

我哪里出错了?

推荐答案

好吧,我的问题似乎被放弃了,但经过一段时间的尝试后,我终于找到了一个很好的解决方案..

Well it appears that my question is abandoned but after quite a while playing around I finally found a GOOD fix..

基本上我在一个 Resolve 里面,我有另一个 MAP,其中一个属性称为另一个 ResolveUsing ...

basically i was inside a Resolve and i had another MAP which one of the properties called another ResolveUsing ...

这似乎有问题.另一个奇怪的事情是每次启动或回收应用程序池时它都会失败..因此它第一次失败,然后在回收发生之前没问题(我正在使用 wcf 应用程序).

It appears there seems to be an issue with this. Another weird thing is that it failed everytime the application pool was started or recycled.. Hence it failed the first time and then was ok until the recycle happened (i am using a wcf app).

所以我用 foreach 替换了第二个 Mapping 并在我原来的 Resolve 中做了我的映射......

So i replaced the second Mapping with with a foreach and did my mapping like that inside my original Resolve ...

我已经把答案放在这里,以防将来它可以帮助其他人..

I have put the answer here in case it can help anybody else in the future..

我使用 Mapper 静态方法进行映射,这些不在 global.asax 中,因为我需要根据某些因素传递不同的东西..

I was using the Mapper static methods to do my mappings, these were not in global.asax as i need to pass different things depending on certain factors..

我一直想知道是否可以使用 mappper 的实例版本来做到这一点,我虽然它存在......但从未发现......

I always wondered if it would be possible to do it with Instance versions of mappper, i though it existed..... but never found out..

但无论如何,现在一切都在 100% 工作......

But anyway all is working 100% now...

这篇关于将 CreateMap 和 Map 的实例版本与 WCF 服务一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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