Error with Wcf Exception Handling block

Topics: Exception Handling Application Block
Oct 21, 2011 at 3:43 PM

Hi All,

I have created Exception Block with my WCF application and I am getting following error when I try to have my own Fault Contract Class. The .dll is throwing following error

Fault Contract Exception Handler ->
Fault Contract Type ->
Unable to find type <myExceptionClassName> Public key token=null
Warning

<DataContract()> _
<Serializable()> _
Public Class PeopleException   
 Private message As String
    Private m_id As Guid

    <DataMember()> _
    Public Property MessageText() As String
        Get
            Return message
        End Get

        Set(value As String)
            message = value
        End Set
    End Property
    <DataMember()> _
    Public Property Id() As Guid
        Get
            Return m_id
        End Get

        Set(value As Guid)
            m_id = value
        End Set
    End Property
End Class

But, when I integrate the same with Fault Contract Exception Handler, this throws the error that i mentioned. Anyhow, I must implement this Exception block.

I have done the parameter mapping as well. I also added <ExceptionShielding()> attribute. but still its not workng.

Pls. help. Its urgent

Oct 22, 2011 at 5:03 AM
Edited Oct 24, 2011 at 8:38 AM

What is the myExceptionClassName class?

You also need to add references to:

  • Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
  • Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.Unity.dll
  • Microsoft.Practices.Unity.Interception.dll
  • Microsoft.Practices.ServiceLocation.dll

Then your code would look something like:

 

Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF

<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    <FaultContract(GetType(PeopleException))>
    Function GetData(ByVal value As Integer) As String

End Interface

<ExceptionShielding("Policy")>
Public Class Service1
    Implements IService1

    Public Sub New()
    End Sub

    Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData
        Throw New Exception("OOPS!")        
    End Function

End Class

And the configuration file would be something like:
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessageResourceType="" faultContractType="WcfService1.PeopleException, WcfService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="Message" name="MessageText" />
                  <add source="Guid" name="Id" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>

 

I hope that points you in the right direction.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Oct 24, 2011 at 7:49 AM

Thanks for your reply. I m using Ent. Lib. v5

Ya.. in my last mail, I mistakenly typed wrong class name. Its Unable to find type <PeopleException> Public key token=null
Warning is the right one.

I implemented the steps as mentioned above. In the catch block, I mentioned the code line as

Catch Staging_Exception As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Staging_Exception, "Policy")

1. On hitting this, it gives me Unable to handle exception: 'FaultContractExceptionHandler'. Is this the right behavoiur? if yes, how do I cross-check this? If not, what else need to be done in the code?

Sometimes, it takes me to a page "No Source Available". It gives Source File Information as :

Locating source for 'e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs'. Checksum: MD5 {ec 3a 78 29 df dc 37 19 9e eb ca eb 47 1c 53 b8}
The file 'e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs' does not exist.
Looking in script documents for 'e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs'...
Looking in the projects for 'e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs'.
The file was not found in a project.
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs.
The debugger could not locate the source file 'e:\Builds\EntLib\Latest\Source\Blocks\ExceptionHandling\Src\ExceptionHandling\ExceptionPolicy.cs'.

2. In your reply, you mentioned {HandlingInstanceId} as the mapping name. Why it cant be {Id}? And why MessageText is not put as {MessageText}?

Thnx for your inputs.

Oct 24, 2011 at 8:35 AM

If you are using Exception Shielding attributes then you don't really need to explicitly catch the exception (that's the nice part of the exception shielding implementation). 

I wouldn't expect to see Unable to handle exception: 'FaultContractExceptionHandler'.  You might see "FaultContractWrapperException was not handled by user code" if you are debugging the service.

Brace brackets are used as tokens that Enterprise Library substitutes in messages.  Actually, I don't think that was required in this case.  It should have been something like:

                <mappings>
                  <add source="Message" name="MessageText" />
                  <add source="Guid" name="Id" />
                </mappings>

If you are still having problems then please post your config and your code.


Thanks,

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Oct 24, 2011 at 9:17 AM

Thnx for your views again. My code goes below..

<ServiceContract()>
Public Interface Emp

    <OperationContract(), FaultContract(GetType(Service_Exception))> _
    Function Put(ByVal Emp As EmpClass) As Boolean

End Interface

<DataContract()> _
Public Class Service_Exception
    Private message As String
    Private m_id As Guid

    Public Sub New(ByVal message As String)
        Me.MessageText = message
    End Sub

    <DataMember()> _
    Public Property MessageText() As String
        Get
            Return message
        End Get

        Set(value As String)
            message = value
        End Set
    End Property
    <DataMember()> _
    Public Property Id() As Guid
        Get
            Return m_id
        End Get

        Set(value As Guid)
            m_id = value
        End Set
    End Property
End Class

Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF

Imports System.Data.SqlClient
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

<ExceptionShielding("Policy")> _
Public Class EmpService : Implements Emp

 Public Function Put(Staging_Emp As EmpClass) As Boolean Implements People.Put

Try
            Dim i As Integer = 2
            Dim j As Integer = 0
            Dim k As Integer = i / j
Catch Staging_Exception As Exception
            'Dim rethrow As Boolean = ExceptionPolicy.HandleException(Staging_Exception, "Policy")
End Try

Web.config:
<exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessageResourceType="" faultContractType="PNMR.DMS.Service_Exception, People, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="Message" name="MessageText" />
                  <add source="Id" name="{HandlingInstanceId}" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Thanks,

 

Oct 24, 2011 at 9:46 AM

I still face same issues

 

Oct 25, 2011 at 5:01 AM
Edited Oct 29, 2011 at 4:57 AM

Your postHandlingAction in the configuration needs to be "ThrowNewException" and not "NotifyRethrow".

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessageResourceType="" faultContractType="WcfService1.Service_Exception, WcfService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="Message" name="MessageText" />
                  <add source="Guid" name="Id" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

And the code should look like:

Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF

<ExceptionShielding("Policy")>
Public Class EmpService
    Implements Emp

    Public Sub New()
    End Sub

    Public Function Put(Staging_Emp As EmpClass) As Boolean Implements Emp.Put
        Dim i As Integer = 2
        Dim j As Integer = 0
        Dim k As Integer = i / j

        Return True
    End Function

End Class

There isn't a need to catch the exception explicitly since the exception shielding is going to catch and handle it for you.  

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Oct 28, 2011 at 7:31 AM

Thanks.. Its workign as intended..!