-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathServiceClientBase.cs
More file actions
151 lines (125 loc) · 3.71 KB
/
ServiceClientBase.cs
File metadata and controls
151 lines (125 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.IO;
using System.Net;
using System.Security.Authentication;
using ServiceStack.Logging;
using ServiceStack.Service;
using ServiceStack.Text;
namespace ServiceStack.ServiceClient.Web
{
public abstract class ServiceClientBase
: IServiceClient
{
private static ILog log = LogManager.GetLogger(typeof (ServiceClientBase));
public static Action<HttpWebRequest> HttpWebRequestFilter { get; set; }
public const string DefaultHttpMethod = "POST";
protected ServiceClientBase()
{
this.HttpMethod = DefaultHttpMethod;
}
protected ServiceClientBase(string syncReplyBaseUri, string asyncOneWayBaseUri)
: this()
{
this.SyncReplyBaseUri = syncReplyBaseUri;
this.AsyncOneWayBaseUri = asyncOneWayBaseUri;
}
public string BaseUri
{
set
{
var baseUri = value.WithTrailingSlash();
this.SyncReplyBaseUri = baseUri + "SyncReply/";
this.AsyncOneWayBaseUri = baseUri + "AsyncOneWay/";
}
}
public string SyncReplyBaseUri { get; set; }
public string AsyncOneWayBaseUri { get; set; }
public TimeSpan? Timeout { get; set; }
public abstract string ContentType { get; }
public string HttpMethod { get; set; }
public abstract void SerializeToStream(object request, Stream stream);
public abstract T DeserializeFromStream<T>(Stream stream);
public virtual TResponse Send<TResponse>(object request)
{
var requestUri = this.SyncReplyBaseUri.WithTrailingSlash() + request.GetType().Name;
var client = SendRequest(request, requestUri);
try
{
using (var responseStream = client.GetResponse().GetResponseStream())
{
var response = DeserializeFromStream<TResponse>(responseStream);
return response;
}
}
catch (WebException webEx)
{
if (webEx.Status == WebExceptionStatus.ProtocolError)
{
var errorResponse = ((HttpWebResponse)webEx.Response);
log.Error(webEx);
log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);
try
{
using (var stream = errorResponse.GetResponseStream())
{
var response = DeserializeFromStream<TResponse>(stream);
return response;
}
}
catch (WebException ex)
{
// Oh, well, we tried
throw;
}
}
throw;
}
}
private WebRequest SendRequest(object request, string requestUri)
{
var isHttpGet = HttpMethod != null && HttpMethod.ToUpper() == "GET";
if (isHttpGet)
{
var queryString = QueryStringSerializer.SerializeToString(request);
if (!string.IsNullOrEmpty(queryString))
{
requestUri += "?" + queryString;
}
}
var client = (HttpWebRequest)WebRequest.Create(requestUri);
try
{
if (this.Timeout.HasValue)
{
client.Timeout = (int)this.Timeout.Value.TotalMilliseconds;
}
client.Accept = string.Format("{0}, */*", ContentType);
client.Method = HttpMethod ?? DefaultHttpMethod;
if (HttpWebRequestFilter != null)
{
HttpWebRequestFilter(client);
}
if (!isHttpGet)
{
client.ContentType = ContentType;
using (var requestStream = client.GetRequestStream())
{
SerializeToStream(request, requestStream);
}
}
}
catch (AuthenticationException ex)
{
throw WebRequestUtils.CreateCustomException(requestUri, ex) ?? ex;
}
return client;
}
public void SendOneWay(object request)
{
var requestUri = this.AsyncOneWayBaseUri.WithTrailingSlash() + request.GetType().Name;
var client = SendRequest(request, requestUri);
}
public void Dispose() { }
}
}