Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unable to cast object from webservice

Unable to cast object from webservice

Scheduled Pinned Locked Moved C#
helpwcftutoriallearning
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    il_manti
    wrote on last edited by
    #1

    Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

    In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

    K P OriginalGriffO I B 5 Replies Last reply
    0
    • I il_manti

      Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

      In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Web services are not OO friendly, in fact outlining why is a classic interview question. The Book class in the LibraySorter is not the same type as the Book class in the LibrarySearch as far as the proxies are concerned (even if the services use a common class from a dll for example). The generated proxy classes generated have different namespaces for one thing. One solution is to take the pain: Client model<--->client/proxy translator<--->proxy classes<--->service <--->service OM translator<--->OM The translators aren't too bad, normally. [Edit fixed spelling, also, see Pete's reply]

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]

      modified on Wednesday, November 3, 2010 8:36 AM

      1 Reply Last reply
      0
      • I il_manti

        Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

        In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        The problem is, as far as your code is concerned - these are two separate and discrete objects with no commonality, so you can't cast between them. This is because they are from two separate discrete sources - this is to do with the way that items are created when you import their definitions in - each reference has it's own physical implementation of the class.

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        • I il_manti

          Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

          In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          If you can change them both, then the best thing to do is to set up a separate assembly (LibraryUtilities or similar) which contains your Book class. Both services then refer to that. Other than that, it's a case of manual conversion if you want to be safe - i.e. create a new LibrarySearch.Book from your instance of a LibrarySorter.Book and vice versa. They are treated as different classes because one of them could be changed without the other altering. Just casting (or serialize / deserialise) won't work because the compiler can't know they are the same: it sees the names are different and knows they could be different in the future.

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • I il_manti

            Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

            In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

            I Offline
            I Offline
            il_manti
            wrote on last edited by
            #5

            What I ended up doing is serializing from one type to a file, then de-serializing that file to the other type. Messy, but worked. I'll try to see if I can serialize to a memory stream instead.

            In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

            1 Reply Last reply
            0
            • I il_manti

              Hi guys, I have a huge problem and I have no idea how to fix it. I have two different web services - one we'll call LibrarySearch and one we'll call LibrarySorter. These are two different projects. Both have a class called Book and it's the same exact class. But when I call each of them they return as LibrarySearch.Book and LibrarySorter.Book ... and I have no way of converting these to some generic item which I can use (without having to create two objects, one for each result depending on whether the user did a search or a sort). Please help. I even tried to serialize to a stream, and de-serialize back and it still didn't work. Thank you for your time.

              In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              Actually, your problem is the same as the one described in the post "Is there any technique available for copying one object of class A to another object of class B?". Also in your case, you can use my trick with reflection safely, see: http://www.codeproject.com/Messages/3556901/Re-Is-there-any-technique-available-for-copying-on.aspx[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups