使用CreateMap和地图的实例版采用了WCF服务? [英] Using the instance version of CreateMap and Map with a WCF service?

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

问题描述

一直与automapper一些实际问题。我想我已经找到了如何实现它的解决方案,但不确定。

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

基本上我用用ResolveUsing和ConstructedBy自定义映射在PARAMS到构造通过,据我所知,大多数人在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)通过在不同的PARAMS到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实例的版本,地图,使每个人请愿都有自己的地图,并且可以在自己的PARAMS通过。

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申请被覆盖其他 - 因此映射的静态版本

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...

我真的AP preciat任何帮助。

I would really appreciat any help.

编辑 - 什么,我有一个例子

基本上所有的映射工作,因为它应该,因为我在大多数情况下使用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个网址取决于办公室,我有日志无处不在,我可以看到我传递在但一旦我通过它 - 它不是我传递一个,如果是有道理的,这是不可思议!

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。这就像一个会话overwritting其他...

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 overwritting 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))
            ;

FOUND在哪里失败 - 但不明所以

请参阅我的意见内嵌code。在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返回记录集(这是我SOURCE),所以我在进入每一个对有正确的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.

不过,我真的很想知道我要去的地方错了?

But I would really love to know where I am going wrong?

推荐答案

好吧,看来我的问题是被遗弃的,但很长一段时间玩耍后,我终于找到了一个很好的修复。

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

基本上我是一个解决内,我有另外的地图,属性的一个叫另一个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,做我的映射像我原来的决心里面...

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..

我用的映射静态方法做我的映射,这些人不是在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和地图的实例版采用了WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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