C# 是否有更好的例外防护罩替代品?
我们公司正在从C# 是否有更好的例外防护罩替代品?,c#,wcf,enterprise-library,C#,Wcf,Enterprise Library,我们公司正在从Microsoft.Practices.Enterprise.Library日志记录和异常处理转换为使用Log4Net。我删除了所有标准的日志记录和异常处理调用,并将它们替换为等价的Log4Net,但是当我删除实践DLL时,我注意到我们正在使用它们进行异常屏蔽: using System; using System.ServiceModel; using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF; na
Microsoft.Practices.Enterprise.Library
日志记录和异常处理转换为使用Log4Net
。我删除了所有标准的日志记录和异常处理调用,并将它们替换为等价的Log4Net
,但是当我删除实践DLL时,我注意到我们正在使用它们进行异常屏蔽:
using System;
using System.ServiceModel;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF;
namespace CRM.WCFServices.LeadIntegration.ServiceContracts
{
/// <summary>
/// Interface for systems external to CRM to interact with outbound calls.
/// </summary>
[ExceptionShielding("WCF Exception Shielding")]
[ServiceContract]
public interface IOutboundIntegrationService
{
#region Contracts
[OperationContract]
[FaultContract(typeof (Common.Services.WCF.FaultContracts.ServiceFault))]
void SendOutboundData(string entityName, Guid entityId, DateTime modifiedOn, Guid modifiedBy,
string crmOrganization);
#endregion
}
}
使用系统;
使用System.ServiceModel;
使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF;
命名空间CRM.WCFServices.LeadIntegration.ServiceContracts
{
///
///CRM外部系统与出站呼叫交互的接口。
///
[例外屏蔽(“WCF例外屏蔽”)]
[服务合同]
公共接口IOutboundIntegrationService
{
#区域合同
[经营合同]
[FaultContract(typeof(Common.Services.WCF.FaultContracts.ServiceFault))]
void SendOutboundData(字符串entityName、Guid entityId、DateTime modifiedOn、Guid modifiedBy、,
字符串组织);
#端区
}
}
基本问题是“我如何才能安全地移除它?”这涉及到回答以下大部分问题:
如果我没记错的话,这个功能的目的是
includeExceptionDetailInFaults
设置为false,或者在服务方法周围放置try/catch块:
try
{
// service code
}
catch (Exception ex)
{
// Log the exception
throw new FaultException("Unknown service failure");
}
在接口上声明什么工作?你是指我发布的代码吗?显然,该代码需要存在于服务操作代码本身中。你可以想象使用PostSharp注入上述代码,但它的内容很少,只需输入到每个操作中,可能是从代码段插入的。我说的是我的代码。我认为接口不可能应用任何实现,而且如果实现接口提供异常处理,对我来说似乎有点可疑。通常情况下,为什么接口应该与行为有关?您碰巧看到了一个示例,其中属性被放置在接口上。我确信它也是如果属性在服务类上,则可以使用。我原以为服务类上需要type属性。我想这是一个示例,说明了如何使用接口通过属性实际提供实现。属性不提供实现-它只提供元数据。其他一些代码(企业库)查看元数据并提供实现。出于同样的目的,它可以很容易地读取XML配置文件。