问题
I wrote a few SOAP Webservices in Java, running on a JBoss 5.1. Two of them share a class, AddressTO. The Webservices are deploying correctly on my ApplycationServer and all went well until I try to use the class addressTO in my C#-client. There are two types in the client application, addressTO and addressTO1. This is a problem because this causes errors like:
Fehler 1 Eine implizite Konvertierung vom Typ
"acsysteme.i4workspace.client.webservices.addressTO1[]" in
"acsysteme.i4workspace.client.webservices.addressTO[]" ist nicht möglich.
[...]
This means that it is impossible to cast the to types implicitly. AddressTo is something like a core class which can be used by other webservices.
The webreferences for the C#-client are created by the command
wsdl.exe /parameters:CreateWebService.xml
The xml-file contains the urls to the differend .wsdl-files of my webservices.
Does someone know how to handle this problem?
回答1:
Use the /sharetypes
option when calling wsdl.exe
:
/sharetypes Turns on type sharing feature. This feature creates one code file with a single type definition for identical types shared between different services (namespace, name and wire signature must be identical). Reference the services with http:// URLs as command-line parameters or create a discomap document for local files.
If the classes match exactly, they should only be generated once if you generate code for both services in a single command. Both services will be using the same class, so no conversion will be necessary.
Edit:
If the XML namespaces do not match (which is a common occurrence), .NET will consider them to be different types, and rightly so. You will either have to fix the web services so the types are exactly the same (recommended), or do conversion between the two generated types. This will result in a lot of boring property assignment code, so you might want to consider using something like AutoMapper to handle the conversion for you.
wsdl.exe should generate partial classes, so if you want, you can define implicit conversions between the different types:
public static implicit operator addressTO1(addressTO source)
{
addressTO1 result = new addressTO1();
// Assign properties, etc.
return result;
}
I'm not usually a big fan of implicit conversions myself, but in this case it might be warranted.
回答2:
I solved it!
I followed the hint of Thorarin to use the wsdl.exe
option sharetypes
. But to use this option is not enougth. First, you need to setup the correct namespace (using a URI) in the Webservice class in your java server with the following annotation:
@WebService(targetNamespace="http://com/project/client/webservices/")
public class WebServiceImplementation implements WebService{
// ... your @WebMethod-methods
}
Second, you need to modify the settings in createWebService.xml
accordingly: The namespace of the webservice needs to be added like this:
<wsdlParameters xmlns="http://microsoft.com/webReference/">
<!-- Defaultsettings -->
<language>CS</language>
<sharetypes>true</sharetypes>
<namespace>com.project.client.webservices</namespace>
<!-- output -->
<out>soap/WebServices.cs</out>
<appSettingUrlKey>BaseUrl</appSettingUrlKey>
<appSettingBaseUrl>http://localhost:8080</appSettingBaseUrl>
<!-- web service locations -->
<documents>
<document>http://localhost:8080/Core?wsdl</document>
<document>http://localhost:8080/WebService0?wsdl</document>
<document>http://localhost:8080/WebService1?wsdl</document>
</documents>
</wsdlParameters>
That's it! Call wsdl.exe /parameters:createWebService.xml
and you are done.
Thanks for your help!
来源:https://stackoverflow.com/questions/6029300/webreferences-sharing-classes