none
WCF Parameter Value is null RRS feed

  • Question

  • I'm trying to test WCF REST service with multiple parameter. I only get one value from my passed parameter, the reset is null. What wrong on my code or fiddler. I hope someone can point me right direction.

    [OperationContract] [WebInvoke( BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Valid/{Id}")] string ValidateUser(LogInDetail loginDetail,string Id); [DataContract] public class LogInDetail { [DataMember] public string userName { get; set; } [DataMember] public string password { get; set; } } public string ValidateUser(LogInDetail loginDetail,string Id) { if (!string.IsNullOrEmpty(Id)) return string.Format("User Name : {0}, Password : {1}", loginDetail.userName,

    loginDetail.password); //username and password always null value else return string.Empty; }


    regards,


    Make Simple & Easy


    • Edited by Yukonn Monday, July 4, 2016 5:03 PM
    Monday, July 4, 2016 5:02 PM

Answers

  • Hi Yukonn,

    In order to make your code work, first please try to use the WebMessageBodyStyle.Bare instead of the WebMessageBodyStyle.Wrapped, for the difference between the WebMessageBodyStyle.Bare and WebMessageBodyStyle.Wrapped, please check this article.

    After that I have used the following code to consume the WCF Rest Service, it works fine as following:

                var serialized= "{\"userName\":\"UName\",\"password\":\"Pwd\"}";
                var httpClient = new HttpClient();
                var request = new StringContent(serialized, Encoding.UTF8, "application/json");
                try
                {
                    var response = await httpClient.PostAsync("http://localhost:13842/Service1.svc/Valid/TestID", request);
                    string content = await response.Content.ReadAsStringAsync();
                }
    
                catch (Exception ex)
                {
                   Debug.WriteLine(ex.Message.ToString());
                }

    The result:

    Best Regards,
    Amy Peng

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 5, 2016 3:20 AM
    Moderator

All replies

  • I'm trying to test WCF REST service with multiple parameter. I only get one value from my passed parameter, the reset is null. What wrong on my code or fiddler. I hope someone can point me right direction.

    [OperationContract]
    [WebInvoke(
        BodyStyle = WebMessageBodyStyle.Wrapped,
        Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "Valid/{Id}")]
    string ValidateUser(LogInDetail loginDetail,string Id);
    
    [DataContract]
        public class LogInDetail
        {
            [DataMember]
            public string userName { get; set; }
            [DataMember]
            public string password { get; set; }                
        }
    
    public string ValidateUser(LogInDetail loginDetail,string Id)
    {
                if (!string.IsNullOrEmpty(Id))
                    return string.Format("User Name : {0}, Password : {1}", loginDetail.userName, loginDetail.password); //username and password always null value
                else
                    return string.Empty;
    }



    Make Simple & Easy

    Monday, July 4, 2016 3:08 PM
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    WCF forum is above.

    Monday, July 4, 2016 4:46 PM
  • Thanks! I posted at WCF forum.

    Regards,


    Make Simple & Easy

    Monday, July 4, 2016 5:02 PM
  • Hi Yukonn,

    In order to make your code work, first please try to use the WebMessageBodyStyle.Bare instead of the WebMessageBodyStyle.Wrapped, for the difference between the WebMessageBodyStyle.Bare and WebMessageBodyStyle.Wrapped, please check this article.

    After that I have used the following code to consume the WCF Rest Service, it works fine as following:

                var serialized= "{\"userName\":\"UName\",\"password\":\"Pwd\"}";
                var httpClient = new HttpClient();
                var request = new StringContent(serialized, Encoding.UTF8, "application/json");
                try
                {
                    var response = await httpClient.PostAsync("http://localhost:13842/Service1.svc/Valid/TestID", request);
                    string content = await response.Content.ReadAsStringAsync();
                }
    
                catch (Exception ex)
                {
                   Debug.WriteLine(ex.Message.ToString());
                }

    The result:

    Best Regards,
    Amy Peng

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 5, 2016 3:20 AM
    Moderator
  • Hi,

             Thanks for reply and reproduce the code. Now, it's working after I changed to "WebMessageBodyStyle.Bare".

    [OperationContract]
    [WebInvoke(
    	BodyStyle = WebMessageBodyStyle.Bare,
    	Method = "POST",
    	RequestFormat = WebMessageFormat.Json,
    	ResponseFormat = WebMessageFormat.Json,
    	UriTemplate = "Valid/{Id}")]
    string ValidateUser(LogInDetail loginDetail, string Id);
    
    [DataContract]
    public class LogInDetail
    {
    	[DataMember]
    	public string userName { get; set; }
    	[DataMember]
    	public string password { get; set; }
    }
    
    public string ValidateUser(LogInDetail loginDetail, string Id)
    {
    	if (!string.IsNullOrEmpty(Id))
    		return string.Format("User Id : {0}, User Name : {1}, Password : {2}", 
    			Id,loginDetail.userName,loginDetail.password);
    	else
    		return string.Empty;
    }

    Result

    Regards,

    Yukon


    Make Simple & Easy


    • Edited by Yukonn Tuesday, July 5, 2016 4:05 AM
    Tuesday, July 5, 2016 4:04 AM
  • Hi Yukonn,

    Since your issue is related to WCF forum. I will move thread for better support. if your question has been resolved, please mark it.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/2030d9c7-f52c-43cb-b5eb-1c0827228eda/wcf-parameter-value-is-null?forum=wcf

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, July 5, 2016 7:53 AM
  • Hi,

      change your code    "WebMessageBodyStyle.Bare" instead of " BodyStyle = WebMessageBodyStyle.Wrapped"


    Tuesday, July 5, 2016 7:58 AM