我有一个接收自定义对象作为输入的方法[OperationContract(Name = "MyMethod")]public CustomOutput MyMethod(CustomInput inp){}[DataContract(Namespace = "")] public class CustomInput { [DataMember(Name = "x")] public string x { get; set; } }我从控制台应用程序调用它:[DataContract(Namespace = "")]public class CustomInput { [DataMember(Name = "x")] public string x { get; set; }} class Program { private const string URL2 = "http://.../MyMethod"; static void Main(string[] args) { HttpClient client = new HttpClient(); string str = JsonConvert.SerializeObject(new CustomInput () { x = "pippo" }); HttpContent content = new StringContent(str, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(URL2, content).Result; // Blocking call! if (response.IsSuccessStatusCode) { } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } } }使用此代码,我可以在服务器上正确调用 MyMethod,但 CustomInput 始终为空。有什么建议吗?
1 回答
jeck猫
TA贡献1909条经验 获得超7个赞
我认为 WCF 服务想要理解您的 json 的方式是使用外部包装器。
var data= new
{
inp = new CustomInput({ x = "pippo" })
};
string str = JsonConvert.SerializeObject(data);
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报
0/150
提交
取消