0

I have WCF Rest service. my Datacontract object is

[DataContract(Name = "Z")]
public class User
{
    [DataMember(Name = "A", EmitDefaultValue = false)]
    public int UserName{ get; set; }

    [DataMember(Name = "B", EmitDefaultValue = false)]
    public int Address{ get; set; }

}

when I consume service (Rest Call) I am getting response as

{"A" : "TestName", "B": "India"}.

but I want response like this

{"userName" : "TestName", "Address": "India"}.

How can I get above response. I dont want to remove data annotations for my model object, because my old clients are using WCF service.

Thanks in advance.

Update

I have tried with below code

HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.Remove(config.Formatters.JsonFormatter);
config.Formatters.Clear();
config.Formatters.Add(new NewtonsoftJsonFormatter());

*NewtonsoftJsonFormatter is extended from MediatypeForamtter But no use. still it taking default .net serializer. any help ?

  • add [JsonProperty("userName")] attribute from Newtonsoft.Json – Houssam Hamdan Jul 25 '17 at 10:13
  • I have tried... but no use....Do I need to add in global.asax application start ? – naresh mesineni Jul 25 '17 at 15:15
  • no you dont. try removing the datamember and use JsonProperty only – Houssam Hamdan Jul 26 '17 at 05:22
  • I need both. As my desktop application is using WCF service. we are developing angular application which uses WCF Rest. But newtonsoft docu. says that we can use both. _"Json.NET attributes take precedence over standard .NET serialization attributes (e.g. if both JsonPropertyAttribute and DataMemberAttribute are present on a property and both customize the name, the name from JsonPropertyAttribute will be used)."_ or else is it possible to assign default serializer in global.asax to JSON.Net. Because when I serialize manually using jsonconvert output is correct. considering JsonProperty name. – naresh mesineni Jul 26 '17 at 07:35
  • https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc This link may help you to override the default json serializer. though it applies to asp.net mvc but it should help. – Houssam Hamdan Jul 26 '17 at 10:34
  • not helped me in context of WCF Rest – naresh mesineni Jul 26 '17 at 14:17

1 Answers1

0

Changing the name of one of the properties on your response is a breaking change to the contract of your WCF service. Formally, you want to version your service, so that old clients carry on working as usual, but new ones pick up the new functionality. Versioning of WCF services is discussed elsewhere on Stack Overflow (e.g. Best practices for versioning your services with WCF?), but at the very simpliest level you could create a new method on your service which returns the new contract (whilst behaving identically to the existing method in every other respect):

[DataContract(Name = "SomethingMoreSensible")]
public class User
{
    [DataMember(Name = "userName", EmitDefaultValue = false)]
    public int UserName{ get; set; }

    [DataMember(Name = "Address", EmitDefaultValue = false)]
    public int Address{ get; set; }
}

New clients are routed to this new method (either by explicitly giving them a different url to go to, or potentially something cleverer).

Lawrence
  • 3,287
  • 19
  • 32
  • Thanks for reply Lawrence..Ya I don't want to break the contract. But Is it possible to add JSON formatters in Global.asax to create response with original property names ? only for REST calls – naresh mesineni Jul 25 '17 at 08:24
  • Sorry, it wasn't clear that your old clients aren't using REST protocol. You can hook in to the WCF pipeline, look at `IDispatchMessageInspector` as an extension point, where you could potentially interrogate `OperationContext` to work out the context of the client call. Personally, I'd advise against doing anything too clever - the responses having different schema depending on the protocol could be surprising (depending on your scenario). – Lawrence Jul 25 '17 at 08:30
  • We are converting(creating) our desktop application to web(Angular). So our desktop client will use WCF service. Where as web client will use same service but in REST. – naresh mesineni Jul 25 '17 at 08:58