Skip to content

Commit

Permalink
Add Proxy to all ServiceClients, add url overload for SendOneWay meth…
Browse files Browse the repository at this point in the history
…od, use overload in mqhost reply messages
  • Loading branch information
mythz committed Nov 1, 2011
1 parent b1c8c2b commit e2ab673
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/ServiceStack.Common/Messaging/MessageHandler.cs
Expand Up @@ -143,26 +143,26 @@ public void ProcessMessage(IMessageQueueClient mqClient, Message<T> message)
else
{
//If there is a response send it to the typed response OutQ
var mqName = message.ReplyTo ?? new QueueNames(response.GetType()).In;
var replyClient = ReplyClientFactory(mqName);
var mqReplyTo = message.ReplyTo ?? new QueueNames(response.GetType()).In;
var replyClient = ReplyClientFactory(mqReplyTo);
if (replyClient != null)
{
try
{
replyClient.SendOneWay(response);
replyClient.SendOneWay(mqReplyTo, response);
return;
}
catch (Exception ex)
{
Log.Error("Could not send response to '{0}' with client '{1}'"
.Fmt(mqName, replyClient.GetType().Name), ex);
.Fmt(mqReplyTo, replyClient.GetType().Name), ex);
}
}

//Otherwise send to our trusty response Queue (inc if replyClient fails)
var responseMessage = Message.Create(response);
responseMessage.ReplyId = message.Id;
mqClient.Publish(mqName, responseMessage.ToBytes());
mqClient.Publish(mqReplyTo, responseMessage.ToBytes());
}
}
catch (Exception ex)
Expand Down
38 changes: 24 additions & 14 deletions src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs
Expand Up @@ -81,6 +81,8 @@ public void SetCredentials(string userName, string password)

public string HttpMethod { get; set; }

public IWebProxy Proxy { get; set; }

private ICredentials credentials;
public ICredentials Credentials
{
Expand Down Expand Up @@ -222,22 +224,15 @@ private WebRequest SendRequest(string httpMethod, string requestUri, object requ
var client = (HttpWebRequest)WebRequest.Create(requestUri);
try
{
if (this.Timeout.HasValue)
{
client.Timeout = (int)this.Timeout.Value.TotalMilliseconds;
}

client.Accept = ContentType;
client.Method = httpMethod;
if (this.credentials != null)
{
client.Credentials = this.credentials;
}

if (Proxy != null) client.Proxy = Proxy;
if (this.Timeout.HasValue) client.Timeout = (int)this.Timeout.Value.TotalMilliseconds;
if (this.credentials != null) client.Credentials = this.credentials;

if (HttpWebRequestFilter != null)
{
HttpWebRequestFilter(client);
}
HttpWebRequestFilter(client);

if (httpMethod != Web.HttpMethod.Get
&& httpMethod != Web.HttpMethod.Delete)
Expand Down Expand Up @@ -265,13 +260,27 @@ private string GetUrl(string relativeOrAbsoluteUrl)
: this.BaseUri + relativeOrAbsoluteUrl;
}

private byte[] DownloadBytes(string requestUri, object request)
{
var webRequest = SendRequest(requestUri, request);
using (var response = webRequest.GetResponse())
using (var stream = response.GetResponseStream())
return stream.ReadFully();
}

public void SendOneWay(object request)
{
var requestUri = this.AsyncOneWayBaseUri.WithTrailingSlash() + request.GetType().Name;
SendRequest(requestUri, request);
DownloadBytes(requestUri, request);
}

public void SendAsync<TResponse>(object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
public void SendOneWay(string relativeOrAbsoluteUrl, object request)
{
var requestUri = GetUrl(relativeOrAbsoluteUrl);
DownloadBytes(requestUri, request);
}

public void SendAsync<TResponse>(object request, Action<TResponse> onSuccess, Action<TResponse, Exception> onError)
{
var requestUri = this.SyncReplyBaseUri.WithTrailingSlash() + request.GetType().Name;
asyncClient.SendAsync(Web.HttpMethod.Post, requestUri, request, onSuccess, onError);
Expand Down Expand Up @@ -350,6 +359,7 @@ public TResponse PostFile<TResponse>(string relativeOrAbsoluteUrl, FileInfo file
var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.Method = Web.HttpMethod.Post;
webRequest.Accept = ContentType;
if (Proxy != null) webRequest.Proxy = Proxy;

try
{
Expand Down
Expand Up @@ -130,7 +130,12 @@ public void SendOneWay(object request)
SendOneWay(request, request.GetType().Name);
}

public void SendOneWay(object request, string action)
public void SendOneWay(string relativeOrAbsoluteUrl, object request)
{
SendOneWay(Message.CreateMessage(MessageVersion, relativeOrAbsoluteUrl, request));
}

public void SendOneWay(object request, string action)
{
SendOneWay(Message.CreateMessage(MessageVersion, action, request));
}
Expand Down
6 changes: 4 additions & 2 deletions src/ServiceStack.Interfaces/Service/IOneWayClient.cs
Expand Up @@ -2,6 +2,8 @@ namespace ServiceStack.Service
{
public interface IOneWayClient
{
void SendOneWay(object request);
}
void SendOneWay(object request);

void SendOneWay(string relativeOrAbsoluteUrl, object request);
}
}
7 changes: 6 additions & 1 deletion src/ServiceStack.ServiceInterface/Testing/TestBase.cs
Expand Up @@ -111,7 +111,12 @@ public void SendOneWay(object request)
ServiceManager.Execute(request);
}

public TResponse Send<TResponse>(object request)
public void SendOneWay(string relativeOrAbsoluteUrl, object request)
{
ServiceManager.Execute(request);
}

public TResponse Send<TResponse>(object request)
{
var response = ServiceManager.Execute(request);
var httpResult = response as HttpResult;
Expand Down
Expand Up @@ -25,7 +25,12 @@ public void SendOneWay(object request)
ServiceManager.Execute(request);
}

private bool ApplyRequestFilters(object request)
public void SendOneWay(string relativeOrAbsoluteUrl, object request)
{
ServiceManager.Execute(request);
}

private bool ApplyRequestFilters(object request)
{
if (EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
{
Expand Down

0 comments on commit e2ab673

Please sign in to comment.