简体   繁体   English

在wcf服务中,请求中对象的值始终为null

[英]The value of the object in the request always null in wcf service

I have a WCF service and the client website to test the service. 我有一个WCF服务和客户端网站来测试服务。 The WCF service doesn't get the value of the object. WCF服务未获取对象的值。 I have searched the web and modified my code. 我搜索了网页并修改了我的代码。 However I haven't solved it. 但是我还没有解决它。 Would someone help me. 有人会帮助我吗? Thanks in advance. 提前致谢。

There is my service: 有我的服务:

 [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;
    }

There is the method in my website to call the service. 我网站上有一种方法可以调用该服务。

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;

    }

Where did you get your error information, could you please share with me? 您从哪里获得错误信息,请与我分享? Have you configured and published the WCF web-mode service correctly? 您是否正确配置并发布了WCF Web模式服务? I copy your code and host the web-mode service on the IIS, finally I succeed to access the method. 我复制你的代码并在IIS上托管web模式服务,最后我成功访问了该方法。 I suggest that you could expose the default GetData() method and then test it whether you could get the returned results correctly, and then we test the strongly-typed method. 我建议您可以公开默认的GetData()方法,然后测试它是否可以正确获取返回的结果,然后我们测试强类型方法。

Here is my demo, wish it is useful to you. 这是我的演示,希望它对您有用。

IService1.cs 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);
}

Service1.svc.cs Service1.svc.cs

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; }

} }

Webconfig Webconfig

<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" />

Result. 结果。

GetMethod. GetMethod。 GetData方法

Booking Method. 预订方式。 预订方式

Feel free to let me know if you have any questions. 如果您有任何疑问,请随时告诉我。

There is a problem with the payload you are sending to the service. 您发送给服务的有效负载存在问题。 Change 更改

string stringPayload = "{\\"bookInfo\\":[" +JsonConvert.SerializeObject(input) +"]}";

to

string stringPayload = "{\\"bookInfo\\":" +JsonConvert.SerializeObject(input) +"}";

(notice there is no need to wrap JSON value inside [] )and your WebClient request will be working and your service will have a non-null value for the arguments. (注意不需要在[]中包装JSON值,并且您的WebClient请求将起作用,并且您的服务将具有参数的非null值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM