ServiceStack:SerializationException

时间:2013-04-26 19:57:05

标签: servicestack

我正在使用SoapUI测试使用ServiceStack创建的api,当我尝试通过标头发送所需的DataMember时,api返回正确的值。当我尝试通过Body发送所需的DataMember时,我收到以下错误...请帮助

通过正文发送的请求

<GetProductDetailsReq>
<AToken>ck0b0YYBPkrnVF/j6e16DUPzxLX2SMCXewoR4T</AToken>
</GetProductDetailsReq>

POST http://localhost/ServiceStackAPI/GetProductDetails HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/xml
Accept: application/xml
Content-Length: 777
Host: localhost
Proxy-Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

响应状态

  

错误代码       SerializationException消息       无法使用ServiceModel.DTO.GetProductDetailsReq'反序列化'application / xml'请求'错误:   System.Runtime.Serialization.SerializationException:第1行中的错误   位置66.期望来自命名空间的元素'GetProductDetailsReq'   ''..遇到'Element',名称为'GetProductDetailsReq',名称空间   'http://schemas.servicestack.net/types'。在   System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator   xmlReader,Boolean verifyObjectName,DataContractResolver   dataContractResolver)at   System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator   reader,Boolean verifyObjectName,DataContractResolver   dataContractResolver)at   System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader   读者)   System.Runtime.Serialization.XmlObjectSerializer.ReadObject(流   stream)在ServiceStack.Text.XmlSerializer.DeserializeFromStream(Type   类型,流流)   ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.CreateContentTypeRequest(IHttpRequest   httpReq,Type requestType,String contentType)堆栈跟踪       at ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.CreateContentTypeRequest(IHttpRequest)   httpReq,类型requestType,String contentType)at   ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest   httpReq,IRestPath restPath)at   ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest   httpReq,IHttpResponse httpRes,String operationName)

1 个答案:

答案 0 :(得分:0)

检查SOAP Limitations以确保您创建可以在SOAP中发送的服务,即如果请求DTO被调用GetProductDetails,则它必须返回名为GetProductDetailsResponse的响应DTO。此外,您需要确保您的DTO对所有类型都有[DataContract],并且所有公共属性都有[DataMember],并且您的DTO位于single namespace

此请求的另一个问题是您正在将SOAP请求用于错误的端点地址。即SOAP端点是/soap11/soap12。在上面的示例中,它尝试将其发送到/GetProductDetails自定义HTTP路由,而对于SOAP,它需要是/soap11soap12

如果您只是想通过HTTP发送XML,那么您应该发送原始XML有效负载,通过序列化Request DTO可以找到它的样子,例如:

string requestXml = new GetProductDetails { ... }.ToXml();

您可以使用HTTP Utils轻松发送:

var responseXml = "http://localhost/ServiceStackAPI/GetProductDetails"
    .PostXmlToUrl(xmlRequest);

var responseDto = responseXml.FromXml<GetProductDetailsResponse>();

假设您在[Route("/GetProductDetails")]请求DTO上定义了GetProductDetails