Unable to cast object from webservice
-
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)
-
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)
Web services are not OO friendly, in fact outlining why is a classic interview question. The
Book
class in theLibraySorter
is not the same type as theBook
class in theLibrarySearch
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
-
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)
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
-
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)
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.
-
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)
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)
-
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)
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[^]