1

I have an AJAX-enabled WCF service (with enableWebScript in the behavior) that has a ValidationFault which I created.

Here's the service:

[ServiceContract]
public interface ICoreWCF
{
    /// <summary>
    /// Saves the Customer.
    /// </summary>
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [FaultContract(typeof(ValidationFault))]
    void Customer_Save(Customer customer);
}

Here's the fault:

[DataContract]
public class ValidationFault
{
    [DataMember(Name = "success")]
    public bool Success { get; set; }

    [DataMember(Name = "msg")]
    public string ValidationMessage { get; set; }

    [DataMember(Name = "errors")]
    public Dictionary<string, string> Errors { get; set; }
}

I would like to send this fault back to the client javascript. The problem is that my custom fault's DataMembers are ignored and a general exception is returned.

How can I send the errors collection to the client?

I already tried writing my own IErrorHandler similar to this, such that it uses Exception Handling Application Block to convert an exception to a fault, and then the IErrorHandler serializes the resulting fault. But it appears that the JsonErrorHandler of the WebScriptingEnablingBehavior is not dealing well with the resulting Message object.

Thanks.

Dor Rotman
  • 1,511
  • 1
  • 15
  • 27
  • Take a look [here][1] its been answered already. [1]: http://stackoverflow.com/questions/1272877/returning-error-details-from-ajax-enabled-wcf-service – Bryan Corazza Jul 24 '11 at 16:49
  • Thanks @Bryan. I saw this question, but unfortunately I am experiencing the same problem as described by this answer: http://stackoverflow.com/questions/1272877/returning-error-details-from-ajax-enabled-wcf-service/3705135#3705135 – Dor Rotman Jul 26 '11 at 07:21

2 Answers2

0

in webinvoke you can add RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json try it

Ria
  • 10,237
  • 3
  • 33
  • 60
yapingchen
  • 836
  • 5
  • 4
0

If you have implemented IErrorHandler and associated it to service using using custom behavior inherited from WebHttpBehavior as sighted by link then perhaps you should try adding default request/response format etc. For example,

private class CustomWebScriptBehavior : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint,
        System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        // clear current error handlers
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
        // add our error handler
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(
                new ErrorHandler(true));
    }

    private WebMessageFormat _requestFormat;
    private WebMessageFormat _responseFormat;

    public CustomWebScriptBehavior()
    {
        _requestFormat = _responseFormat = WebMessageFormat.Json;
    }

    public override bool AutomaticFormatSelectionEnabled
    {
        get { return false; }
        set { throw new NotSupportedException(); }
    }

    public override WebMessageBodyStyle DefaultBodyStyle
    {
        get { return WebMessageBodyStyle.WrappedRequest; }
        set { throw new NotSupportedException(); }
    }

    public override WebMessageFormat DefaultOutgoingRequestFormat
    {
        get { return _requestFormat; } 
        set { _requestFormat = value; }
    }

    public override WebMessageFormat DefaultOutgoingResponseFormat
    {
        get { return _responseFormat; }
        set { _responseFormat = value; }
    }
}

This will eliminate the need to specify WebInvoke attribute for each method.

VinayC
  • 47,395
  • 5
  • 59
  • 72