2

I have created a bulk request service. The purpose of this service is to allow a developer to send multiple requests at once, and then get a response back for each request made. This works great using non-cached requests. POST, PUT, and DELETE work for cached requests as these operations flush the cache. The response for the GET verb returns a ServiceStack.Common.Web.CompressedResult object instead of an object of the appropriate type.

My first question is am I re-inventing the wheel? Is there ServiceStack functionality that does this already that I missed?

If not, here are code excerpts of how I am accomplishing this. Calling the cached service directly (not using the bulk request object) returns the appropriate response. Thank you for any insight into this matter.

    public override object OnPost(BulkRequest request)
    {
        var response = new BulkResponse();
        response.Responses = new System.Collections.Generic.List<object>();

        string url = this.Request.AbsoluteUri.Replace(this.Request.PathInfo, string.Empty);

        if (request.Requests == null)
        {
            response.Message.Add("The Requests property cannot be null.");
            response.Status = Contract.StatusType.Error;
            return response;
        }

        if (request.Verb == null)
        {
            response.Message.Add("The Verb property cannot be null.");
            response.Status = Contract.StatusType.Error;
            return response;
        }

        if (request.Verb.Count != request.Requests.Count && request.UseFirstVerbForAllRequests == false)
        {
            response.Message.Add("There must be a matching verb for each request.");
            response.Status = Contract.StatusType.Error;
            return response;
        }

        try
        {
            for (int i = 0; i < request.Requests.Count; i++)
            {
                string verb = request.UseFirstVerbForAllRequests ? request.Verb[0] : request.Verb[i];

                if (String.IsNullOrWhiteSpace(verb))
                {
                    verb = HttpMethods.Get;
                }

                var requestResponse = this.Query(request.Requests[i], verb);
                response.Responses.Add(requestResponse);
            }

            response.Status = Contract.StatusType.Successful;
        } 
        catch (Exception ex) 
        {
            response.Message = new System.Collections.Generic.List<string>();
            response.Message.Add(ex.Message);
        }

        return response;
    }

The Query function determines the type of the request object and makes the appropriate request.

    private object Query(Object request, string verb)
    {
        var type = request.GetType();
        object response = null;

        if (type == typeof(Contract.Activity.ActivityRequest))
        {
            var service = this.ResolveService<Service.Services.Activity.ActivityService>();
            response = (verb == HttpMethod.Post.ToString()) ? service.Post((Contract.Activity.ActivityRequest)request) :
                (verb == HttpMethod.Get.ToString()) ? service.Get((Contract.Activity.ActivityRequest)request) :
                (verb == HttpMethod.Put.ToString()) ? service.Put((Contract.Activity.ActivityRequest)request) :
                (verb == HttpMethod.Delete.ToString()) ? service.Delete((Contract.Activity.ActivityRequest)request) : null;
        }
        else if (type == typeof(Contract.Activity.CachedActivityRequest))
        {
            var service = this.ResolveService<Service.Services.Activity.CachedActivityService>();
            response = (verb == HttpMethod.Post.ToString()) ? service.Post((Contract.Activity.CachedActivityRequest)request) :
                (verb == HttpMethod.Get.ToString()) ? service.Get((Contract.Activity.CachedActivityRequest)request) :
                (verb == HttpMethod.Put.ToString()) ? service.Put((Contract.Activity.CachedActivityRequest)request) :
                (verb == HttpMethod.Delete.ToString()) ? service.Delete((Contract.Activity.CachedActivityRequest)request) : null;
        }
        ...
        return response;
    }
4

0 回答 0