服务器无法识别自动批处理的请求
Auto batched requests not recognized on the server
我想试用 ServiceStack 的 Auto Batched Request 功能。但它似乎不适用于服务器端。
我有这样的服务:
public class HelloService : Service
{
public object Any(HelloRequestDTO request)
{
return new HelloResponseDTO { Greetings = "Hello, " + request.Name };
}
/// <summary>This method does not get invoked.</summary>
public object Any(HelloRequestDTO[] requests)
{
return new HelloResponseDTO { Greetings = "Hello, " + string.Join(", ", requests.Select(r => r.Name).ToArray()) };
}
}
这是 DTO,它们位于共享库中,因此客户端和服务器都可以使用它们。
[ProtoContract]
public class HelloRequestDTO : IReturn<HelloResponseDTO>
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
public class HelloResponseDTO
{
[ProtoMember(1)]
public string Greetings { get; set; }
}
我使用以下代码从控制台应用程序发送请求:
var requests = new[]
{
new HelloRequestDTO { Name = "PersonA" },
new HelloRequestDTO { Name = "PersonB" },
new HelloRequestDTO { Name = "PersonC" }
};
const string host = "MY-MACHINE:5667";
var serviceUrl = string.Format("http://{0}/api/hello?", host);
var protoClient = new ProtoBufServiceClient(serviceUrl);
var jsonClient = new JsonServiceClient(serviceUrl);
//var protoResponses = protoClient.SendAll(requests);
var jsonResponses = jsonClient.SendAll(requests);
当 JSON 序列化请求到达服务器时抛出异常:
Type definitions should start with a '{', expecting serialized type 'HelloRequestDTO', got string starting with: [{"Name":"PersonA"},{"Name":"PersonB"},{"Name":"Pe
我检查了请求是否有效,这是我用 Fiddler 捕获的内容:
POST http://MY-MACHINE:5667/api/hello?/json/reply/HelloRequestDTO[]
HTTP/1.1
Accept: application/json
User-Agent: ServiceStack .NET Client 4,038
Accept-Encoding: gzip,deflate
Content-Type: application/json
Host: MY-MACHINE:5667
Content-Length: 58
Expect: 100-continue
Connection: Keep-Alive
[{"Name":"PersonA"},{"Name":"PersonB"},{"Name":"PersonC"}]
当 protobuf 序列化请求到达时,仅调用非数组处理程序(public 对象 Any(HelloRequestDTO 请求))并且参数为 "PersonC",其他两个 DTO 被丢弃。
看来我缺少某种开关,ServiceStack 无法识别它正在处理自动批处理请求。
如果有帮助,我也可以上传我的测试解决方案。
ServiceClient中的url应该只包含ServiceStack实例的BaseUrl
,而不是具体服务的url。
将 Url 更改为:
var baseUrl = "http://{0}/api/".Fmt(host);
var protoClient = new ProtoBufServiceClient(baseUrl);
var jsonClient = new JsonServiceClient(baseUrl);
我想试用 ServiceStack 的 Auto Batched Request 功能。但它似乎不适用于服务器端。 我有这样的服务:
public class HelloService : Service
{
public object Any(HelloRequestDTO request)
{
return new HelloResponseDTO { Greetings = "Hello, " + request.Name };
}
/// <summary>This method does not get invoked.</summary>
public object Any(HelloRequestDTO[] requests)
{
return new HelloResponseDTO { Greetings = "Hello, " + string.Join(", ", requests.Select(r => r.Name).ToArray()) };
}
}
这是 DTO,它们位于共享库中,因此客户端和服务器都可以使用它们。
[ProtoContract]
public class HelloRequestDTO : IReturn<HelloResponseDTO>
{
[ProtoMember(1)]
public string Name { get; set; }
}
[ProtoContract]
public class HelloResponseDTO
{
[ProtoMember(1)]
public string Greetings { get; set; }
}
我使用以下代码从控制台应用程序发送请求:
var requests = new[]
{
new HelloRequestDTO { Name = "PersonA" },
new HelloRequestDTO { Name = "PersonB" },
new HelloRequestDTO { Name = "PersonC" }
};
const string host = "MY-MACHINE:5667";
var serviceUrl = string.Format("http://{0}/api/hello?", host);
var protoClient = new ProtoBufServiceClient(serviceUrl);
var jsonClient = new JsonServiceClient(serviceUrl);
//var protoResponses = protoClient.SendAll(requests);
var jsonResponses = jsonClient.SendAll(requests);
当 JSON 序列化请求到达服务器时抛出异常:
Type definitions should start with a '{', expecting serialized type 'HelloRequestDTO', got string starting with: [{"Name":"PersonA"},{"Name":"PersonB"},{"Name":"Pe
我检查了请求是否有效,这是我用 Fiddler 捕获的内容:
POST http://MY-MACHINE:5667/api/hello?/json/reply/HelloRequestDTO[] HTTP/1.1
Accept: application/json
User-Agent: ServiceStack .NET Client 4,038
Accept-Encoding: gzip,deflate
Content-Type: application/json
Host: MY-MACHINE:5667
Content-Length: 58
Expect: 100-continue
Connection: Keep-Alive
[{"Name":"PersonA"},{"Name":"PersonB"},{"Name":"PersonC"}]
当 protobuf 序列化请求到达时,仅调用非数组处理程序(public 对象 Any(HelloRequestDTO 请求))并且参数为 "PersonC",其他两个 DTO 被丢弃。
看来我缺少某种开关,ServiceStack 无法识别它正在处理自动批处理请求。
如果有帮助,我也可以上传我的测试解决方案。
ServiceClient中的url应该只包含ServiceStack实例的BaseUrl
,而不是具体服务的url。
将 Url 更改为:
var baseUrl = "http://{0}/api/".Fmt(host);
var protoClient = new ProtoBufServiceClient(baseUrl);
var jsonClient = new JsonServiceClient(baseUrl);