Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the instance version of CreateMap and Map with a WCF service?
    text
    copied!<p>Been having some real issues with automapper. I think I have found the solution but unsure of how to implement it.</p> <p>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.</p> <p>But the problem is that my method (on a wcf) passes in different params to the constructor of a ResolveUsing ...... </p> <p>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.</p> <p>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.</p> <p>But I can't seem to find how to do it. Can anyone explain please? I am really stuck...</p> <p>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.</p> <p>Well I hope I am correct, but I can't find anything else...</p> <p><strong>EDITED - AN EXAMPLE OF WHAT I HAVE</strong></p> <p>Basically all mapping is working as it should, as I am using MapFrom in most cases.</p> <p>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.</p> <p>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!! </p> <p>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...</p> <p>I hope this makes sense.</p> <pre><code> SalesPointResolver newSalesPointResolver = new SalesPointResolver(returnReservationUrl, reservationSite.ReservationUrl, startDate, endDate, officeCode); Mapper.CreateMap&lt;Models.Custom.House, DTO.House&gt;() .ForMember(dest =&gt; dest.Id, opt =&gt; opt.MapFrom(src =&gt; src.Id)) .ForMember(dest =&gt; dest.TaxIncluded, opt =&gt; opt.MapFrom(src =&gt; src.Segments.FirstOrDefault().TaxIncluded)) .ForMember(dest =&gt; dest.TaxPercentage, opt =&gt; opt.MapFrom(src =&gt; src.Segments.FirstOrDefault().TaxPercentage)) .ForMember(dest =&gt; dest.SalesPoints, opt =&gt; opt.ResolveUsing(newSalesPointResolver)) ; </code></pre> <p><strong>FOUND OUT WHERE IS FAILING - BUT UNKNOWN WHY</strong></p> <p>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 :-)</p> <p>I have placed some log4net logs on there, so I can see whats happening.</p> <pre><code>[Log] public class SalesPointResolver : ValueResolver&lt;Models.Custom.House, IList&lt;DTO.SalesPoint&gt;&gt; { 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&lt;DTO.SalesPoint&gt; ResolveCore(House source) { this.house = source; log.Error("in resolveCore :" + this.urlTemplate); // THIS IS RETURNING THE WRONG VALUE </code></pre> <p><strong>TEMPORARY SOLUTION</strong></p> <p>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.</p> <p>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.</p> <p>Of course, this really is patch and not ideal but it gets me running.</p> <p>Where am I going wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload