0

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 (i.e. public string GetTest) and does not reach the catch. At run time it is reported in my silverlight application as an unhandled error (I have try.. catch blocks there too). There doesn't seem to be a place to catch the error as it never gets into the try blocks!!

EzaBlade
  • 657
  • 1
  • 9
  • 23

2 Answers2

2

The check for the role is made (by the WCF runtime) before the method is actually called - not inside the method!

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Aaah so you mean on proxy.GetTestAsynch(). I have no error handling there, I have it currently on the callback event (proxy_GetTestCompleted(...). Thanks. I am at home right now so I will try this immediately at work tomorrow. – EzaBlade Feb 28 '11 at 21:31
  • I tried that but it doesn't work. I still get an unhandled error. I have try..catch on the call to the service and on the callback event neither of which catch the error. It always ends up in the Application_UnhandledException event of the App.xaml.cs module which I don't want. That is for unforeseen errors only. – EzaBlade Mar 01 '11 at 09:18
1

If you want global error handler for your WCF service you can implement IErrorHandler and add it in custom behavior. Operation can't catch exceptions thrown outside of its try block.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks. I did look at this briefly but wanted to get the simple attribute version per method working and understood first. I like to try to understand things in a heirarchical fashion with the easier (but not always the best) method first. I will be trying that also thanks for the suggestion. – EzaBlade Feb 28 '11 at 21:32