Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
vmelamed committed Feb 1, 2016
1 parent f8c1bc2 commit 2a2d2bc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
12 changes: 4 additions & 8 deletions Aspects/Facilities/ExceptionPolicyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,10 @@ public static ExceptionManager CreateExceptionManager()

// create the exception manager
var exceptionManager = new ExceptionManager(
new ExceptionPolicyFactory()
.CreateManager()
.Policies // load the exception handling definitions in the log file
.Union( // add after that the exception handling definitions from the registrars
policyEntries
.Select(
// create a policy definition out of the name and the merged entries
pe => new ExceptionPolicyDefinition(pe.Key, pe.Value))));
policyEntries
.Select(
// create a policy definition out of the name and the merged entries
pe => new ExceptionPolicyDefinition(pe.Key, pe.Value)));

// set the manager in the ExceptionPolicy class
ExceptionPolicy.SetExceptionManager(exceptionManager, false);
Expand Down
71 changes: 40 additions & 31 deletions Aspects/Wcf/Behaviors/ExceptionShieldingErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,15 @@ void HandleFaultException(
if (faultExceptionType.IsGenericType)
{
// it is FaultException<T> - extract the detail and build the message
var faultDetail = faultExceptionType.GetProperty("Detail").GetGetMethod().Invoke(faultException, null);
var faultDetail = faultExceptionType
.GetProperty("Detail")
.GetGetMethod()
.Invoke(faultException, null);

BuildHttpResponseMessage(
faultDetail,
_wcfContext.GetFaultedAction(faultDetail.GetType()) ?? fault.Headers.Action,
HttpStatusCode.InternalServerError,
(faultDetail as Fault)?.HttpStatusCode ?? HttpStatusCode.InternalServerError,
ref fault);
}
else
Expand Down Expand Up @@ -367,36 +370,49 @@ WebContentFormat GetWebContentFormat()
if (WebOperationContext.Current == null)
return WebContentFormat.Default;

// 1. The media types in the request message’s Accept header.
var typeMapper = new WebContentTypeMapperDefaultJson();
var format = WebContentFormat.Default;
ContentType contentType = null;

var contentType = new ContentType(WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Accept]);
WebContentFormat format;
// 1. The media types in the request message’s Accept header.
var acceptHeader = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.Accept];

format = typeMapper.GetMessageFormatForContentType(contentType.MediaType);
if (!string.IsNullOrWhiteSpace(acceptHeader))
{
contentType = new ContentType(acceptHeader);
format = typeMapper.GetMessageFormatForContentType(contentType.MediaType);

if (format != WebContentFormat.Default)
return format;
if (format != WebContentFormat.Default)
return format;
}

// 2. The content-type of the request message.
var requestContentType = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.ContentType];

format = typeMapper.GetMessageFormatForContentType(requestContentType);
if (!string.IsNullOrWhiteSpace(requestContentType))
{
contentType = new ContentType(requestContentType);
format = typeMapper.GetMessageFormatForContentType(contentType.MediaType);

if (format != WebContentFormat.Default)
return format;
if (format != WebContentFormat.Default)
return format;
}

// 3. The default format setting in the operation.
var operation = _wcfContext.OperationMethod;
var webGet = operation.GetCustomAttribute<WebGetAttribute>();

if (webGet != null && webGet.IsResponseFormatSetExplicitly)
return webGet.ResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;
if (operation != null)
{
var webGet = operation.GetCustomAttribute<WebGetAttribute>();

if (webGet != null && webGet.IsResponseFormatSetExplicitly)
return webGet.ResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;

var webInvoke = operation.GetCustomAttribute<WebInvokeAttribute>();
var webInvoke = operation.GetCustomAttribute<WebInvokeAttribute>();

if (webInvoke != null && webInvoke.IsResponseFormatSetExplicitly)
return webInvoke.ResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;
if (webInvoke != null && webInvoke.IsResponseFormatSetExplicitly)
return webInvoke.ResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;
}

// 4. The default format setting in the WebHttpBehavior.
var webBehavior = _wcfContext.WebHttpBehavior;
Expand All @@ -413,17 +429,16 @@ void BuildHttpResponseMessage(
HttpStatusCode httpStatusCode,
ref Message fault)
{
//var faultDetails = faultContractWrapper.FaultContract as Fault;
var webFormat = GetWebContentFormat();
var responseMessageProperty = new HttpResponseMessageProperty();

responseMessageProperty.StatusCode = httpStatusCode;
responseMessageProperty.StatusDescription = Fault.GetHttpStatusDescription(responseMessageProperty.StatusCode);

HttpResponseMessageProperty responseMessageProperty;
var webFormat = GetWebContentFormat();

if (webFormat == WebContentFormat.Json)
{
// set the status code, description and content type in the response header:
responseMessageProperty = new HttpResponseMessageProperty();
responseMessageProperty.StatusCode = httpStatusCode;
responseMessageProperty.StatusDescription = Fault.GetHttpStatusDescription(responseMessageProperty.StatusCode);
responseMessageProperty.Headers[HttpResponseHeader.ContentType] = "application/json";

// build a new fault message. In the body use a JSON serializer to serialize the fault details.
Expand All @@ -433,18 +448,15 @@ void BuildHttpResponseMessage(
faultDetails,
new DataContractJsonSerializer(faultDetails.GetType()));

fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(webFormat));
fault.Properties.Add(HttpResponseMessageProperty.Name, responseMessageProperty);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(webFormat));
return;
}

if (webFormat == WebContentFormat.Xml)
{

// set the status code, description and content type in the response header:
responseMessageProperty = new HttpResponseMessageProperty();
responseMessageProperty.StatusCode = httpStatusCode;
responseMessageProperty.StatusDescription = Fault.GetHttpStatusDescription(responseMessageProperty.StatusCode);
responseMessageProperty.Headers[HttpResponseHeader.ContentType] = "application/xml";

// build a new fault message. In the body use a data contract (XML) serializer to serialize the fault details.
Expand All @@ -461,9 +473,6 @@ void BuildHttpResponseMessage(
else
{
// just dump the message as text
responseMessageProperty = new HttpResponseMessageProperty();
responseMessageProperty.StatusCode = httpStatusCode;
responseMessageProperty.StatusDescription = Fault.GetHttpStatusDescription(responseMessageProperty.StatusCode);
responseMessageProperty.Headers[HttpResponseHeader.ContentType] = "text/plain";

fault = Message.CreateMessage(
Expand All @@ -472,7 +481,7 @@ void BuildHttpResponseMessage(
faultDetails.DumpString());

fault.Properties.Add(HttpResponseMessageProperty.Name, responseMessageProperty);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(webFormat));
fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
}
}
#endregion
Expand Down
38 changes: 27 additions & 11 deletions Aspects/Wcf/Behaviors/WcfContextUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ public string OperationAction
{
get
{
if (OperationContext.Current == null)
if (HasWebOperationContext)
return WebOperationContext
.Current
.IncomingRequest
.UriTemplateMatch
.Data
.ToString();
else
if (HasOperationContext)
return OperationContext
.Current
.RequestContext
.RequestMessage
.Headers
.Action;
else
return null;

return OperationContext
.Current
.RequestContext
.RequestMessage
.Headers
.Action;
}
}

Expand All @@ -51,6 +59,13 @@ public MethodInfo OperationMethod
get
{
var operationAction = OperationAction;
var action = OperationContext
.Current
.EndpointDispatcher
.DispatchRuntime
.Operations
.FirstOrDefault(o => operationAction.Equals(o.Name, StringComparison.OrdinalIgnoreCase))
.Action;

return OperationContext
.Current
Expand All @@ -59,7 +74,7 @@ public MethodInfo OperationMethod
.Type
.GetMethods()
.FirstOrDefault(m => operationAction.Equals(m.Name, StringComparison.OrdinalIgnoreCase) ||
operationAction.Equals(m.GetCustomAttribute<OperationContractAttribute>()?.Action, StringComparison.OrdinalIgnoreCase));
action.Equals(m.GetCustomAttribute<OperationContractAttribute>()?.Action, StringComparison.OrdinalIgnoreCase));
}
}

Expand All @@ -75,7 +90,8 @@ public WebHttpBehavior WebHttpBehavior
.Current
.Host
.Description
.Behaviors
.Endpoints
.SelectMany(ep => ep.EndpointBehaviors)
.OfType<WebHttpBehavior>()
.FirstOrDefault();
}
Expand All @@ -100,7 +116,7 @@ public string GetFaultedAction(
.EndpointDispatcher
.DispatchRuntime
.Operations
.FirstOrDefault(o => o.Action.Equals(operationAction, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault(o => o.Name.Equals(operationAction, StringComparison.OrdinalIgnoreCase))
?.FaultContractInfos
?.FirstOrDefault(f => f.Detail == faultContractType)
?.Action
Expand Down

0 comments on commit 2a2d2bc

Please sign in to comment.