Postman POST 到 WCF 对象为空
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 测试。
这是 URL 在 Postman 中的用法:
http://localhost:49895/service1.svc/REST/GetDocInfo_mobile_V
这是 JSON 在 Postman 中的用法:
{"Labels":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}
docsv
在 GetDocInfo_mobile_V
中始终为 null。
这是为什么?
我有其他功能 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"]}}
我程序中的代码:
[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 测试。
这是 URL 在 Postman 中的用法:
http://localhost:49895/service1.svc/REST/GetDocInfo_mobile_V
这是 JSON 在 Postman 中的用法:
{"Labels":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}
docsv
在 GetDocInfo_mobile_V
中始终为 null。
这是为什么?
我有其他功能 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"]}}