C# 添加响应格式和主体样式时,在wcf服务中获取空对象 string baseAddress=“http://“+Environment.MachineName+”:8000/RestServiceImpl”; ServiceHost主机=新ServiceHost(typeof(RestServiceImpl),新Uri(baseAddress)); //host.AddServiceEndpoint(typeof(IRestServiceImpl),new WebHttpBinding(),“”)。Behaviors.Add(new GainSoft.TaskManager.Service.RestServiceImpl.MyWebHttpBehavior()); host.AddServiceEndpoint(typeof(IRestServiceImpl),new WebHttpBinding(),“”)。Behaviors.Add(new WebHttpBehavior()); ServiceDebugBehavior sdb=host.Description.Behaviors.Find(); sdb.HttpHelpPageEnabled=false; host.Open(); Console.WriteLine(“主机已打开”); WebClient客户端=新的WebClient(); //Console.WriteLine(client.DownloadString(baseAddress+“/InsertData?param1=John,Doe”); 尝试 { 客户端=新的WebClient(); client.Headers[HttpRequestHeader.ContentType]=“application/json”; Console.WriteLine(client.UploadString(baseAddress+“/InsertData”、“{\”FirstName\”:“John\”、“LastName\”:“Doe\”});

C# 添加响应格式和主体样式时,在wcf服务中获取空对象 string baseAddress=“http://“+Environment.MachineName+”:8000/RestServiceImpl”; ServiceHost主机=新ServiceHost(typeof(RestServiceImpl),新Uri(baseAddress)); //host.AddServiceEndpoint(typeof(IRestServiceImpl),new WebHttpBinding(),“”)。Behaviors.Add(new GainSoft.TaskManager.Service.RestServiceImpl.MyWebHttpBehavior()); host.AddServiceEndpoint(typeof(IRestServiceImpl),new WebHttpBinding(),“”)。Behaviors.Add(new WebHttpBehavior()); ServiceDebugBehavior sdb=host.Description.Behaviors.Find(); sdb.HttpHelpPageEnabled=false; host.Open(); Console.WriteLine(“主机已打开”); WebClient客户端=新的WebClient(); //Console.WriteLine(client.DownloadString(baseAddress+“/InsertData?param1=John,Doe”); 尝试 { 客户端=新的WebClient(); client.Headers[HttpRequestHeader.ContentType]=“application/json”; Console.WriteLine(client.UploadString(baseAddress+“/InsertData”、“{\”FirstName\”:“John\”、“LastName\”:“Doe\”});,c#,.net,json,wcf,C#,.net,Json,Wcf,服务代码 如果我删除了突出显示代码,那么它可以正常工作。我不知道我在这里遗漏了什么? 您必须在上载的JSON中包含参数的名称。因此: string baseAddress = "http://" + Environment.MachineName + ":8000/RestServiceImpl"; ServiceHost host = new ServiceHost(typeof(RestServiceImpl), new Uri(baseAddress));

服务代码 如果我删除了突出显示代码,那么它可以正常工作。我不知道我在这里遗漏了什么?

您必须在上载的JSON中包含参数的名称。因此:

string baseAddress = "http://" + Environment.MachineName + ":8000/RestServiceImpl";
        ServiceHost host = new ServiceHost(typeof(RestServiceImpl), new Uri(baseAddress));
        //host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new GainSoft.TaskManager.Service.RestServiceImpl.MyWebHttpBehavior());
        host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        sdb.HttpHelpPageEnabled = false;
        host.Open();
        Console.WriteLine("Host opened");

        WebClient client = new WebClient();
     // Console.WriteLine(client.DownloadString(baseAddress + "/InsertData?param1=John,Doe"));


        try
        {
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            Console.WriteLine(client.UploadString(baseAddress + "/InsertData", "{\"FirstName\":\"John\",\"LastName\":\"Doe\"}"));
但是它没有解释为什么它在没有突出显示代码的情况下工作


这是否解决了您的问题?

您必须在上载的JSON中包含参数的名称。因此:

string baseAddress = "http://" + Environment.MachineName + ":8000/RestServiceImpl";
        ServiceHost host = new ServiceHost(typeof(RestServiceImpl), new Uri(baseAddress));
        //host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new GainSoft.TaskManager.Service.RestServiceImpl.MyWebHttpBehavior());
        host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        sdb.HttpHelpPageEnabled = false;
        host.Open();
        Console.WriteLine("Host opened");

        WebClient client = new WebClient();
     // Console.WriteLine(client.DownloadString(baseAddress + "/InsertData?param1=John,Doe"));


        try
        {
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            Console.WriteLine(client.UploadString(baseAddress + "/InsertData", "{\"FirstName\":\"John\",\"LastName\":\"Doe\"}"));
但是它没有解释为什么它在没有突出显示代码的情况下工作

这能解决你的问题吗