![](/img/trans.png)
[英]How to call RestFul WCF POST service With Custom Object using POSTMAN or any client tool application?
[英]Postman POST to WCF object is null
我的程序中的代碼:
[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"]}}
docsv
在GetDocInfo_mobile_V
始終為空。
這是為什么?
我有其他函數string GetEmpInfoByCard(string CardNo)
通過 Postman 成功測試但不是GetDocInfo_mobile_V
。
我想知道出了什么問題。
您應該將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"]}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.