我有一个接收自定义对象作为输入的方法
[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 始终为空。
有什么建议吗?
jeck猫
相关分类