Search code examples
c#.netweb-serviceswsdlasmx

Web Service: Cast or Convert Generated Objects


I'm running into an issue with casting a generated object from a Web service to a C# object I've created. This object is housed inside a common library that both the and server client have access to so I ideally I would like to use this instead of a generated object type.

So my question is, is it possible to do some sort of "casting" or conversion, and if so, what are the best approaches.

EDIT: The problem I'm running into is that the casting is not working. Consuming the web service is not a problem.

Code for web service:

/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public CommonLib.Models.User GetInfo()
    {
        return new CommonLib.Models.User()
        {
            Firstname = "John",
            Lastname = "Doe",
            UserID = 1,
            Timestamp = DateTime.Now

        };
    }
}

Code to consume web service:

WebService1SoapClient prox = new WebService1SoapClient();

object userInfo = prox.GetInfo();
CommonLib.Models.User two = (CommonLib.Models.User)userInfo;

User object

public class User
{
    public int UserID { get; set; }

    public string Firstname { get; set; }

    public string Lastname { get; set; }
}

Solution

  • I was able to resolve the issue by employing a WCF service and when I added a reference at the client, I simply checked the box to reuse reference assemblies like John Saunders suggested.