.NET Web Service hydrate custom class

Posted by row1 on Stack Overflow See other posts from Stack Overflow or by row1
Published on 2010-03-12T07:48:45Z Indexed on 2010/03/12 8:17 UTC
Read the original article Hit count: 300

Filed under:
|
|

I am consuming an external C# Web Service method which returns a simple calculation result object like this:

[Serializable]
public class CalculationResult
{
    public string Name { get; set; }
    public string Unit { get; set; }
    public decimal? Value { get; set; }
}

When I add a Web Reference to this service in my ASP .NET project Visual Studio is kind enough to generate a matching class so I can easily consume and work with it.

I am using Castle Windsor and I may want to plug in other method of getting a calculation result object, so I want a common class CalculationResult (or ICalculationResult) in my solution which all my objects can work with, this will always match the object returned from the external Web Service 1:1.

Is there anyway I can tell my Web Service client to hydrate a particular class instead of its generated one? I would rather not do it manually:

foreach(var fromService in calcuationResultsFromService)
{
    ICalculationResult calculationResult = new CalculationResult()
    {
       Name = fromService.Name
    };
    yield return calculationResult;
}

Edit: I am happy to use a Service Reference type instead of the older Web Reference.

© Stack Overflow or respective owner

Related posts about c#

Related posts about web