Adding a feature to get the traceidentifier for a request

Addresses : https://github.com/aspnet/HttpAbstractions/issues/117

Related changes in Helios & weblistener in separate PRs.
This commit is contained in:
Praburaj 2015-03-02 16:38:38 -08:00
parent de1e8763dd
commit ae23f7c7bc
4 changed files with 49 additions and 3 deletions

View File

@ -0,0 +1,18 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Http.Interfaces
{
/// <summary>
/// Feature to identify a request.
/// </summary>
public interface IRequestIdentifierFeature
{
/// <summary>
/// Identifier to trace a request.
/// </summary>
Guid TraceIdentifier { get; }
}
}

View File

@ -20,6 +20,14 @@ namespace Microsoft.AspNet.Owin
#endregion
#region OWIN v1.1.0 - 3.2.1 Request Data
// OWIN 1.1.0 http://owin.org/html/owin.html
public const string RequestId = "owin.RequestId";
#endregion
#region OWIN v1.0.0 - 3.2.2. Response Data
// http://owin.org/spec/owin-1.0.0.html

View File

@ -80,7 +80,7 @@ namespace Microsoft.AspNet.Owin
{ OwinConstants.Security.User, new FeatureMap<IHttpAuthenticationFeature>(feature => feature.User,
()=> null, (feature, value) => feature.User = Utilities.MakeClaimsPrincipal((IPrincipal)value),
() => new HttpAuthenticationFeature())
},
}
};
// owin.CallCancelled is required but the feature may not be present.
@ -113,6 +113,9 @@ namespace Microsoft.AspNet.Owin
}
_context.Items[typeof(HttpContext).FullName] = _context; // Store for lookup when we transition back out of OWIN
// The request identifier is a string per the spec.
_entries[OwinConstants.RequestId] = new FeatureMap<IRequestIdentifierFeature>(feature => feature.TraceIdentifier.ToString());
}
// Public in case there's a new/custom feature interface that needs to be added.
@ -369,7 +372,7 @@ namespace Microsoft.AspNet.Owin
}
public FeatureMap(Func<TFeature, object> getter, Func<object> defaultFactory, Action<TFeature, object> setter)
: base(typeof(TFeature), feature => getter((TFeature)feature), defaultFactory,(feature, value) => setter((TFeature)feature, value))
: base(typeof(TFeature), feature => getter((TFeature)feature), defaultFactory, (feature, value) => setter((TFeature)feature, value))
{
}

View File

@ -32,7 +32,8 @@ namespace Microsoft.AspNet.Owin
IHttpRequestLifetimeFeature,
IHttpAuthenticationFeature,
IHttpWebSocketFeature,
IOwinEnvironmentFeature
IOwinEnvironmentFeature,
IRequestIdentifierFeature
{
public IDictionary<string, object> Environment { get; set; }
private bool _headersSent;
@ -427,6 +428,22 @@ namespace Microsoft.AspNet.Owin
get { return true; }
}
Guid IRequestIdentifierFeature.TraceIdentifier
{
get
{
var requestId = Prop<string>(OwinConstants.RequestId);
Guid requestIdentifier;
if (requestId != null && Guid.TryParse(requestId, out requestIdentifier))
{
return requestIdentifier;
}
return Guid.Empty;
}
}
public bool Remove(KeyValuePair<Type, object> item)
{
throw new NotSupportedException();