使用SlSvcUtil生成代码的FaultException

时间:2011-11-12 21:34:10

标签: c# web-services windows-phone-7 wsdl

我有一个带有以下wsdl文件的WebService:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions name="BBAppWebAccessService" targetNamespace="com.test" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="com.test" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <wsdl:types>
  <xs:schema elementFormDefault="unqualified" targetNamespace="com.test" version="1.0" xmlns:tns="com.test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="getPOI" type="tns:getPOI" />
  <xs:element name="getPOIResponse" type="tns:getPOIResponse" />

  <xs:complexType name="poi">
  <xs:complexContent>
  <xs:extension base="tns:poiPreview">
  <xs:sequence>
  <xs:element maxOccurs="unbounded" minOccurs="0" name="images" nillable="true" type="tns:bbImage" />
  </xs:sequence>
  </xs:extension>
  </xs:complexContent>
  </xs:complexType>
    <xs:complexType name="poiPreview">

      <xs:sequence>
        <xs:element name="poiID" type="xs:int" />
        <xs:element maxOccurs="unbounded" minOccurs="0" name="thumbsnails" nillable="true" type="tns:bbImage" />
      </xs:sequence>
    </xs:complexType>
    <xs:complexType name="bbImage">
      <xs:sequence>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="imageData" nillable="true" type="xs:byte" />
        <xs:element minOccurs="0" name="imageName" type="xs:string" />
        <xs:element name="imageID" type="xs:int" />
      </xs:sequence>
    </xs:complexType>
  <xs:complexType name="getPOI">
  <xs:sequence>
  <xs:element name="ID" type="xs:int" />
  </xs:sequence>
  </xs:complexType>
  <xs:complexType name="getPOIResponse">
  <xs:sequence>
  <xs:element minOccurs="0" name="return" type="tns:poi" />
  </xs:sequence>
  </xs:complexType>
  </xs:schema>
    </wsdl:types>
    <wsdl:message name="getPOI">
      <wsdl:part element="tns:getPOI" name="parameters">
      </wsdl:part>
    </wsdl:message>
    <wsdl:message name="getPOIResponse">
      <wsdl:part element="tns:getPOIResponse" name="parameters">
      </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="BBAppWebAccess">
      <wsdl:operation name="getPOI">
        <wsdl:input message="tns:getPOI" name="getPOI">
      </wsdl:input>
        <wsdl:output message="tns:getPOIResponse" name="getPOIResponse">
      </wsdl:output>
      </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="BBAppWebAccessServiceSoapBinding" type="tns:BBAppWebAccess">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
      <wsdl:operation name="getPOI">
        <soap:operation soapAction="" style="document" />
        <wsdl:input name="getPOI">
          <soap:body use="literal" />
        </wsdl:input>
        <wsdl:output name="getPOIResponse">
          <soap:body use="literal" />
        </wsdl:output>
      </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="BBAppWebAccessService">
      <wsdl:port binding="tns:BBAppWebAccessServiceSoapBinding" name="BBAppWebAccessPort">
        <soap:address location="http://localhost:8080/BBAppWeb/HelloWorld" />
      </wsdl:port>
    </wsdl:service>
  </wsdl:definitions>

我使用Windows Phone SDK中的SlSvcUtil生成c#SlSvcUtil.exe test.wsdl的代理类。

这是生成代码的一部分:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="getPOI", WrapperNamespace="com.test", IsWrapped=true)]
public partial class getPOI {

        [System.ServiceModel.MessageBodyMemberAttribute(Order=0)]
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int ID;

        public getPOI() {
        }

        public getPOI(int ID) {
            this.ID = ID;
        }
}

private class BBAppWebAccessClientChannel : ChannelBase<BBAppWebAccess>, BBAppWebAccess
{

    public BBAppWebAccessClientChannel(System.ServiceModel.ClientBase<BBAppWebAccess> client) : 
            base(client)
    {
    }

    public System.IAsyncResult BegingetPOI(getPOI request, System.AsyncCallback callback, object asyncState)
    {
        object[] _args = new object[1];
        _args[0] = request;
        System.IAsyncResult _result = base.BeginInvoke("getPOI", _args, callback, asyncState);
        return _result;
    }

    public getPOIResponse EndgetPOI(System.IAsyncResult result)
    {
        object[] _args = new object[0];
        getPOIResponse _result = ((getPOIResponse)(base.EndInvoke("getPOI", _args, result)));
        return _result;
    }
}

getPOIResponse _result = ((getPOIResponse)(base.EndInvoke("getPOI", _args, result))); 抛出 FaultException

我使用以下代码调用WebService:

private void Load_Initial_Data()
{
    try
    {
        BBAppWebAccessClient wac = new BBAppWebAccessClient();
        wac.getPOICompleted += new EventHandler<getPOICompletedEventArgs>(client_GetDataCompleted);
        wac.getPOIAsync(1);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.StackTrace);
    }

}
void client_GetDataCompleted(object sender, getPOICompletedEventArgs e)
{
    //this.DataContext = e.Result;
    Console.WriteLine(e.Result.poiID);
}

在服务器端有以下例外:

22:27:23,789 WARN  [org.apache.cxf.phase.PhaseInterceptorChain] Interceptor for {com.test}BBAppWebAccessService#{com.test}getPOI has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"com.test", local:"ID").
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:787) [:2.3.1-patch-01]
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:628) [:2.3.1-patch-01]
        at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:133) [:2.3.1-patch-01]
        at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:109) [:2.3.1-patch-01]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) [:2.3.1-patch-01]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113) [:2.3.1-patch-01]
        at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97) [:2.3.1-patch-01]
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461) [:2.3.1-patch-01]
        at org.jboss.wsf.stack.cxf.ServletControllerExt.invoke(ServletControllerExt.java:172) [:3.4.1.GA]
        at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:57) [:3.4.1.GA]
        at org.jboss.wsf.stack.cxf.transport.ServletHelper.callRequestHandler(ServletHelper.java:156) [:3.4.1.GA]
        at org.jboss.wsf.stack.cxf.CXFNonSpringServletExt.invoke(CXFNonSpringServletExt.java:90) [:3.4.1.GA]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179) [:2.3.1-patch-01]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103) [:2.3.1-patch-01]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [:1.0.0.Final]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159) [:2.3.1-patch-01]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.1.0.Final]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
        at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
        at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]
        at java.lang.Thread.run(Unknown Source) [:1.7.0]
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 197; unexpected element (uri:"com.test", local:"ID"). Expected elements are <{}ID>]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:425) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339) [:2.2]
        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:764) [:2.3.1-patch-01]
        ... 34 more
Caused by: com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 197; unexpected element (uri:"com.test", local:"ID"). Expected elements are <{}ID>
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:101) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:243) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:242) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176) [:2.2]
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360) [:2.2]
        ... 36 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"com.test", local:"ID"). Expected elements are <{}ID>
        ... 47 more

ClientConfig:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BBAppWebAccessServiceSoapBinding" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/BBAppWeb/HelloWorld"
                binding="basicHttpBinding" bindingConfiguration="BBAppWebAccessServiceSoapBinding"
                contract="SilverlightApplication1.ServiceReference1.BBAppWebAccess"
                name="BBAppWebAccessPort" />
        </client>
    </system.serviceModel>
</configuration>

使用Fiddler(Windows Phone)捕获的消息:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <getPOI xmlns="com.test">
            <ID>1</ID>
        </getPOI><
    /s:Body>
</s:Envelope>

使用Fiddler(Axis2)捕获的消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns1:getPOI xmlns:ns1="com.test">
            <ID>1</ID>
        </ns1:getPOI>
    </soapenv:Body>
</soapenv:Envelope>

我该如何解决?生成的java代码(Apache Axis2)效果很好。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:2)

我找到了解决方案。使用Java EE创建WebService时,必须添加所有名称空间,否则将出现上述错误 在修复 FaultException 之后,我得到了一个 NullReferenceException ,这表明我也必须修复响应处理。

非常感谢 @calum

以下是WebService的示例实现,它与Axis2和SLsvcUtil工具一起使用:

package com.test;

//imports here

@WebService(targetNamespace = "com.test")
public class BBAppWebAccess {
    @WebMethod
    @WebResult(targetNamespace="com.test.poi")
    public POI getPOI(
            @WebParam(name="ID", targetNamespace="com.test.poi")
            int id) {

        return new POI(id);
    }
}

新消息如下所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <getPOI xmlns="com.test">
            <ID xmlns="com.test.poi">1</ID>
        </getPOI>
    </s:Body>
</s:Envelope>

新生成的 getPOI 类:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="getPOI", WrapperNamespace="com.test", IsWrapped=true)]
public partial class getPOI {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="com.test.poi", Order=0)]
    public int ID;

    public getPOI() {
    }

    public getPOI(int ID) {
        this.ID = ID;
    }
}

答案 1 :(得分:1)

看起来您正在向服务发送错误的类型(我假设这是因为错误表明发现了意外的元素'ID',这意味着您的请求未正确形成)。 使用fiddler(如果可能)查看您要发送到服务器的内容。您可能能够弄清楚发生了什么,然后您可以更新生成的代码,以便发送正确的类型。