Skip to content

Commit 78d00a6

Browse files
committed
Add new SendAllOneWay and SendAllAsync API's and tests
1 parent 2cbd80f commit 78d00a6

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/ServiceStack.Client/ServiceClientBase.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,15 @@ public virtual void SendOneWay(string relativeOrAbsoluteUrl, object request)
818818
SendOneWay(HttpMethods.Post, relativeOrAbsoluteUrl, request);
819819
}
820820

821+
public virtual void SendAllOneWay<TResponse>(IEnumerable<IReturn<TResponse>> requests)
822+
{
823+
var elType = requests.GetType().GetElementType()
824+
?? requests.GetType().GetGenericArguments().First();
825+
826+
var requestUri = this.AsyncOneWayBaseUri.WithTrailingSlash() + elType.Name + "[]";
827+
SendOneWay(HttpMethods.Post, requestUri, requests);
828+
}
829+
821830
public virtual void SendOneWay(string httpMethod, string relativeOrAbsoluteUrl, object requestDto)
822831
{
823832
var requestUri = GetUrl(relativeOrAbsoluteUrl);
@@ -839,6 +848,8 @@ public virtual void SendOneWay(string httpMethod, string relativeOrAbsoluteUrl,
839848
{
840849
throw;
841850
}
851+
852+
using(response){} //auto dispose
842853
}
843854
}
844855

@@ -858,6 +869,16 @@ public virtual Task<HttpWebResponse> SendAsync(IReturnVoid requestDto)
858869
return SendAsync<HttpWebResponse>(requestDto);
859870
}
860871

872+
public virtual Task<List<TResponse>> SendAllAsync<TResponse>(IEnumerable<IReturn<TResponse>> requests)
873+
{
874+
var elType = requests.GetType().GetElementType()
875+
?? requests.GetType().GetGenericArguments().First();
876+
877+
var requestUri = this.SyncReplyBaseUri.WithTrailingSlash() + elType.Name + "[]";
878+
879+
return asyncClient.SendAsync<List<TResponse>>(HttpMethods.Post, requestUri, requests);
880+
}
881+
861882

862883
public virtual Task<TResponse> GetAsync<TResponse>(IReturn<TResponse> requestDto)
863884
{

tests/ServiceStack.WebHost.Endpoints.Tests/ReplyAllTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Funq;
45
using NUnit.Framework;
56
using ServiceStack.Data;
@@ -247,6 +248,43 @@ public void Can_send_multi_reply_HelloAllCustom_requests()
247248
}));
248249
}
249250

251+
[Test]
252+
public async Task Can_send_async_multi_reply_HelloAllCustom_requests()
253+
{
254+
var client = new JsonServiceClient(Config.AbsoluteBaseUri);
255+
256+
var requests = new[]
257+
{
258+
new HelloAllCustom { Name = "Foo" },
259+
new HelloAllCustom { Name = "Bar" },
260+
new HelloAllCustom { Name = "Baz" },
261+
};
262+
263+
var responses = await client.SendAllAsync(requests);
264+
responses.PrintDump();
265+
266+
var results = responses.Map(x => x.Result);
267+
268+
Assert.That(results, Is.EquivalentTo(new[] {
269+
"Custom, Foo!", "Custom, Bar!", "Custom, Baz!"
270+
}));
271+
}
272+
273+
[Test]
274+
public void Can_send_multi_oneway_HelloAllCustom_requests()
275+
{
276+
var client = new JsonServiceClient(Config.AbsoluteBaseUri);
277+
278+
var requests = new[]
279+
{
280+
new HelloAllCustom { Name = "Foo" },
281+
new HelloAllCustom { Name = "Bar" },
282+
new HelloAllCustom { Name = "Baz" },
283+
};
284+
285+
client.SendAllOneWay(requests);
286+
}
287+
250288
[Test]
251289
public void Can_send_multiple_single_HelloAllTransaction()
252290
{

0 commit comments

Comments
 (0)