未识别SOAP指定的类型

时间:2014-04-15 14:12:45

标签: c# .net web-services soap wsdl

我正在尝试使用带有VS2013 + .net 4.5的SOAP Web服务但是我在上面调用getVechicles方法时遇到异常,我找不到这个异常的原因。

我发现以下几点:

  • 在异常结束时,我看到了一个可怕的事情,方法Read6_getVehiclesResponse正在调用Read3_Animal方法,而它应该尝试阅读Vehicle
  • 如果我删除AnimalDog类型,一切正常。

例外:

System.ServiceModel.CommunicationException was unhandled
  HResult=-2146233087
  Message=Error in deserializing body of reply message for operation 'getVehicles'.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
       at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
       at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
       at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
       at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at WindowsFormsApplication4.ServiceReference.TestWebService.getVehicles(getVehicles request)
       at WindowsFormsApplication4.ServiceReference.TestWebServiceClient.WindowsFormsApplication4.ServiceReference.TestWebService.getVehicles(getVehicles request) in c:\Users\tiago\Documents\Visual Studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Service References\ServiceReference\Reference.cs:line 257
       at WindowsFormsApplication4.ServiceReference.TestWebServiceClient.getVehicles() in c:\Users\tiago\Documents\Visual Studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Service References\ServiceReference\Reference.cs:line 262
       at WindowsFormsApplication4.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\tiago\Documents\Visual Studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs:line 26
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication4.Program.Main() in c:\Users\tiago\Documents\Visual Studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=There is an error in XML document (1, 273).
       Source=System.Xml
       StackTrace:
            at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
            at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
       InnerException: System.InvalidOperationException
            HResult=-2146233079
            Message=The specified type was not recognized: name='Car', namespace='http://ws.kontrol.aiko.com/', at <item xmlns=''>.
            Source=Microsoft.GeneratedCode
            StackTrace:
                 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTestWebService.Read3_Animal(Boolean isNullable, Boolean checkType)
                 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTestWebService.Read6_getVehiclesResponse()
                 at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Deserialize(XmlSerializationReader reader)
                 at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
            InnerException: 

Soap WSDL:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.kontrol.aiko.com/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestWebServiceService" targetNamespace="http://ws.kontrol.aiko.com/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.kontrol.aiko.com/" targetNamespace="http://ws.kontrol.aiko.com/" version="1.0">

  <xs:complexType name="Animal">
    <xs:sequence/>
    <xs:attribute name="AnimalProp" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="Dog">
    <xs:complexContent>
      <xs:extension base="tns:Animal">
        <xs:sequence/>
        <xs:attribute name="DogProp" type="xs:string"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="Vehicle">
    <xs:sequence/>
    <xs:attribute name="VechicleProp" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="Car">
    <xs:complexContent>
      <xs:extension base="tns:Vehicle">
        <xs:sequence/>
        <xs:attribute name="CarProp" type="xs:string"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType final="#all" name="AnimalArray">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="item" nillable="true" type="tns:Animal"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType final="#all" name="VehicleArray">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="item" nillable="true" type="tns:Vehicle"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="getVehiclesResponse">
    <wsdl:part name="return" type="tns:VehicleArray">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getAnimals">
  </wsdl:message>
  <wsdl:message name="getAnimalsResponse">
    <wsdl:part name="return" type="tns:AnimalArray">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getVehicles">
  </wsdl:message>
  <wsdl:portType name="TestWebService">
    <wsdl:operation name="getAnimals">
      <wsdl:input message="tns:getAnimals" name="getAnimals">
    </wsdl:input>
      <wsdl:output message="tns:getAnimalsResponse" name="getAnimalsResponse">
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getVehicles">
      <wsdl:input message="tns:getVehicles" name="getVehicles">
    </wsdl:input>
      <wsdl:output message="tns:getVehiclesResponse" name="getVehiclesResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="TestWebServiceServiceSoapBinding" type="tns:TestWebService">
    <soap12:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getAnimals">
      <soap12:operation soapAction="" style="rpc"/>
      <wsdl:input name="getAnimals">
        <soap12:body namespace="http://ws.kontrol.aiko.com/" use="literal"/>
      </wsdl:input>
      <wsdl:output name="getAnimalsResponse">
        <soap12:body namespace="http://ws.kontrol.aiko.com/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getVehicles">
      <soap12:operation soapAction="" style="rpc"/>
      <wsdl:input name="getVehicles">
        <soap12:body namespace="http://ws.kontrol.aiko.com/" use="literal"/>
      </wsdl:input>
      <wsdl:output name="getVehiclesResponse">
        <soap12:body namespace="http://ws.kontrol.aiko.com/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestWebServiceService">
    <wsdl:port binding="tns:TestWebServiceServiceSoapBinding" name="TestWebServicePort">
      <soap12:address location="http://localhost:8080/app/TestWebService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

getVehicles回复:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns1:getVehiclesResponse xmlns:ns1="http://ws.kontrol.aiko.com/">
            <return>
                <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://ws.kontrol.aiko.com/" xsi:type="ns3:Car"/>
            </return>
        </ns1:getVehiclesResponse>
    </soap:Body>
</soap:Envelope>

自动生成C#代码: https://gist.github.com/anonymous/ae9a42fb146b159a4ccd

App.config: https://gist.github.com/anonymous/e5a311bd0e779f592ac0

测试代码:

ServiceReference.TestWebServiceClient client = new ServiceReference.TestWebServiceClient();
Animal[] sensors = client.getAnimals();
Vehicle[] vehicles = client.getVehicles(); //THIS LINE THROWS THE EXCEPTION

服务参考设置: http://i.stack.imgur.com/vuOMi.png

1 个答案:

答案 0 :(得分:0)

查看this链接我找到了解决方案,在服务器端(JAX-WS,Wildfly 8)我使用的是WSDL RPC/Literal样式,更改为Document/Literal解决了问题

原始代码:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class TestWebService {

    @WebMethod
    public List<Vehicle> getVehicles() {
        return Arrays.asList(new Car() {{ setCarProp("CARP"); setVehicleProp("VEHP");}});
    }

    @WebMethod
    public List<Animal> getAnimals() {
        return Arrays.asList(new Dog());
    }
}

<强>修改:

@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT) //CHANGE FROM RPC TO DOCUMENT
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class TestWebService {
...

有关WSDL样式的更多信息:http://www.ibm.com/developerworks/library/ws-whichwsdl/