سؤال

I am trying to call a WCF service from a Silverlight client using channel factory as per this link. Working with channel factory is something new for me so please bear with me!

Everything mentioned in the article works just fine. But now I am trying to implement Fault exceptions so that I can catch the actual exceptions on the Silverlight side. But for some reason I always end up catching CommunicationException which doesn't serve my purpose.

Here is my service contract:

[OperationContract]
[FaultContract(typeof(Fault))]
IList<Category> GetCategories();

Catch block of the service:

    catch (Exception ex)
    {
        Fault fault = new Fault(ex.Message);
        throw new FaultException<Fault>(fault, "Error occured in the GetCategories service");
    }

Service contract for client with async pattern:

[OperationContract(AsyncPattern = true)]
[FaultContract(typeof(Fault))]
IAsyncResult BeginGetCategories(AsyncCallback callback, object state);

IList<Category> EndGetCategories(IAsyncResult result);

Here is the service call from client:

        ICommonServices channel = ChannelProviderFactory.CreateFactory<ICommonServices>(COMMONSERVICE_URL, false);
        var result = channel.BeginGetCategories(
            (asyncResult) =>
            {
                try
                {
                    var returnval = channel.EndGetCategories(asyncResult);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        CategoryCollection = new ObservableCollection<Category>(returnval);
                    });
                }
                catch (FaultException<Fault> serviceFault)
                {
                    MessageBox.Show(serviceFault.Message);
                }
                catch (CommunicationException cex)
                {
                    MessageBox.Show("Unknown Communications exception occured.");
                }
            }, null
            );

I am sharing the DataContract .dll between both the service and client applications and hence they are referring to same data contract classes (Category & Fault)

Please tell me what I am doing wrongly?

UPDATE: I do clearly see the fault exception sent from the service in Fiddler. Which makes me believe I am missing something in the client side.

هل كانت مفيدة؟

المحلول

For catching normal exceptions in sivleright you must create "Silverlight-enabled WCF Service" (Add -> New Item -> Silverlight-enabled WCF Service). If you already created standard WCF service you can add attribute [SilverlightFaultBehavior] to your service manually. Default implementation of this attribute is:

    public class SilverlightFaultBehavior : Attribute, IServiceBehavior
{
    private class SilverlightFaultEndpointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SilverlightFaultMessageInspector());
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        private class SilverlightFaultMessageInspector : IDispatchMessageInspector
        {
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                return null;
            }

            public void BeforeSendReply(ref Message reply, object correlationState)
            {
                if ((reply != null) && reply.IsFault)
                {
                    HttpResponseMessageProperty property = new HttpResponseMessageProperty();
                    property.StatusCode = HttpStatusCode.OK;
                    reply.Properties[HttpResponseMessageProperty.Name] = property;
                }
            }
        }
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
        {
            endpoint.Behaviors.Add(new SilverlightFaultEndpointBehavior());
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }
}

نصائح أخرى

We use our own custom ServiceException class on the server e.g.

[Serializable]
public class ServiceException : Exception
{
    public ServiceException()
    {

    }

    public ServiceException(string message, Exception innerException)
        : base(message, innerException)
    {

    }

    public ServiceException(Exception innerException)
        : base("Service Exception Occurred", innerException)
    {

    }

    public ServiceException(string message)
        : base(message)
    {

    }
}

And then in our server side service methods we use error handling like this:

        try
        {
        ......
        }
        catch (Exception ex)
        {
            Logger.GetLog(Logger.ServiceLog).Error("MyErrorMessage", ex);
            throw new ServiceException("MyErrorMessage", ex);
        }

We then use a generic method for all web service calls:

    /// <summary>
    /// Runs the given functon in a try catch block to wrap service exception.
    /// Returns the result of the function.
    /// </summary>
    /// <param name="action">function to run</param>
    /// <typeparam name="T">Return type of the function.</typeparam>
    /// <returns>The result of the function</returns>
    protected T Run<T>(Func<T> action)
    {
        try
        {
            return action();
        }
        catch (ServiceException ex)
        {
            ServiceLogger.Error(ex);
            throw new FaultException(ex.Message, new FaultCode("ServiceError"));
        }
        catch (Exception ex)
        {
            ServiceLogger.Error(ex);
            throw new FaultException(GenericErrorMessage, new FaultCode("ServiceError"));
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top