WCF Exception Shielding

Topics: Building and extending application blocks, Caching Application Block , General discussion
Jan 26, 2010 at 2:20 PM

The below is my exception handling block in web.config

<exceptionHandling>
    <exceptionPolicies>
      <add name="WCF Exception Shielding">
        <exceptionTypes>
          <add type="System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="InvalidOperationException">
            <exceptionHandlers>
              <add exceptionMessage="Invalid operation!!" faultContractType="WcfServiceTest.ServiceContracts.ServiceFault, WcfServiceTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Throw ServiceFault">
                <mappings>
                  <add source="{Guid}" name="Id" />
                  <add source="{Message}" name="MessageText" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>

        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>


The below is my Fault contract used for handling exceptions.
 [DataContract]
    public class ServiceFault
    {
        public ServiceFault()
        {

        }
        public ServiceFault(string message)
        {
            MessageText = message;
        }

        public ServiceFault(Guid newId, string message)
        {
            id = newId;
            MessageText = message;
        }

        private string message;
        private Guid id;

        [DataMember]
        public string MessageText
        {
            get { return message; }
            set { message = value; }
        }

        [DataMember]
        public Guid Id
        {
            get { return id; }
            set { id = value; }
        }
    }

and the below is my code in Webservice. If I throw an invalid exception, it is received as a system exception and it is not converted to FaultContract which i have created.

But if i throw a fault exception with the fault contract i have created, it is caught perfectly.

 public int CreateOrder(string currency, double amount)
        {
                //throw new InvalidOperationException("Cannot call this operation!");
                        ServiceFault fault = new ServiceFault(Guid.NewGuid(), "Cannot call this operation!");
            
            throw new FaultException<ServiceFault>(fault, new FaultReason("Invalid Operation"), FaultCode.CreateReceiverFaultCode(new FaultCode("Create Order")));
            

        }

and finally this is the code in my client sample application.

 class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Press any key when the service is listening...");
                Console.ReadLine();


                try
                {
                    // Create an instance of the service proxy
                    OrdersServiceClient proxy = new OrdersServiceClient();
                  
                    int orderID = proxy.CreateOrder("USD", 3.0);
                  
                }
                catch (FaultException<ServiceFault> ex)
                {
                    ServiceFault fault = ex.Detail;
                    Console.WriteLine(fault.MessageText);
                    Console.WriteLine(fault.Id);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.WriteLine("Press any key to close...");
                Console.ReadLine();
            }
        }
    }

What is that i am missing to handle System exceptions? Please help me out.

Thanks in advance.

Jan 27, 2010 at 1:56 AM

Did you put the ExceptionShielding attribute in your service interface?

 

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

Jan 27, 2010 at 11:52 AM

thanks for you reply.

Yes, I have added it here is code for it.

  [ServiceContract]
    [ExceptionShielding]
    public interface IOrdersService
    {
        [OperationContract]
        [FaultContract(typeof(ServiceFault))]
        string GetData(int value);

        [OperationContract]
        [FaultContract(typeof(ServiceFault))]
        int CreateOrder(string currency, double amount);
    }

as the name of the application block is "WCF Exception Shielding" by default, i have not passed this value as a parameter to the ExceptionShielding attribute.

 

Jan 28, 2010 at 1:54 AM

I can't figure out yet what could be wrong, you could send me your sample solution if you want so I could take a look or if you haven't read this, you may also want to check it out first. http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/04/07/Shielding-WCF-Services-with-Exception-Handling-Application-Block-_2D00_-Part-1.aspx

 

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

Jan 28, 2010 at 10:30 AM
Hi,
Thanks for your reply. I have checked the link you have provided long
back and started doing the sample application.
I forgot to mention that i am using Visual Studio 2008. As the sample
project provided in the link is in VS2005.

Attached is my sample application. Please let me know if you can find
the solution.

The application works when i throw a Fault Exception but does not work
when i throw a System exception-- InvalidOperationException.

Please help me out.

Thanks,
Jan 29, 2010 at 2:01 AM

Where's the sample app? It's not in our inbox...

 

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

Jan 29, 2010 at 9:49 AM
Please find the attachment. I have attached this using googlemail. and
it is a .rar file.
if you want me to forward it to any other id, please let me know

Thanks,
Jan 29, 2010 at 9:55 AM

I still haven't received it, did you check if you got any mail delivery failure?  I usually can't send .rar or .zip files to gmails.   Try sending it in .zip file to sarah.b.urmeneta@avanade.com

 

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

Jan 29, 2010 at 10:04 AM
no failure deliveries in my inbox. It is strange!!
Now i am sending .zip file

Please let me know whether you have received it or not.

Thanks


On 29 January 2010 09:56, AvanadeSupport <notifications@codeplex.com> wrote:
> From: AvanadeSupport
>
> I still haven't received it, did you check if you got any mail delivery
> failure?  I usually can't send .rar or .zip files to gmails.   Try sending
> it in .zip file to [email removed]
>
>
>
> Sarah Urmeneta
> Global Technology and Solutions
> Avanade, Inc.
> [email removed]
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe on CodePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at CodePlex.com
Jan 29, 2010 at 10:05 AM

Ok, got it, guess it wants a .zip file.  I'll look into this now.

 

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

Jan 29, 2010 at 10:23 AM

Found it, you set the includeExceptionDetailInFaults to false but in your ServiceBehavior attribute, you set it to true.  Remove the IncludeExceptionDetailInFaults parameter and everything works as expected.

 

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

Jan 29, 2010 at 10:45 AM

IF I remove the parameter, i receive a "Communication Exception"

It says: "The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."

 

Jan 29, 2010 at 10:51 AM

Really? I'll send you the working copy and let me know.

 

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

Jan 29, 2010 at 10:54 AM
yes, sure.

out of curiosity, are you working in the Vs2008 or 2005??

because i am working in VS2008

On 29 January 2010 10:52, AvanadeSupport <notifications@codeplex.com> wrote:
> From: AvanadeSupport
>
> Really? I'll send you the working copy and let me know.
>
>
>
> Sarah Urmeneta
> Global Technology and Solutions
> Avanade, Inc.
> [email removed]
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe on CodePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at CodePlex.com
Jan 29, 2010 at 10:59 AM

Yes I'm using vs2008.  I've sent it, pls try it out.

Jan 29, 2010 at 2:43 PM

Thanks for your help.. You have been very patient answering my questions.

The version of dlls I was using( 3.0.0) varied from the version of dlls you sent me ( 4.1 )

so, i got the latest version and used the Specific port assign option instead of Auto assign port option

for more details check this link to set Specific port assign option.

http://forums.asp.net/t/1154035.aspx

Thanks a lot Sarah!!

 

 

Feb 2, 2010 at 4:04 AM

Are you still unable to resolve this?  Regarding on your last email, yes,  I was able to catch a FaultException when throwing an InvalidOperation Exception on the service.  Were you able to change the port specified in your client's configuration based on what port is being used by the service? 

 

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

Feb 2, 2010 at 9:55 AM
I am still facing the same issue throwing an Invalid operation exception on the service and catch a fault exception.
I am not sure whether i have configured the web.config properly, or where i am doing wrong.. But it says

"'System.ServiceModel.FaultException`1' occurred in WCFclienttest.DLL but was not handled in user code"
 Please help me solving the problem


I have changed the port with the below steps and assigned a constant port number instead of using dynamic ports.

 For the Web Site project, we can fix the port number by following steps:

    1.  Click the project in Solution Explorer.
    2.  Open “Properties” panel.
    3.  Set “Use dynamic ports” property to false and assign a value to "Port number".


On 2 February 2010 04:04, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Are you still unable to resolve this?  Regarding on your last email, yes,  I was able to catch a FaultException when throwing an InvalidOperation Exception on the service.  Were you able to change the port specified in your client's configuration based on what port is being used by the service? 

 

Sarah Urmeneta


Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 10:00 AM

In the last copy of the solution I sent you, what error are you getting? How did you come up with the new error message you're getting now?

 

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

Feb 2, 2010 at 10:04 AM
I have added 4.1 version dlls and removed IncludeExceptionDetailsinFaults paramenter in service behaviour attribute.

and I have followed the steps mentioned in the below link to avoid the unhandled exception.

http://sergecalderara.wordpress.com/2008/11/25/systemservicemodelfaultexception1-was-unhandled-by-user-code/

then i received this New error message.




On 2 February 2010 10:01, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

In the last copy of the solution I sent you, what error are you getting? How did you come up with the new error message you're getting now?

 

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

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 10:14 AM

Where do you exactly see the exception?  Is it in the service code?  Are you running on debug mode? 

 

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

Feb 2, 2010 at 10:24 AM
yes. I see this in the service code. It is in the debug mode.

On 2 February 2010 10:14, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Where do you exactly see the exception?  Is it in the service code?  Are you running on debug mode? 

 

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

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 10:28 AM

I think its just a halt when your code throws the exception, if you continue to press F5 after you got that error message, you'll see the client will be able to catch it.  Or try running running without debugging (Ctrl F5), you'll get the output on your screen, the fault's MessageText and Id properties.

 

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

Feb 2, 2010 at 10:32 AM
No, I am still not able to get the result.

i am getting this

Press any key when the service is listening...


Unhandled Exception: System.ServiceModel.FaultException`1[WCFClientTest.ServiceR
eference1.GenericFault]: Invalid Operation (Fault Detail is equal to WCFClientTe
st.ServiceReference1.GenericFault).
Press any key to close...


On 2 February 2010 10:28, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

I think its just a halt when your code throws the exception, if you continue to press F5 after you got that error message, you'll see the client will be able to catch it.  Or try running running without debugging (Ctrl F5), you'll get the output on your screen, the fault's MessageText and Id properties.

 

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

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 10:33 AM

Could you send me your latest copy of this?

Feb 2, 2010 at 10:45 AM
yes.

Here it is.. Please let  me know if you need anything

if you solve it, could you please send me that version? so that even i can see it?

On 2 February 2010 10:33, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Could you send me your latest copy of this?

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 10:48 AM

I will.  I trust you sent it in .zip format? It's not yet on my inbox.

 

Feb 2, 2010 at 11:17 AM
Yes, i sent it in .zip format. Now i have forwarded it again.
Please let me know whether you received it ot not.


On 2 February 2010 10:48, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

I will.  I trust you sent it in .zip format? It's not yet on my inbox.

 

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 11:24 AM

You're throwing FaultException<GenericFault> but you're catching for a FaultException<ServiceFault>

Feb 2, 2010 at 11:35 AM
I was trying all possible ways... to solve it...

On 2 February 2010 11:24, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

You're throwing FaultException<GenericFault> but you're catching for a FaultException<ServiceFault>

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 11:39 AM
Edited Feb 2, 2010 at 11:52 AM

Change your catch to FaultException<GenericFault>, that'll solve it.  If you encounter again the exception above regarding the user code wasn't able to handle it, press F5 and see if it proceeds in the catch block.

Feb 2, 2010 at 11:52 AM
I have modified to catch to FaultException<GenericFault>.

hmm... Now  i am getting this.

An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll

Additional information: The underlying connection was closed: The connection was closed unexpectedly.




On 2 February 2010 11:39, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Change your catch to FaultException<GenericFault>, that'll solve it. 

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 11:54 AM

Are you throwing a FaultException in the service or did you changed it to InvalidOperationException?  I'm guessing its the latter.

Feb 2, 2010 at 12:01 PM
I am throwing the Fault exception. I have NOT changed it to Invalid operationException.

On 2 February 2010 11:54, AvanadeSupport <notifications@codeplex.com> wrote:

From: AvanadeSupport

Are you throwing a FaultException in the service or did you changed it to InvalidOperationException?  I'm guessing its the latter.

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Feb 2, 2010 at 12:04 PM

I really have no idea now.  We have the same copy of the solution, changed the same thing, but get a different result.  Are you sure you didn't changed anything aside from that? 

Feb 4, 2010 at 11:52 AM

Hi,

When i tried to execute this same application in my colleague's PC, it worked exactly the way I needed. so, i think there is some wrong with the system settings ( I am not sure what is wrong!!)

I tried in other PC, again it is not working. So, I have stopped using Exception shielding as it is dependent on system settings and I don't want to take any chances while deploying it.

To achieve this, manually, I am going  to throw the Fault Exceptions instead of system exceptions which we might not need Exception Shielding.

Thanks for the support this forum gave me. I learnt new things.