1

Code in my program:

[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 in my system:

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

I use Postman to do the POST testing.

This is the URL use in Postman:
http://localhost:49895/service1.svc/REST/GetDocInfo_mobile_V

This is the JSON use in Postman:
{"Labels":{"labellist":["5015058942/0001/0000","5015298272/0001/0000"]}}

docsv is always null in GetDocInfo_mobile_V.
Why is that?

I have other function string GetEmpInfoByCard(string CardNo) which tested successfully via Postman but not GetDocInfo_mobile_V.
I wonder what went wrong.

Postman

Pop
  • 525
  • 1
  • 7
  • 22

1 Answers1

4

You should add DataMember attibute to labellist:

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

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

EDIT:

Also you need to send request as:

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

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28