Search code examples
c#requestservicestackbatching

Auto batched requests not recognized on the server


I wanted to try out the Auto Batched Request feature of ServiceStack. But it does not seem to work on the server side. I have a Service like this:

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()) };
    }
}

Here are the DTOs, they are located in a shared library so they can be used by both the client and the server.

[ProtoContract]
public class HelloRequestDTO : IReturn<HelloResponseDTO>
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

[ProtoContract]
public class HelloResponseDTO
{
    [ProtoMember(1)]
    public string Greetings { get; set; }
}

I send the requests from a console app with the following code:

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);

When the JSON serialized requests arrive at the server a Exception is thrown:

Type definitions should start with a '{', expecting serialized type 'HelloRequestDTO', got string starting with: [{"Name":"PersonA"},{"Name":"PersonB"},{"Name":"Pe

I checked if the request is valid, here is what I captured with 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"}]

When the protobuf serialized requests arrive only the non-array handler gets invoked (public object Any(HelloRequestDTO request)) and the parameter is "PersonC", the other two DTOs get dropped.

It seems that I am missing some kind of switch, ServiceStack does not recognize that it is dealing with auto batched requests.

I can also upload my test solution if that helps.


Solution

  • The url in the ServiceClient should only contain the BaseUrl of the ServiceStack instance, not a url to a specific service.

    Change the Url to just:

    var baseUrl = "http://{0}/api/".Fmt(host);
    var protoClient = new ProtoBufServiceClient(baseUrl);
    var jsonClient = new JsonServiceClient(baseUrl);