angularasp.net-web-apihttpclientwcf-rest

Not able to get the correct object in WCF methods when communicating between WCF Rest services and .Net WebAPI


I am trying to call WebAPI from Angular Application, which then calls WCF Rest services, but in WCF method, param is always NULL. When checked in detail, I noticed that in Web API Method it is accepting the jSON without the ClassName. i.e:

{
        "workflowUserName": "xyz",
        "workflowPassword": "abc123"
}

When I try to pass same json to WCF service method (via Postman), its giving NULL, although both methods have same input parameters i.e. MyTestInputParam . WCF method is accepting the json with the object name like. i.e:

"MyTestInputParam": {
    "workflowUserName": "xyz",
    "workflowPassword": "abc123"
}

Here is my code: WEB API

 public class iOPSController : ApiController
{
    [HttpPost, HttpOptions]
    public async Task<HttpResponseMessage> Login(MyTestInputParam MyTestInputParam)
    {
        string json = MyTestInputParam.ToJson();
        System.Net.Http.Headers.HttpRequestHeaders headers = this.Request.Headers;

        HttpResponseMessage responsePostA = new HttpResponseMessage();
        string URL = ConfigurationManager.AppSettings["AplicationServer"].ToString();
        URL = URL + "\\Login";
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(URL);

            var content = new StringContent(JsonConvert.SerializeObject(MyTestInputParam), System.Text.Encoding.UTF8, "application/json");
            string jsonContent = content.ReadAsStringAsync().Result;
            var result = await client.PostAsync(URL, content);
            responsePostA = result.EnsureSuccessStatusCode();
        }
        return responsePostA;
    }

WCF Method

    [ServiceContract]
    public interface IExternalService
    {

        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Login")]
        LoginResponse Login(MyTestInputParam MyTestInputParam);
}

Please let me know what i am doing wrong, how i can get the same response in API and WCF method.


Solution

  • Please remove "BodyStyle = WebMessageBodyStyle.Wrapped" tag from WCF Method and you will get what you required .I hope this will solve your problem. Actually this property wrapped the request in class name removing this tag will solve your problem .