简体   繁体   English

WCF服务中的错误处理

[英]Error Handling in WCF Service

With the following service method example:- 使用以下服务方法示例:

[PrincipalPermission(SecurityAction.Demand, Role="BUILTIN\\Administrator")]
public string GetTest()
{
  try
  {
    return "Hello";
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

How do I get an error from the method when the caller is not in the correct Role. 当调用者的角色不正确时,如何从方法中获取错误。 In design time the error breaks on the method line (ie public string GetTest) and does not reach the catch. 在设计时,错误在方法行(即公共字符串GetTest)上中断,并且没有达到目的。 At run time it is reported in my silverlight application as an unhandled error (I have try.. catch blocks there too). 在运行时,它在我的Silverlight应用程序中报告为未处理的错误(我在那里也尝试过.. catch块)。 There doesn't seem to be a place to catch the error as it never gets into the try blocks!! 似乎没有发现错误的地方,因为它永远不会进入try块!

The check for the role is made (by the WCF runtime) before the method is actually called - not inside the method! 在实际调用方法之前 (由WCF运行时)检查角色,而不是在方法内部!

You need to handle this exception on the caller's side when you make this call. 进行此呼叫时,您需要在呼叫者一方处理此异常。

If you need to check certain conditions inside your service code, don't decorate the method with an attribute, but instead use the role provider in code to check for a given condition. 如果您需要检查服务代码中的某些条件,请不要使用属性来修饰该方法,而应使用代码中的角色提供程序来检查给定条件。

If you want global error handler for your WCF service you can implement IErrorHandler and add it in custom behavior. 如果要为WCF服务使用全局错误处理程序,则可以实现IErrorHandler并将其添加到自定义行为中。 Operation can't catch exceptions thrown outside of its try block. 操作无法捕获在try块之外抛出的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM