Servicestack SOAP& ToOptimizedResult客户端解析错误

时间:2012-07-04 03:10:30

标签: web-services soap servicestack

我有一个超级简单的ServiceStack webservice使用最新的Nuget包配置(3.8.3我相信?)。我做的主要更改是调用ResultContext.ToOptimizedResult(object)来压缩响应消息,如果调用客户端支持它。该服务定义如下:

public class PingService : BaseService<Ping>
    {
        protected override string OperationName { get { return "Ping"; } }

        protected override object Run(Ping request)
        {
            // Implementation removed for brevity
            return new PingResponse();
        }

        protected override object OnAfterExecute(object response)
        {
            //return response;
            return RequestContext.ToOptimizedResult(response);
        }
    }

客户端代码如下:

var jsonClient = new JsonServiceClient("http://localhost/WebAppServiceV3/api");
var response = jsonClient.Send<PingResponse>(new Ping { LoginInfo = new ClientLoginInfo { UserName = "guest", Password = "guest", ClientPlatform = "TEST", ClientVersion = "1.3", InstanceUID = Guid.NewGuid().ToString() } });
Console.WriteLine(response.ToJson() + "\n");

var jsvClient = new JsvServiceClient("http://localhost/WebAppServiceV3/api");
response = jsvClient.Send<PingResponse>(new Ping { LoginInfo = new ClientLoginInfo { UserName = "guest", Password = "guest", ClientPlatform = "TEST", ClientVersion = "1.3", InstanceUID = Guid.NewGuid().ToString() } });
Console.WriteLine(response.ToJsv() + "\n");

var xmlClient = new XmlServiceClient("http://localhost/WebAppServiceV3/api");
response = xmlClient.Send<PingResponse>(new Ping { LoginInfo = new ClientLoginInfo { UserName = "guest", Password = "guest", ClientPlatform = "TEST", ClientVersion = "1.3", InstanceUID = Guid.NewGuid().ToString() } });
Console.WriteLine(response.ToXml() + "\n");

var soap11 = new Soap11ServiceClient("http://localhost/WebAppServiceV3/api");
response = soap11.Send<PingResponse>(new Ping { LoginInfo = new ClientLoginInfo { UserName = "guest", Password = "guest", ClientPlatform = "TEST", ClientVersion = "1.3", InstanceUID = Guid.NewGuid().ToString() } });
Console.WriteLine(response.ToXml() + "\n");

var soap12 = new Soap12ServiceClient("http://localhost/WebAppServiceV3/api");
response = soap12.Send<PingResponse>(new Ping { LoginInfo = new ClientLoginInfo { UserName = "guest", Password = "guest", ClientPlatform = "TEST", ClientVersion = "1.3", InstanceUID = Guid.NewGuid().ToString() } });
Console.WriteLine(response.ToXml() + "\n");

Json,Jsv和Xml客户端都在这里接收压缩响应并且工作正常。当行return ResultContext.ToOptimizedResult(result)包含在服务中而不仅仅是return result时,Soap客户端会抛出异常。

似乎客户端期望<PingResponse/>元素,但在返回压缩结果时会收到<Base64Binary/>元素:

Error in line 1 position 185. Expecting element 'PingResponse' from namespace 'http://schemas.datacontract.org/2004/07/IMDSSWebService_SS.ServiceModel'.. Encountered 'Element'  with name 'base64Binary', namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.

压缩开启(失败)

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">...</base64Binary>
  </s:Body>
</s:Envelope>

压缩关闭(成功)

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <PingResponse>...<PingResponse>
  </s:Body>
</s:Envelope>

有关我可能在这里做错的任何见解?提前谢谢。

2 个答案:

答案 0 :(得分:1)

目前不支持压缩SOAP。

您可以在https://github.com/ServiceStack/ServiceStack/issues提交问题以请求和跟踪此功能。

答案 1 :(得分:0)

我们之前遇到过这种情况,除了ServiceStack中的JSON序列化程序...你必须明确指定返回类型:

protected PingResponse Run(Ping request)
{
    // Implementation removed for brevity
    return new PingResponse();
}

(例如,您不能使用object作为返回类型。)

我们来回尝试所有不同的方法(dyanmic,泛型等),而SerivceStack由于其数据契约性质而不能全局支持这些方法。

我们最终要做的是为每种类型创建API方法,这些方法将使用泛型/简单对象(例如RunPingResponse()RunOtherResponse()等)。 :(