C# 请求中对象的值在wcf服务中始终为空

C# 请求中对象的值在wcf服务中始终为空,c#,asp.net,wcf,json.net,C#,Asp.net,Wcf,Json.net,我有一个WCF服务和客户端网站来测试该服务。WCF服务未获取对象的值。我已经在网上搜索并修改了我的代码。但是我还没有解决它。有人能帮我吗。提前谢谢 这是我的服务: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Json, BodyStyle = Web

我有一个WCF服务和客户端网站来测试该服务。WCF服务未获取对象的值。我已经在网上搜索并修改了我的代码。但是我还没有解决它。有人能帮我吗。提前谢谢

这是我的服务:

 [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest,          
       UriTemplate = "BookInfo/")]

    BookingResult Booking(BookInfo bookInfo);


 public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();            
        if (bookInfo.Name == null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }

        return result;
    }
在我的网站上有一个调用服务的方法

using Booking; //this is WCF service reference
private string callService(BookInfo input)
    {

        string serviceUrl = "http://localhost:1599026/Booking.svc/BookInfo/";            
        string stringPayload = "{\"bookInfo\":[" +JsonConvert.SerializeObject(input) +"]}";           
        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";                    
        client.Encoding = Encoding.UTF8;
        string rtn = client.UploadString(serviceUrl,"POST", stringPayload);
        return rtn;

    }

你是从哪里得到错误信息的,你能和我分享一下吗?您是否正确配置并发布了WCF web模式服务?我复制了您的代码并在IIS上托管了web模式服务,最后成功地访问了该方法。我建议您可以公开默认的GetData()方法,然后测试它是否能够正确获得返回的结果,然后测试强类型方法

这是我的演示,希望对你有用

IService1.cs

public interface IService1
{
    [OperationContract]
    [WebGet]
    string GetData(int value);

    [OperationContract]
    [WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Json,
   BodyStyle = WebMessageBodyStyle.WrappedRequest,
   UriTemplate = "BookInfo/")]
    BookingResult Booking(BookInfo bookInfo);
}
public class Service1 : IService1
{
    public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();
        if (bookInfo==null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }
        return result;
    }
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}
[DataContract]
public class BookingResult
{
    [DataMember]
    public bool isSucceed { get; set; }
Service1.svc.cs

public interface IService1
{
    [OperationContract]
    [WebGet]
    string GetData(int value);

    [OperationContract]
    [WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Json,
   BodyStyle = WebMessageBodyStyle.WrappedRequest,
   UriTemplate = "BookInfo/")]
    BookingResult Booking(BookInfo bookInfo);
}
public class Service1 : IService1
{
    public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();
        if (bookInfo==null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }
        return result;
    }
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}
[DataContract]
public class BookingResult
{
    [DataMember]
    public bool isSucceed { get; set; }
}

网络配置

<system.serviceModel>
<services>
  <service name="WcfService4.Service1" behaviorConfiguration="svbehavior">
    <endpoint address="" binding="webHttpBinding" contract="WcfService4.IService1" behaviorConfiguration="webbehavior">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="svbehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webbehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

预订方式。


如果您有任何问题,请随时告诉我。

您发送到服务的有效负载有问题。改变

string stringPayload=“{\“bookInfo\”:[“+JsonConvert.SerializeObject(输入)+“]}”

string stringPayload=“{\“bookInfo\”:“+JsonConvert.SerializeObject(输入)+“}”


(请注意,不需要在[]中包装JSON值),您的
WebClient
请求将正常工作,您的服务的参数值将为非空。

我在服务上有五个方法,它们的BodyStyle是WebMessageBodyStyle。这些方法可以获得值并正确发布结果。我在服务中调试该方法,因此我确认调用了该方法。您的webconfig有两个端点。我的只有一个。我又读了一遍我的代码。客户端应用程序使用HttpWebRequest获得正确的结果。客户端应用程序使用WebClient。你知道我要去哪里找吗?顺便说一下,在您的示例webconfig中,您有两个端点。我是否需要为服务中的每个方法都有端点?不需要添加额外的MEX端点。我建议您使用Fiddler来比较这两个请求信息(使用machinename而不是localhost URL,以便Fiddler可以绘制localhost请求)。