locked
Creating and Handling WCF Faults in Silverlight 3 RRS feed

  • Question

  • Has anyone succeeded in getting this running in a vb app?

    http://msdn.microsoft.com/en-us/library/dd470096(VS.96).aspx

    "Silverlight version 3 enables support for the Windows Communication Foundation (WCF) SOAP fault programming model, which allows the service to communicate error conditions to the client. In previous versions of Silverlight, if an error occurred on the service, it would register as an HTTP 500 response code and details about the error would not be accessible to the Silverlight client." ...

    Thursday, January 14, 2010 9:52 PM

Answers

  • Hi,

    I have a sample in C# http://cid-8890f90042412611.skydrive.live.com/self.aspx/ForAll/Faults.rar. I think there are no dependences of language.

    Friday, January 15, 2010 6:19 AM
  • Thanks for the project 

    I've translated the class to VB - below in case anyone else needs it and I've tried to implement it

    I get

    Invalid element in configuration. The extension name 'SilverlightFaults' is not registered in the collection at system.serviceModel/extensions/behaviorExtensions.

    On a LINQ query against an Entity Framework service.  So now I have to figure out how to stop EF using the SilverlightFaults or how to get it using it correctly.


    VB version 

     Public Class SilverlightFaultBehavior
        Inherits BehaviorExtensionElement : Implements IEndpointBehavior

        ' The following methods are stubs and not relevant.
        Public Sub AddBindingParameters(ByVal endpoint As ServiceEndpoint, _
                                        ByVal bindingParameters As Channels.BindingParameterCollection) _
            Implements System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters
        End Sub

        Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As EndpointDispatcher) _
            Implements System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior
            Dim inspector As New SilverlightFaultMessageInspector()
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector)
        End Sub

        Public Sub ApplyClientBehavior(ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) _
            Implements System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior
        End Sub
        Public Sub Validate(ByVal endpoint As ServiceEndpoint) _
            Implements System.ServiceModel.Description.IEndpointBehavior.Validate
        End Sub
        Public Overloads Overrides ReadOnly Property BehaviorType() As System.Type
            Get
                Return (GetType(SilverlightFaultBehavior))
            End Get
        End Property
        Protected Overloads Overrides Function CreateBehavior() As Object
            Return (New SilverlightFaultBehavior())
        End Function

        Public Class SilverlightFaultMessageInspector : Implements IDispatchMessageInspector

            Public Sub BeforeSendReply(ByRef reply As Channels.Message, _
                                       ByVal correlationState As Object) _
                    Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply
                Dim Aproperty As New HttpResponseMessageProperty()
                If reply.IsFault Then
                    ' Here the response code is changed to 200.
                    Aproperty.StatusCode = System.Net.HttpStatusCode.OK
                    reply.Properties(HttpResponseMessageProperty.Name) = Aproperty
                End If
            End Sub

            Public Function AfterReceiveRequest(ByRef request As Message, ByVal channel As IClientChannel, ByVal instanceContext As InstanceContext) As Object _
                    Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest
                ' Do nothing to the incoming message.
                Return (Nothing)
            End Function
        End Class
    End Class

     

     

    Friday, January 15, 2010 9:55 AM

All replies

  • Hi,

    I have a sample in C# http://cid-8890f90042412611.skydrive.live.com/self.aspx/ForAll/Faults.rar. I think there are no dependences of language.

    Friday, January 15, 2010 6:19 AM
  • Thanks for the project 

    I've translated the class to VB - below in case anyone else needs it and I've tried to implement it

    I get

    Invalid element in configuration. The extension name 'SilverlightFaults' is not registered in the collection at system.serviceModel/extensions/behaviorExtensions.

    On a LINQ query against an Entity Framework service.  So now I have to figure out how to stop EF using the SilverlightFaults or how to get it using it correctly.


    VB version 

     Public Class SilverlightFaultBehavior
        Inherits BehaviorExtensionElement : Implements IEndpointBehavior

        ' The following methods are stubs and not relevant.
        Public Sub AddBindingParameters(ByVal endpoint As ServiceEndpoint, _
                                        ByVal bindingParameters As Channels.BindingParameterCollection) _
            Implements System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters
        End Sub

        Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As EndpointDispatcher) _
            Implements System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior
            Dim inspector As New SilverlightFaultMessageInspector()
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector)
        End Sub

        Public Sub ApplyClientBehavior(ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) _
            Implements System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior
        End Sub
        Public Sub Validate(ByVal endpoint As ServiceEndpoint) _
            Implements System.ServiceModel.Description.IEndpointBehavior.Validate
        End Sub
        Public Overloads Overrides ReadOnly Property BehaviorType() As System.Type
            Get
                Return (GetType(SilverlightFaultBehavior))
            End Get
        End Property
        Protected Overloads Overrides Function CreateBehavior() As Object
            Return (New SilverlightFaultBehavior())
        End Function

        Public Class SilverlightFaultMessageInspector : Implements IDispatchMessageInspector

            Public Sub BeforeSendReply(ByRef reply As Channels.Message, _
                                       ByVal correlationState As Object) _
                    Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply
                Dim Aproperty As New HttpResponseMessageProperty()
                If reply.IsFault Then
                    ' Here the response code is changed to 200.
                    Aproperty.StatusCode = System.Net.HttpStatusCode.OK
                    reply.Properties(HttpResponseMessageProperty.Name) = Aproperty
                End If
            End Sub

            Public Function AfterReceiveRequest(ByRef request As Message, ByVal channel As IClientChannel, ByVal instanceContext As InstanceContext) As Object _
                    Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest
                ' Do nothing to the incoming message.
                Return (Nothing)
            End Function
        End Class
    End Class

     

     

    Friday, January 15, 2010 9:55 AM