Using the same class in both server and web service
I have classes that are needed in both my web service and my server.
For example, I have a class named Order
that I'd like to send from my server to the web service and vice-versa.
Problem is that the class in the server is Order
and the one on the web service is localhost.Order
, and it is impossible to convert between them, even though they are built f开发者_高级运维rom the very same code. Error is cannot convert from 'Order[]' to 'localhost.Order[]'
.
What can I do? Thank you very much.
when you add reference to web service you can specify which classes to reuse. by default it generates classes based on WDSL that web service produce.
The namespace used is determined by the name you give the reference when you add it.
For more information see this answer to a similar question:
Unable to cast object of type MyObject to type MyObject
You should maybe have a look at WCF services:
http://msdn.microsoft.com/en-us/library/bb332338.aspx
I've used these on a few projects where both have references to a shared library, and one web site will request one of these objects via a WCF service call from another site. It's very clean, and it opens up other options for transport/security which can be very useful.
The question appears to suggest that you are using ASMX Web Services. If so, you have your work cut out for you.
Jelle Druyts wrote an extension for ASMX that can do, more or less, what you're asking. You have to configure your shared types at the machine level (machine.config). It's not pretty.
There's also a fix for that extension to make it work with nullable types.
Good luck getting this to work with Visual Studio 2008 or on Vista/Windows 7. You'll be OK if you're still running XP with VS 2005.
If you can, you really should consider using WCF for the client proxy instead, since WCF makes it very easy to share types. In fact, the Visual Studio 2008 integration does this by default; you just need to make sure your client project references the assembly containing the service types.
精彩评论