How to get a webserice to serialize/deserialize th

2019-07-24 21:14发布

问题:

I want to use typed parameters in the web methods. But when visual studio 2005 creates the web reference on for the client it automatically creates it's own types. I am using a .net web service and a .net client.

For example:

< WebMethod > _ Public Function Foo() as ServerNamespace.Bar ... End Function

at the client the method becomes WebServiceInstance.Foo() as ClientNamespace.Webservice.Bar

returning a different type.

I started writing my own serialization helper class to convert the type, but I would have thought there would be a better way....

回答1:

If you can upgrade to .Net 3.0, I would suggest switching to WCF for your client proxy. Svcutil.exe is the tool to generate a WCF client proxy from the web service; it has an option /reference, which takes an assembly and reuses any shared types from it. (I would also suggest switchnig the service to WCF if it's under your control :-))

If you can't upgrade, wsdl.exe has an option /sharetypes which might help you, although I am not sure how to do it exactly.



回答2:

I've asked the same question before, so the answers I got might be of help:

Force .NET webservice to use local object class, not proxy class



回答3:

There is also a good answer here: .NET SOAP Common types

Basically you are not supposed to share the data structures between the server and the client. But if you really want to there seam to be possibilities.



回答4:

I have upgraded to VS 2008, and WCF. I still find the solution to be half baked. Shared types seem to work ok as long as they are not typed collections, which get converted to and array or List(Of T).

I really don't understand what is so hard about serializing to and from the same object type, collection or otherwise.

A number of people reference this article from Code Project (article)

Which works for some objects, but for my data objects generates the following warnings (if I force it to use the DataContractSerializer):

Warning: The optional WSDL extension element 'body' from namespace 'http://schem as.xmlsoap.org/wsdl/soap12/' was not handled. XPath: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:binding[@ name='WSHttpBinding_IService']/wsdl:operation[@name='GetData']/wsdl:output

I could not find anything meaningful that would help me to debug this.