Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF全局错误处理_C#_Wcf - Fatal编程技术网

C# WCF全局错误处理

C# WCF全局错误处理,c#,wcf,C#,Wcf,我试图使用IErrorHandler interfcae在WCF中实现全局错误处理,但我想返回(在未处理异常的情况下)一个简单字符串 (这是我的服务的返回类型)我如何实现?IErrorHandler中的providDefault方法将'fault'参数设置为Message type,并将Message type ToString()设置为生成类似XML的响应,因此这对我不起作用 (因为我的客户端希望获得特定格式的简单字符串) 谢谢根据 IErrorHandler使您能够显式控制生成的SOAP错误

我试图使用IErrorHandler interfcae在WCF中实现全局错误处理,但我想返回(在未处理异常的情况下)一个简单字符串 (这是我的服务的返回类型)我如何实现?IErrorHandler中的providDefault方法将'fault'参数设置为Message type,并将Message type ToString()设置为生成类似XML的响应,因此这对我不起作用 (因为我的客户端希望获得特定格式的简单字符串)

谢谢

根据

IErrorHandler使您能够显式控制生成的SOAP错误

它不用于更改服务的实际返回值

如果我理解正确,您的服务已经返回字符串。如果发生错误,您希望服务返回特定字符串

最简单的形式如下所示:

public string YourServiceMethod(int someInput)
{
    try
    {
        // Do whatever the method does
    }
    catch(Exception ex)
    {
        // Log the exception, probably?
        return "This is the specific error message I want to return."
    }
}
public class ErrorMessageInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        try
        {
            invocation.Proceed();
        }
        catch (Exception ex)
        {
            // log your exception
            invocation.ReturnValue = "Your specific error message.";
        }
    }
}
如果您希望避免在每个方法中编写相同的代码,实现这一点的一种方法是使用拦截器,例如使用

这意味着修改整个WCF服务以使用Windsor(或一些类似的DI框架),但出于许多其他原因,这是值得的。我写的每一个WCF服务都使用它。下面是一个关于如何在五分钟内将Windsor添加到WCF服务的示例

然后编写一个名为拦截器的类。它可能看起来像这样:

public string YourServiceMethod(int someInput)
{
    try
    {
        // Do whatever the method does
    }
    catch(Exception ex)
    {
        // Log the exception, probably?
        return "This is the specific error message I want to return."
    }
}
public class ErrorMessageInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        try
        {
            invocation.Proceed();
        }
        catch (Exception ex)
        {
            // log your exception
            invocation.ReturnValue = "Your specific error message.";
        }
    }
}
然后,当您向容器注册服务时,您将执行以下操作:

container.Register(
    Component.For<IInterceptor>().ImplementedBy<ServerLogInterceptor>()
       .Named("ErrorMessageInterceptor"));

container.Register(Component.For<IYourService,YourService>()
   .Interceptors("ErrorMessageInterceptor"));
container.Register(
Component.For().ImplementedBy()实现
。命名为(“ErrorMessageInterceptor”);
container.Register(Component.For())
.拦截器(“ErrorMessageInterceptor”);
其中,
IYourService
是您的服务接口,
YourService
是实际的服务类别

这意味着每次激活
YourService
的实例时,都会创建
ErrorMessageInterceptor
的实例。对服务方法的所有调用都被拦截器的
Intercept
方法“拦截”。它本质上是在方法调用周围放置一个
try/catch
,而不直接将其添加到原始方法中

如果你需要的话,你可以更具体一些。例如,拦截器可以检查
invocation.Method.ReturnType
,查看该方法是否返回字符串,然后只添加错误消息


你可以用拦截器做一些很棒的事情。在这样一个场景中,你尝试做一些小事情,这是一个尝试与他们合作的好机会。如果您在WCF看起来很奇怪之前没有使用DI,但同样地,一旦您尝试它,您可能会发现它是一种非常有用的模式。

如果我理解正确,您的服务方法已经返回了一个字符串。如果发生异常,您希望从服务返回一个特定字符串?@ScottHannen是的,完全正确。我想从全局错误处理程序中执行此操作。我可以提供一种方法来执行此操作,它不是从全局错误处理程序中执行的,但不要求您在每个服务方法中编写相同的代码。您好,对于这个想法,我真的不这么认为,但这是有意义的,我已经改变了我的代码使用城堡,所以它看起来是一个很好的选择,我的需要。