Skip to content

Commit

Permalink
fixed some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vmelamed committed Feb 1, 2016
1 parent 74b2da7 commit f8c1bc2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
20 changes: 13 additions & 7 deletions Aspects/Wcf/Behaviors/ExceptionShieldingErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Net;
using System.Net.Mime;
using System.Reflection;
Expand Down Expand Up @@ -75,7 +76,7 @@ public ExceptionShieldingErrorHandler(
/// <param name="version">The SOAP version of the message.</param>
/// <param name="fault">The <see cref="Message"/> object that is returned to the client, or service, in the duplex case.</param>
[SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "exceptionToThrow is evaluated two times.")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "As designed. Core feature of the block.")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "As designed. Core feature of the handler.")]
public void ProvideFault(
Exception error,
MessageVersion version,
Expand Down Expand Up @@ -177,7 +178,7 @@ public bool HandleError(Exception error)

#region Internal Implementation

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "As designed. Core feature of the block.")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "As designed. Core feature of the handler.")]
void HandleFaultWrapper(
FaultContractWrapperException faultContractWrapper,
ref Message fault)
Expand Down Expand Up @@ -221,6 +222,7 @@ void HandleFaultWrapper(
}
}

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "As designed. Core feature of the handler.")]
void HandleFaultException(
FaultException faultException,
ref Message fault)
Expand Down Expand Up @@ -295,6 +297,7 @@ void HandleFault(
if (_wcfContext.HasWebOperationContext)
BuildHttpResponseMessage(
string.Format(
CultureInfo.InvariantCulture,
"An error has occurred while consuming this service. Please contact your administrator for more information. Error ID: {0}",
GetHandlingInstanceId(error, handlingInstanceId)),
fault.Headers.Action,
Expand All @@ -305,9 +308,12 @@ void HandleFault(
fault.Version,
new FaultException(
new FaultReason(
string.Format(
"An error has occurred while consuming this service. Please contact your administrator for more information. Error ID: {0}",
GetHandlingInstanceId(error, handlingInstanceId))),
new FaultReasonText(
string.Format(
CultureInfo.InvariantCulture,
"An error has occurred while consuming this service. Please contact your administrator for more information. Error ID: {0}",
GetHandlingInstanceId(error, handlingInstanceId)),
CultureInfo.InvariantCulture)),
FaultCode.CreateReceiverFaultCode(
SoapException.ServerFaultCode.Name,
SoapException.ServerFaultCode.Namespace))
Expand Down Expand Up @@ -381,7 +387,7 @@ WebContentFormat GetWebContentFormat()
return format;

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

if (webGet != null && webGet.IsResponseFormatSetExplicitly)
Expand All @@ -393,7 +399,7 @@ WebContentFormat GetWebContentFormat()
return webInvoke.ResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;

// 4. The default format setting in the WebHttpBehavior.
var webBehavior = _wcfContext.GetWebHttpBehavior();
var webBehavior = _wcfContext.WebHttpBehavior;

if (webBehavior != null)
return webBehavior.DefaultOutgoingResponseFormat == WebMessageFormat.Json ? WebContentFormat.Json : WebContentFormat.Xml;
Expand Down
9 changes: 3 additions & 6 deletions Aspects/Wcf/Behaviors/IWcfContextUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ public interface IWcfContextUtilities
/// <summary>
/// Gets the operation action.
/// </summary>
/// <returns>The action.</returns>
string GetOperationAction();
string OperationAction { get; }

/// <summary>
/// Gets the method corresponding to the operation action.
/// </summary>
/// <returns>The method.</returns>
MethodInfo GetOperationMethod();
MethodInfo OperationMethod { get; }

/// <summary>
/// Gets the <see cref="WebHttpBehavior"/>.
/// </summary>
/// <returns>The <see cref="WebHttpBehavior"/>.</returns>
WebHttpBehavior GetWebHttpBehavior();
WebHttpBehavior WebHttpBehavior { get; }

/// <summary>
/// Gets the fault action either from the fault's type or if not specified, from the action in the current <see cref="OperationContext"/>.
Expand Down
65 changes: 37 additions & 28 deletions Aspects/Wcf/Behaviors/WcfContextUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,59 @@ public class WcfContextUtilities : IWcfContextUtilities
/// Gets the action.
/// </summary>
/// <returns>The fault action.</returns>
public string GetOperationAction()
public string OperationAction
{
if (OperationContext.Current == null)
return null;
get
{
if (OperationContext.Current == null)
return null;

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

/// <summary>
/// Gets the method corresponding to the operation action.
/// </summary>
/// <returns>The method.</returns>
public MethodInfo GetOperationMethod()
public MethodInfo OperationMethod
{
var operationAction = GetOperationAction();
get
{
var operationAction = OperationAction;

return OperationContext
.Current
.EndpointDispatcher
.DispatchRuntime
.Type
.GetMethods()
.FirstOrDefault(m => operationAction.Equals(m.Name, StringComparison.OrdinalIgnoreCase) ||
operationAction.Equals(m.GetCustomAttribute<OperationContractAttribute>()?.Action, StringComparison.OrdinalIgnoreCase));
return OperationContext
.Current
.EndpointDispatcher
.DispatchRuntime
.Type
.GetMethods()
.FirstOrDefault(m => operationAction.Equals(m.Name, StringComparison.OrdinalIgnoreCase) ||
operationAction.Equals(m.GetCustomAttribute<OperationContractAttribute>()?.Action, StringComparison.OrdinalIgnoreCase));
}
}

/// <summary>
/// Gets the web HTTP behavior.
/// </summary>
/// <returns>The WebHttpBehavior.</returns>
public WebHttpBehavior GetWebHttpBehavior()
public WebHttpBehavior WebHttpBehavior
{
return OperationContext
.Current
.Host
.Description
.Behaviors
.OfType<WebHttpBehavior>()
.FirstOrDefault();
get
{
return OperationContext
.Current
.Host
.Description
.Behaviors
.OfType<WebHttpBehavior>()
.FirstOrDefault();
}
}

/// <summary>
Expand All @@ -80,7 +89,7 @@ public WebHttpBehavior GetWebHttpBehavior()
public string GetFaultedAction(
Type faultContractType)
{
var operationAction = GetOperationAction();
var operationAction = OperationAction;

// for unhandled exception use the operation action
if (operationAction == null || faultContractType == null)
Expand Down
6 changes: 3 additions & 3 deletions Aspects/Wcf/Tests/ExceptionShieldingErrorHandlerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class NoContext : IWcfContextUtilities

public string GetFaultedAction(Type faultContractType) => null;

public string GetOperationAction() => null;
public string OperationAction => null;

public MethodInfo GetOperationMethod() => null;
public MethodInfo OperationMethod => null;

public WebHttpBehavior GetWebHttpBehavior() => null;
public WebHttpBehavior WebHttpBehavior => null;
}

[TestMethod]
Expand Down

0 comments on commit f8c1bc2

Please sign in to comment.