0

I have very ususual situation where I have WCF service on which I have added REST endpoint and returning JSON data to angular app. My response object is defined in Pascal Case but I would like to return the JSON response in camelCase. Is there anyway to do this in WCF?

I did not have an option to create new Web Api or MVC app where I can do this easily using options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Here is my code:

WCF Rest Endpoint

IFormsConfigTool.cs

 [ServiceContract]
    public interface IFormsConfigTool
    {

        [OperationContract]
        [System.ServiceModel.Web.WebInvoke(
                    Method = "GET",
                    RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json,
                    ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,
                    BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Bare,
                    UriTemplate = "getPackageConfig/{superBranch}")]
        PackageConfigResponse getPackageConfig(string superBranch);

    }

Response Class

 public class PackageConfigResponse
    {
        #region Properties

        public List<Package> Packages { get; set; }
        public MessageInfo Messages { get; set; }

        #endregion Properties
    }

Current JSON response enter image description here

Expected JSON response enter image description here

Code-Strings
  • 111
  • 2
  • 10
  • 1
    You can declare your method to return a *System.ServiceModel.Channels.Message* and serialize your object by yourself. `var json = JsonConvert.SerializeObject(...., options); return WebOperationContext.Current.CreateTextResponse(json,"application/json", Encoding.UTF8);` – Eser May 31 '18 at 18:11
  • As @Eser writes, the simplest solution is to serialize manually using Json.NET and `CamelCasePropertyNamesContractResolver` as shown in [How to set Json.Net as the default serializer for WCF REST service](https://stackoverflow.com/a/3131413/3744182). You can also manually specify data member names as shown in [Name attribute of DataMember in WCF looks like not working](https://stackoverflow.com/q/47808290). – dbc Jun 01 '18 at 01:11
  • Thank you very much @Eser - your answer worked for us! – Code-Strings Jun 01 '18 at 15:25
  • 1
    Here is the function I created and worked. ---private System.ServiceModel.Channels.Message CreateJsonResponse(Object resultObj) { string json = JsonConvert.SerializeObject(resultObj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); return System.ServiceModel.Web.WebOperationContext.Current.CreateTextResponse(json, "application/json", System.Text.Encoding.UTF8); } – Code-Strings Jun 01 '18 at 15:49

0 Answers0