2

我使用最新的 Nuget 包(我相信是 3.8.3?)配置了一个超级简单的 ServiceStack Web 服务。我所做的主要更改是调用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)包含在服务中时,Soap 客户端会抛出异常,而不仅仅是return result.

似乎客户端期待一个元素,但在返回压缩结果时<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>

关于我在这里可能做错了什么的任何见解?提前致谢。

4

2 回答 2

1

当前不支持 SOAP 的压缩。

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

于 2012-07-05T04:31:48.823 回答
0

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

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

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

我们来回尝试了所有不同的方法(dyanmic、泛型等),而 SerivceStack 只是由于其数据合同性质而无法在全球范围内支持这些方法。

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

于 2012-07-04T03:17:09.797 回答