Skip to content

Commit 5391de5

Browse files
committed
390625: Move the System.Web.Http.HttpRequestMessageExtensions and HttpResponseMessageExtensions classes to the System.Net.Http namespace
Also move GetCorrelationId to main request extensions file.
1 parent ca9c37e commit 5391de5

File tree

8 files changed

+49
-60
lines changed

8 files changed

+49
-60
lines changed

src/System.Web.Http/Controllers/HttpActionBinding.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.Http;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using System.Web.Http.Metadata;

src/System.Web.Http/GlobalSuppressions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Web.Http.Dependencies", Justification = "Namespace follows folder structure")]
1919
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Web.Http.Tracing.Tracers", Justification = "Namespace follows folder structure")]
2020
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Web.Http.Validation.Validators", Justification = "Namespace follows folder structure")]
21+
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "System.Net.Http", Justification = "Functionality logically belongs in a namespace defined in a different binary")]

src/System.Web.Http/HttpRequestMessageExtensions.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using System.Collections.Generic;
22
using System.ComponentModel;
33
using System.Diagnostics.CodeAnalysis;
4-
using System.Net;
5-
using System.Net.Http;
64
using System.Net.Http.Formatting;
75
using System.Net.Http.Headers;
86
using System.Threading;
7+
using System.Web.Http;
98
using System.Web.Http.Dependencies;
109
using System.Web.Http.Hosting;
1110
using System.Web.Http.Properties;
1211
using System.Web.Http.Routing;
1312

14-
namespace System.Web.Http
13+
namespace System.Net.Http
1514
{
1615
/// <summary>
1716
/// Provides extension methods for the <see cref="HttpRequestMessage"/> class.
@@ -353,5 +352,29 @@ public static void DisposeRequestResources(this HttpRequestMessage request)
353352
resourcesToDispose.Clear();
354353
}
355354
}
355+
356+
/// <summary>
357+
/// Retrieves the <see cref="Guid"/> which has been assigned as the
358+
/// correlation id associated with the given <paramref name="request"/>.
359+
/// The value will be created and set the first time this method is called.
360+
/// </summary>
361+
/// <param name="request">The <see cref="HttpRequestMessage"/></param>
362+
/// <returns>The <see cref="Guid"/> associated with that request.</returns>
363+
public static Guid GetCorrelationId(this HttpRequestMessage request)
364+
{
365+
if (request == null)
366+
{
367+
throw Error.ArgumentNull("request");
368+
}
369+
370+
Guid correlationId;
371+
if (!request.Properties.TryGetValue<Guid>(HttpPropertyKeys.RequestCorrelationKey, out correlationId))
372+
{
373+
correlationId = Guid.NewGuid();
374+
request.Properties.Add(HttpPropertyKeys.RequestCorrelationKey, correlationId);
375+
}
376+
377+
return correlationId;
378+
}
356379
}
357380
}

src/System.Web.Http/HttpResponseMessageExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.ComponentModel;
2-
using System.Net.Http;
2+
using System.Web.Http;
33

4-
namespace System.Web.Http
4+
namespace System.Net.Http
55
{
66
[EditorBrowsable(EditorBrowsableState.Never)]
77
public static class HttpResponseMessageExtensions

src/System.Web.Http/System.Web.Http.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@
263263
<DesignTime>True</DesignTime>
264264
<DependentUpon>SRResources.resx</DependentUpon>
265265
</Compile>
266-
<Compile Include="Tracing\HttpRequestMessageExtensions.cs" />
267266
<Compile Include="Tracing\IFormatterTracer.cs" />
268267
<Compile Include="Tracing\ITraceManager.cs" />
269268
<Compile Include="Tracing\ITraceWriter.cs" />

src/System.Web.Http/Tracing/HttpRequestMessageExtensions.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/System.Web.Http.Test/HttpRequestMessageExtensionsTest.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using System.Collections.Generic;
2-
using System.Net;
3-
using System.Net.Http;
42
using System.Net.Http.Formatting;
53
using System.Net.Http.Formatting.Mocks;
64
using System.Net.Http.Headers;
75
using System.Threading;
6+
using System.Web.Http;
87
using System.Web.Http.Hosting;
98
using System.Web.Http.Routing;
109
using System.Web.Http.Services;
11-
using Microsoft.TestCommon;
1210
using Moq;
1311
using Xunit;
1412
using Assert = Microsoft.TestCommon.AssertEx;
1513

16-
namespace System.Web.Http
14+
namespace System.Net.Http
1715
{
1816
public class HttpRequestMessageExtensionsTest
1917
{
@@ -30,16 +28,11 @@ public HttpRequestMessageExtensionsTest()
3028
_request.Properties[HttpPropertyKeys.HttpConfigurationKey] = _config;
3129
}
3230

33-
[Fact]
34-
public void IsCorrectType()
35-
{
36-
Assert.Type.HasProperties(typeof(HttpRequestMessageExtensions), TypeAssert.TypeProperties.IsStatic | TypeAssert.TypeProperties.IsPublicVisibleClass);
37-
}
38-
3931
[Fact]
4032
public void GetConfigurationThrowsOnNull()
4133
{
42-
Assert.ThrowsArgumentNull(() => HttpRequestMessageExtensions.GetConfiguration(null), "request");
34+
HttpRequestMessage request = null;
35+
Assert.ThrowsArgumentNull(() => request.GetConfiguration(), "request");
4336
}
4437

4538
[Fact]
@@ -58,7 +51,8 @@ public void GetConfiguration()
5851
[Fact]
5952
public void GetSynchronizationContextThrowsOnNull()
6053
{
61-
Assert.ThrowsArgumentNull(() => HttpRequestMessageExtensions.GetSynchronizationContext(null), "request");
54+
HttpRequestMessage request = null;
55+
Assert.ThrowsArgumentNull(() => request.GetSynchronizationContext(), "request");
6256
}
6357

6458
[Fact]
@@ -103,14 +97,15 @@ public void GetRouteData_WhenRequestIsNull_Throws()
10397
[Fact]
10498
public void CreateResponse_DoingConneg_OnNullRequest_ThrowsException()
10599
{
100+
HttpRequestMessage request = null;
106101
Assert.ThrowsArgumentNull(() =>
107102
{
108-
HttpRequestMessageExtensions.CreateResponse(null, HttpStatusCode.OK, _value);
103+
request.CreateResponse(HttpStatusCode.OK, _value);
109104
}, "request");
110105

111106
Assert.ThrowsArgumentNull(() =>
112107
{
113-
HttpRequestMessageExtensions.CreateResponse(null, HttpStatusCode.OK, _value, configuration: null);
108+
request.CreateResponse(HttpStatusCode.OK, _value, configuration: null);
114109
}, "request");
115110
}
116111

@@ -121,7 +116,7 @@ public void CreateResponse_DoingConneg_OnNullConfiguration_ThrowsException()
121116

122117
Assert.Throws<InvalidOperationException>(() =>
123118
{
124-
HttpRequestMessageExtensions.CreateResponse(_request, HttpStatusCode.OK, _value, configuration: null);
119+
_request.CreateResponse(HttpStatusCode.OK, _value, configuration: null);
125120
}, "The request does not have an associated configuration object or the provided configuration was null.");
126121
}
127122

@@ -136,7 +131,7 @@ public void CreateResponse_DoingConneg_RetrievesContentNegotiatorFromServiceReso
136131
_config.Services = servicesMock.Object;
137132

138133
// Act
139-
HttpRequestMessageExtensions.CreateResponse(_request, HttpStatusCode.OK, _value, _config);
134+
_request.CreateResponse(HttpStatusCode.OK, _value, _config);
140135

141136
// Assert
142137
servicesMock.Verify();
@@ -149,7 +144,7 @@ public void CreateResponse_DoingConneg_WhenNoContentNegotiatorInstanceRegistered
149144
_config.Services.Clear(typeof(IContentNegotiator));
150145

151146
// Act & Assert
152-
Assert.Throws<InvalidOperationException>(() => HttpRequestMessageExtensions.CreateResponse(_request, HttpStatusCode.OK, _value, _config),
147+
Assert.Throws<InvalidOperationException>(() => _request.CreateResponse(HttpStatusCode.OK, _value, _config),
153148
"The provided configuration does not have an instance of the 'System.Net.Http.Formatting.IContentNegotiator' service registered.");
154149
}
155150

@@ -161,7 +156,7 @@ public void CreateResponse_DoingConneg_WhenContentNegotiatorReturnsNullResult_Th
161156
_config.Services.Replace(typeof(IContentNegotiator), _negotiatorMock.Object);
162157

163158
// Act
164-
var response = HttpRequestMessageExtensions.CreateResponse<string>(_request, HttpStatusCode.OK, "", _config);
159+
var response = _request.CreateResponse<string>(HttpStatusCode.OK, "", _config);
165160

166161
// Assert
167162
Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode);
@@ -178,7 +173,7 @@ public void CreateResponse_DoingConneg_PerformsContentNegotiationAndCreatesConte
178173
_config.Services.Replace(typeof(IContentNegotiator), _negotiatorMock.Object);
179174

180175
// Act
181-
var response = HttpRequestMessageExtensions.CreateResponse<string>(_request, HttpStatusCode.NoContent, "42", _config);
176+
var response = _request.CreateResponse<string>(HttpStatusCode.NoContent, "42", _config);
182177

183178
// Assert
184179
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
@@ -335,8 +330,8 @@ public void CreateResponse_AcceptingFormatter_WithOverridenMediaTypeHeader_Creat
335330
[Fact]
336331
public void RegisterForDispose_WhenRequestParameterIsNull_Throws()
337332
{
338-
Assert.ThrowsArgumentNull(
339-
() => HttpRequestMessageExtensions.RegisterForDispose(request: null, resource: null), "request");
333+
HttpRequestMessage request = null;
334+
Assert.ThrowsArgumentNull(() => request.RegisterForDispose(resource: null), "request");
340335
}
341336

342337
[Fact]
@@ -375,7 +370,8 @@ public void RegisterForDispose_WhenResourceListExists_AddsResource()
375370
[Fact]
376371
public void DisposeRequestResources_WhenRequestParameterIsNull_Throws()
377372
{
378-
Assert.ThrowsArgumentNull(() => HttpRequestMessageExtensions.DisposeRequestResources(request: null), "request");
373+
HttpRequestMessage request = null;
374+
Assert.ThrowsArgumentNull(() => request.DisposeRequestResources(), "request");
379375
}
380376

381377
[Fact]

test/System.Web.Http.Test/HttpResponseMessageExtensionsTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.Collections.Generic;
2-
using System.Net.Http;
32
using System.Net.Http.Formatting;
43
using Moq;
54
using Xunit;
65
using Xunit.Extensions;
76
using Assert = Microsoft.TestCommon.AssertEx;
87

9-
namespace System.Web.Http
8+
namespace System.Net.Http
109
{
1110
public class HttpResponseMessageExtensionsTest
1211
{

0 commit comments

Comments
 (0)