4

I am trying to find info on how to go about modifying the name of the object returned from a wcf service, in json format, to a web client via an ajax call, instead of using the default wrapper. I have searched for related articles with no luck. It seems the default wrapper name for the result will be MethodNameResult and I would like it to be GenericResponse from multiple methods.

My contract:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[WcfSerialization::DataContract(Name = "MyServiceResponse")]

public class MyServiceResponse : object
{

    public MyServiceResponse()
    {            
    }

    [WcfSerialization::DataMember(Name = "Success", IsRequired = true, Order = 0)]
    public bool Success { get; set; }

    [WcfSerialization::DataMember(Name = "ErrorMessage", IsRequired = true, Order = 1)]
    public string ErrorMessage { get; set; }  


}

My interface:

    [OperationContract()]        
    [WebInvoke(Method = "POST", 
        UriTemplate = "MyMethod", 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat=WebMessageFormat.Json
    )]
    MyServiceResponse MyMethod(MyRequest requestData); 

    [OperationContract()]        
    [WebInvoke(Method = "POST", 
        UriTemplate = "MyMethod2", 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat=WebMessageFormat.Json
    )]
    MyServiceResponse MyMethod2(MyRequest requestData); 

I would expect since I have decorated the method result's data contract with a name "MyServiceResult" then that would be the name of the resulting json object, instead I am getting a different name for each method request. For example, instead of:

{"MyServiceResponse":{"Success":true,"ErrorMessage":""}}

Over the wire I am getting:

{"Method1Result":{"Success":true,"ErrorMessage":""}}

and

{"Method2Result":{"Success":true,"ErrorMessage":""}}

This is preventing the client from making a generic inspection of the result as would be the case in

success: function (returnData, textStatus, xhr) {
            result.success = returnData.MyServiceResponse.Success;
            result.errorMessage = returnData.errorMessage;
},

Thanks

Ross Bush
  • 14,648
  • 2
  • 32
  • 55

2 Answers2

5

You can add additional attribute to you method which specify name of the wrapper:

[return: MessageParameter(Name = "MyServiceResponse")]

For details of JSON REST formatting you can refer RESTful web service body format question.

Community
  • 1
  • 1
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
0

Try setting your UriTemplate setting to the following...

UriTemplate = "MyServiceResponse"

... and edit your response down to just this...

public class MyServiceResponse
{ 
    public bool Success { get; set; }
    public string ErrorMessage { get; set; }  
}

You're specifying serialization for SOAP and adding a lot of stuff that C# will take care of for you, for example, a default constructor for your class. This way you're specifying that you want your service to return a clean JSON object and the string will begin with "MyServiceResponse" when it's traveling across the wire.

gfish3000
  • 1,557
  • 1
  • 11
  • 22
  • Thanks but the service has to support pox,json and soap results. I am trying to keep the Response objects interchangeable. – Ross Bush Apr 09 '14 at 15:40