【问题标题】:Postman POST to WCF object is null邮递员 POST 到 WCF 对象为空
【发布时间】:2020-05-26 14:29:28
【问题描述】:

我的程序中的代码:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    string GetDocInfo_mobile_V(Labels docs);
}

[DataContract]
public class Labels
{
    private List<string> label;

    [DataMember]
    public List<string> labellist
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }
    }
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    private static readonly NLog.Logger nLogger = NLog.LogManager.GetCurrentClassLogger();

    public string GetDocInfo_mobile_V(Labels docsv)
    {
        try
        {
            Documents docs = new Documents();
            foreach (var docv in docsv.labellist)
            {
                string[] info = docv.Split('/');
                if (info.Length > 1)
                {
                    Document doc = new Document { Doc_item_num = info[1], Doc_num = info[0] };
                    docs.Listdocument.Add(doc);
                }
            }
            return GetDocInfo_mobile(docs);
        }
        catch (Exception exception)
        {
            nLogger.Error(exception);
            throw;
        }
    }
}

我系统中的Web.config:

<system.serviceModel>
<services>
  <service behaviorConfiguration="SmartLockerWS.Service1" name="SmartLockerWS.Service1">
    <endpoint address="" binding="wsHttpBinding" contract="SmartLockerWS.IService1">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <endpoint address="REST" binding="webHttpBinding" behaviorConfiguration="restfulbehavior" bindingConfiguration="" contract="SmartLockerWS.IService1"></endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="restfulbehavior">
      <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" defaultBodyStyle="WrappedRequest"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="SmartLockerWS.Service1">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="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="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

我使用 Postman 进行 POST 测试。

这是 Postman 中使用的 URL:
http://localhost:49895/service1.svc/REST/GetDocInfo_mobile_V

这是 Postman 中使用的 JSON:
{"Labels":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}

docsvGetDocInfo_mobile_V 中始终为空。
这是为什么呢?

我还有其他功能 string GetEmpInfoByCard(string CardNo) 已通过 Postman 成功测试,但未通过 GetDocInfo_mobile_V
我不知道出了什么问题。

【问题讨论】:

  • 我想你忘了给labellist添加DataMember属性

标签: c# wcf postman


【解决方案1】:

您应该将DataMember 属性添加到labellist

[DataContract]
public class Labels
{
    private List<string> label;

    [DataMember]
    public List<string> labellist
    {
        get
        {
            return label;
        }
        set
        {
            label = value;
        }
    }
}

编辑:

您还需要将请求发送为:

{"docs":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}

【讨论】:

  • 我更新了我的代码,结果还是一样,仍然收到null。
  • {"docs":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}发送请求
  • 不,仍然为空。
  • 应该可以,我认为您在通过 Postman 发送请求时遇到问题。请查看 Postman stackoverflow.com/questions/12756688/…如何发布 Json 数据
  • 我检查了你在回答中提到的邮递员屏幕,它应该是docs而不是docsv
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2019-11-14
  • 2020-09-17
  • 2019-11-11
  • 2017-12-30
  • 2016-04-03
  • 1970-01-01
  • 2016-02-12
相关资源
最近更新 更多