.Net对象未随请求一起发布

时间:2016-05-18 13:05:30

标签: c# .net wcf

我创建了一个带有webhttp绑定的wcf-rest自托管服务或web api服务,并附加了证书以启用https。当我从控制台应用程序中执行以下代码时,请求将在服务端获得,但参数为空。难道我做错了什么??请帮忙。

screen shot of the service method

Person Model

using (var client = new HttpClient())
{
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    client.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("John:Doe")));
    Person person = new Person() { FirstName = "Rudri", LastName = "Test" };
    HttpContent content = new StringContent(JsonConvert.SerializeObject(person), System.Text.Encoding.UTF8, "application/json");
    var response = await client.PostAsync("https://0.0.0.0:8085/WtfService/HelloWorldPostComplex", content);
    var statusCode = response.StatusCode;
    var responseMessage = response.ReasonPhrase;
    var result = await response.Content.ReadAsStringAsync();
    Console.WriteLine(result);
    Console.ReadLine();
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您遇到的问题通常与WebMessageBodyStyle - 问题有关。

确保您的界面定义邮件是否将被包装或裸露:

[ServiceContract]
public interface IWtfService
{
    [OperationContract]
    [WebInvoke(Method = "POST", 
               ResponseFormat = WebMessageFormat.Json, 
               RequestFormat = WebMessageFormat.Json, 
               BodyStyle = WebMessageBodyStyle.Wrapped)]
    void HelloWorldPostComplex(Person person);
}

此外,请注意WebMessageFormat.Json声明。 此外,要了解线路的内容,使用Fiddler调试HTTP流量通常很有帮助。