HandlingInstanceID

Topics: Exception Handling Application Block
Mar 18, 2011 at 1:09 PM

Hi

 

The HandlingInstanceID giveds the system generated number.

I need to have specific error num for each specific exception how can i achive this. In exception handling block

Mar 18, 2011 at 1:21 PM
Edited Mar 18, 2011 at 1:21 PM

It seems you have the same question as with the one you posted before.  May I ask why you don't want the suggestion given there? 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 18, 2011 at 2:14 PM

I need to throw back this id to the ui also.I am using the Replace handle to replace the original message with custom message and propogate to the ui.i want this error number to be available for me in the front end.

Mar 18, 2011 at 2:25 PM

Then why not create custom exceptions which will contain a property to hold the error Id?  You would use the custom exception then with the Replace Handler.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 18, 2011 at 2:39 PM

can you please send me a sample please.

Mar 18, 2011 at 2:49 PM

Well, you just need to create a class inherting from the Exception class:

public class MyCustomException : Exception
{
       public MyCustomException()
       {
               ErrorID = 1000;
       }

       public in ErrorId { get; set; }
}

And then you'll specify this as the exception for the Replace Exception Type of the replace handler.  It's up to you if you want to create a custom exception for each exception type or if you simply want to distinguish each exception type based on the ErrorID.  The code above is just a simple example.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 20, 2011 at 11:04 AM

Thanks. I am doing this. in my dal and bal.

Now in service layer how do i convert this custom exception caught and thrown from the dal/bal to fault contract exception and retain the errorid.

also how to achive custom errorid in service layer(wcf) and convert it to fault contract and throw back to UI.

 

 

Mar 21, 2011 at 2:07 AM
Edited Mar 21, 2011 at 4:09 AM

Follow the instructions mentioned in this blog.  You can add an ErrorId property to your custom fault contract where you can put your custom error id.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 21, 2011 at 7:41 AM

Following is a custom class which will resolve the ExceptionManger and call the HandleException to  call the respective policy to handle the exception.

what if exception happens in this class. what we need to do.And what are the exception that can be caught here.

public static bool HandleExcetion(ref Exception ex)
         {
             Exception exceptionToRethrow;
            bool rethrow = false;
            try
            {
                if (ex is BaseException)
                {
                    // do nothing as Data Access or Business Logic exception has already been logged / handled
                }
                else
                {
                    unityContainer = new UnityContainer().AddNewExtension<EnterpriseLibraryCoreExtension>();

                    exManager = unityContainer.Resolve<ExceptionManager>();

                  //  rethrow = ExceptionPolicy.HandleException(ex, "UserInterfacePolicy");
                    rethrow = exManager.HandleException(ex, "UserInterfacePolicy", out exceptionToRethrow);
                }
            }
            catch (Exception exp)
            {
                what i need to impleament here or how i can use the enterprise library here

            }
            return rethrow;
        }

Mar 21, 2011 at 8:19 AM

Ok, have you checked out the link I posted regarding the blog on how to implement exception shielding?  Have you tried it out yet?  When using exception shielding in WCF, you need not enclose your code in a try catch block.  the exception policy you specified in the [Exception Shielding("UserInterfacePolicy")] will automatically be applied when an exception occurs.  In the case when you didn't specify any exception policy name [ExceptionShielding()], it looks up an exception policy named "WCF Exception Shielding". 

Identify the possible exceptions which might occur in your code and if you want specific error id for each type of exception, the easiest way I could think of to have that kind of behavior is to create a specific fault contract for each type of exception that could occur; otherwise, you would need to create a custom faultcontract exception handler.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 21, 2011 at 9:41 AM

This link explains who apply EHAB for exception that happens in service. wht if exception happens in BAL and is propogated to service.

In service i just need to convert this to Faultcontract type and porpoget to UI.

Also

 

Following is a custom class which will resolve the ExceptionManger and call the HandleException to  call the respective policy to handle the exception.

what if exception happens in this class. what we need to do.And what are the exception that can be caught here.

public static bool HandleExcetion(ref Exception ex)
         {
             Exception exceptionToRethrow;
            bool rethrow = false;
            try
            {
                if (ex is BaseException)
                {
                    // do nothing as Data Access or Business Logic exception has already been logged / handled
                }
                else
                {
                    unityContainer = new UnityContainer().AddNewExtension<EnterpriseLibraryCoreExtension>();

                    exManager = unityContainer.Resolve<ExceptionManager>();

                  //  rethrow = ExceptionPolicy.HandleException(ex, "UserInterfacePolicy");
                    rethrow = exManager.HandleException(ex, "UserInterfacePolicy", out exceptionToRethrow);
                }
            }
            catch (Exception exp)
            {
                what i need to impleament here or how i can use the enterprise library here

            }
            return rethrow;
        }

 

Thanks for your replies.

Mar 21, 2011 at 10:10 AM

Since it was eventually propagated in the WCF service, exception shielding would still apply to it.  You can try it out.  As for your question on what are the possible exception, one is ActivationException in the case that you specified an exception policy name which doesn't exists in your config.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 21, 2011 at 10:25 AM

Tried it is not work.In the UI it is being caught on heneral exception rather than the FaultException<ClientSvc.ServiceFault> ex.

Also if the error was propogated to service from BAl i want to have different policy and for exception that occur in the service i want to apply different policy.

How can this be done.


Mar 21, 2011 at 10:32 AM

If you're sure you followed all the instructions from the blog I posted, could you send a small repro?

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 21, 2011 at 11:26 AM

Can we have different policies for a Service.if yes how.

Mar 22, 2011 at 1:42 AM

You can't, but you can have your exception policy handle multiple exceptions.  Please create a new discussion thread for each different question.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com