2

I'm developing a WCF service that accepts parameters in JSON. I cant' figure out where I'm going wrong. Please help.

When I test the service using fiddler, I post the following:

"locations": {
        "Departments": [{
            "Name": "Amazonas",
            "alias": "",
            "Municipalities": [{
                "Name": "El Encanto"
            }, {
                "Name": "La Chorrera"
            }]

        }]
    }

I get a 400 error with: "The server encountered an error processing the request. The exception message is 'OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'string'.'. See server logs for more details."

The stacktrace is:

    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Here is the contract:

 [OperationContract(Name = "setFacilities")]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "/setFacilities")]
    string SetFacilities(LocationData locations);

[UPDATE]

Here's the LocationData class

    [Serializable]
public class LocationData
{
    public IList<Department> Departments;
}

[Serializable]
public class Department
{
    public string Name;
    public string Alias;
    public IList<Municipality> Municipalities;
}

[Serializable]
public class Municipality
{
    public string Name;
}

What I'm I missing?

1

1 Answer 1

3

The problem is your JSON.

You have not shared the class LocationData so I cannot tell you how it must look like but your JSON needs to be wrapped in brackets:

{
    "locations": {
        "Departments": [{
            "Name": "Amazonas",
            "alias": "",
            "Municipalities": [{
                "Name": "El Encanto"
            }, {
                "Name": "La Chorrera"
            }]

        }]
    }
}

I guess LocationData is actually the one which has Departments so I think "locations": is redundant:

{

    "Departments": [{
        "Name": "Amazonas",
        "alias": "",
        "Municipalities": [{
            "Name": "El Encanto"
        }, {
            "Name": "La Chorrera"
        }]

    }]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!. I've posted the LocationData class above if you would care to take a look
The first snippet worked! the second failed.The problems seems to be the omission of the outer curly braces. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.