【问题标题】:The value of the object in the request always null in wcf servicewcf 服务中请求中对象的值始终为空
【发布时间】:2019-03-02 06:33:00
【问题描述】:

我有一个 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;

    }

【问题讨论】:

    标签: c# asp.net wcf json.net


    【解决方案1】:

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

    这是我的demo,希望对你有用。

    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

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

    结果。

    获取方法。

    预订方法。

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

    【讨论】:

    • 我在服务上有五个方法,它们的BodyStyle是WebMessageBodyStyle.Bare,这些方法可以获取值并正确发布结果。我在服务中调试该方法,因此我确认该方法已被调用。您的 webconfig 有两个端点。我的只有一个。
    • 我再次阅读了我的代码。客户端应用程序使用 HttpWebRequest 获得了正确的结果。客户端应用程序使用了 WebClient。你知道我需要在哪里寻找吗?顺便说一句,在您的示例 webconfig 中,您有两个端点。我需要为服务中的每个方法都有端点吗?
    • 无需添加额外的 MEX 端点。我建议你使用 Fiddler 比较两个请求信息(使用机器名而不是 localhost URL,以便 fiddler 可以抓住 localhost 请求)。
    【解决方案2】:

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

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

    到

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

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

    【讨论】:

      猜你喜欢
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2011-08-28
      • 2014-02-20
      • 1970-01-01
      • 2019-04-12
      相关资源
      最近更新 更多