Calling Remote WebServices with custom objects

Have you ever tried to call a WebService and have it return a Custom Object (which both the server and the client have definitions for – e.g. in a shared project) and gotten an error message like:

  • C:\projects\…\WebServiceTests.cs(124): Cannot implicitly convert type ‘CreditSights.SalesLogix.WebService.Tests.WebService.User’ to ‘CreditSights.SalesLogix.TransportLayer.User’

Solution: Show all files, edit Reference.cs under the webservice, add “using;“, remove any created objects at the end of the definition (after the System.Web.Services.Protocols.SoapHttpClientProtocol class declaration)

Isn’t it annoying? Well, the issue is that when you create a webservice, Visual Studio.Net assumes that you don’t have any definitions of the remote objects (as would be the case when you are calling a webservice owned by someone else). So it creates proxy objects for the “custom” objects on the server. However, since you already have definitions for these objects, you probably don’t want to use their definitions. So, you just need to tell the object generated to use the shared namespace, and remove the generated objects – voila, it all works (you need to do this every time you refresh the webservice)

Caveat: Make sure you implement ISerializable, and only need the public members of the object.

Post a comment or leave a trackback: Trackback URL.


  • Mia  On August 31, 2006 at 2:48 pm

    I discovered your blog recently and have become part of your fan base 🙂
    As part of BlogDay2006, I have included a link to your blog in a post I’ve done on my fave blogs.

  • Michael  On October 26, 2006 at 2:59 am

    naive question

    Do you mean here namespace name under which local objects are defined ?

    I am trying to make work Codeproject code where one of reference.cs

    namespace SmartInstitute.Automation.SmartInstituteServices.SecurityService {
    using System.Diagnostics;
    using System.Web.Services;
    using System.ComponentModel;
    using System.Web.Services.Protocols;
    using System;
    using System.Xml.Serialization;

    Locally defined objects under namespace under namespace SmartInstitute with assembly name

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: