How to use a custom object in a web service parameter

Posted on 2007-08-09
Last Modified: 2013-11-07
I have a visual studio solution with a web service project and a project that is a client for that service.  There is a third project which is my Data Access Layer for this solution.  In the DAL i have a custom struct that holds the info i want to send to my web service and a function that queries the database to create an array of these objects.
My Problem:
When i use the web service in the client i call the DAL function to get the array of objects i want to pass as parameters in my web service function call.  It does not work to pass the objects as they are returned from the DAL function to the web service function (even though the web service is set up to take the same struct defined in the DAL).  The web service function expects to see a type of MyWebservice.MyStruct and what is returned from the DAL function is DAL.MyStruct.  These types do not implicitly cast to eachother (even though they hold the same data).  So i have to create a new instance of MyWebservice.MyStruct and fill it with all the values in the DAL.MyStruct that is returned from DAL function.
The best solution would be one which allowed me to pass the value coming back from the DAL function to the web service function directly.
Question by:dave4dl
    LVL 1

    Expert Comment

    Use Casting like so.
    //Cast your class into an object and pass to foo;
    //Webmethod target
    public void foo(object SentTestClass)
       TestClass TestClass1 = (TestClass)SentTestClass;
       //TestClass1 now contains the object you passed.
    LVL 15

    Author Comment

    Thanks for the idea lordHaze.

    When i did what you said it gave me the following error:

    NUnit_Tests.clsUnitTests.myTest: System.InvalidOperationException : There was an error generating the XML document.
      ----> System.InvalidOperationException : The type Web_Service_DAL.clsTableRecord[] may not be used in this context.

    I am trying to pass an array of custom objects to the web service like:
    objWebService.insertIntoTable((object)testRecords, "MyTable");

    I defined the custom classes (the clsTableRecord contains, among other things, a custom object) as [Serializable]

    Any light you can shed on how to do what i am trying to do would be much appreciated
    LVL 1

    Accepted Solution

    Try converting  to an ArrayList before passing, could have something to do with the Serializable Object.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now