15
15
16
16
namespace Exceptionless . Submission {
17
17
public class DefaultSubmissionClient : ISubmissionClient , IDisposable {
18
- private readonly HttpClient _client ;
18
+ private readonly Lazy < HttpClient > _client ;
19
19
20
20
public DefaultSubmissionClient ( ExceptionlessConfiguration config ) {
21
- _client = CreateHttpClient ( config . UserAgent ) ;
21
+ _client = new Lazy < HttpClient > ( ( ) => CreateHttpClient ( config ) ) ;
22
22
}
23
23
24
24
public SubmissionResponse PostEvents ( IEnumerable < Event > events , ExceptionlessConfiguration config , IJsonSerializer serializer ) {
@@ -36,8 +36,8 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
36
36
if ( data . Length > 1024 * 4 )
37
37
content = new GzipContent ( content ) ;
38
38
39
- _client . AddAuthorizationHeader ( config . ApiKey ) ;
40
- response = _client . PostAsync ( url , content ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
39
+ _client . Value . AddAuthorizationHeader ( config . ApiKey ) ;
40
+ response = _client . Value . PostAsync ( url , content ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
41
41
} catch ( Exception ex ) {
42
42
return new SubmissionResponse ( 500 , message : ex . Message ) ;
43
43
}
@@ -48,7 +48,7 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
48
48
49
49
return new SubmissionResponse ( ( int ) response . StatusCode , GetResponseMessage ( response ) ) ;
50
50
}
51
-
51
+
52
52
public SubmissionResponse PostUserDescription ( string referenceId , UserDescription description , ExceptionlessConfiguration config , IJsonSerializer serializer ) {
53
53
if ( ! config . IsValid )
54
54
return new SubmissionResponse ( 500 , message : "Invalid client configuration settings." ) ;
@@ -64,8 +64,8 @@ public SubmissionResponse PostUserDescription(string referenceId, UserDescriptio
64
64
if ( data . Length > 1024 * 4 )
65
65
content = new GzipContent ( content ) ;
66
66
67
- _client . AddAuthorizationHeader ( config . ApiKey ) ;
68
- response = _client . PostAsync ( url , content ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
67
+ _client . Value . AddAuthorizationHeader ( config . ApiKey ) ;
68
+ response = _client . Value . PostAsync ( url , content ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
69
69
} catch ( Exception ex ) {
70
70
return new SubmissionResponse ( 500 , message : ex . Message ) ;
71
71
}
@@ -85,8 +85,8 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
85
85
86
86
HttpResponseMessage response ;
87
87
try {
88
- _client . AddAuthorizationHeader ( config . ApiKey ) ;
89
- response = _client . GetAsync ( url ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
88
+ _client . Value . AddAuthorizationHeader ( config . ApiKey ) ;
89
+ response = _client . Value . GetAsync ( url ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
90
90
} catch ( Exception ex ) {
91
91
var message = String . Concat ( "Unable to retrieve configuration settings. Exception: " , ex . GetMessage ( ) ) ;
92
92
return new SettingsResponse ( false , message : message ) ;
@@ -105,22 +105,22 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
105
105
var settings = serializer . Deserialize < ClientConfiguration > ( json ) ;
106
106
return new SettingsResponse ( true , settings . Settings , settings . Version ) ;
107
107
}
108
-
108
+
109
109
public void SendHeartbeat ( string sessionIdOrUserId , bool closeSession , ExceptionlessConfiguration config ) {
110
110
if ( ! config . IsValid )
111
111
return ;
112
112
113
113
string url = String . Format ( "{0}/events/session/heartbeat?id={1}&close={2}" , GetHeartbeatServiceEndPoint ( config ) , sessionIdOrUserId , closeSession ) ;
114
114
try {
115
- _client . AddAuthorizationHeader ( config . ApiKey ) ;
116
- _client . GetAsync ( url ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
115
+ _client . Value . AddAuthorizationHeader ( config . ApiKey ) ;
116
+ _client . Value . GetAsync ( url ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
117
117
} catch ( Exception ex ) {
118
118
var log = config . Resolver . GetLog ( ) ;
119
119
log . Error ( String . Concat ( "Error submitting heartbeat: " , ex . GetMessage ( ) ) ) ;
120
120
}
121
121
}
122
122
123
- private HttpClient CreateHttpClient ( string userAgent ) {
123
+ protected virtual HttpClient CreateHttpClient ( ExceptionlessConfiguration config ) {
124
124
#if NET45
125
125
var handler = new WebRequestHandler { UseDefaultCredentials = true } ;
126
126
handler . ServerCertificateValidationCallback = delegate { return true ; } ;
@@ -130,17 +130,19 @@ private HttpClient CreateHttpClient(string userAgent) {
130
130
//handler.ServerCertificateCustomValidationCallback = delegate { return true; };
131
131
#endif
132
132
#endif
133
-
134
133
if ( handler . SupportsAutomaticDecompression )
135
134
handler . AutomaticDecompression = DecompressionMethods . Deflate | DecompressionMethods . GZip | DecompressionMethods . None ;
136
135
137
136
if ( handler . SupportsRedirectConfiguration )
138
137
handler . AllowAutoRedirect = true ;
139
-
138
+
139
+ if ( handler . SupportsProxy && config . Proxy != null )
140
+ handler . Proxy = config . Proxy ;
141
+
140
142
var client = new HttpClient ( handler , true ) ;
141
143
client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
142
144
client . DefaultRequestHeaders . ExpectContinue = false ;
143
- client . DefaultRequestHeaders . UserAgent . ParseAdd ( userAgent ) ;
145
+ client . DefaultRequestHeaders . UserAgent . ParseAdd ( config . UserAgent ) ;
144
146
145
147
return client ;
146
148
}
@@ -205,7 +207,8 @@ private Uri GetHeartbeatServiceEndPoint(ExceptionlessConfiguration config) {
205
207
}
206
208
207
209
public void Dispose ( ) {
208
- _client . Dispose ( ) ;
210
+ if ( _client . IsValueCreated )
211
+ _client . Value . Dispose ( ) ;
209
212
}
210
213
}
211
214
}
0 commit comments