我使用最新的 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>
关于我在这里可能做错了什么的任何见解?提前致谢。