Wcf服务发送具有空值的类对象列表

时间:2012-06-29 19:45:18

标签: wcf list class object contract

问题是当wcf服务将类对象列表发送到客户端时,值为null。任何帮助或指导表示赞赏。

我的服务界面

IPswdService.cs

namespace GetPasswordsSvc
{
    [ServiceContract]
    public interface IPswdService
    {
        [OperationContract]
        List<dbVal> GetData(int value);
    }

[DataContract]
    public class dbVal
    {
        public string Group;
        public string Title;
        public string Username;
    } ;
}

GetPasswords.svc.cs

namespace GetPasswordsSvc
{
    public class PswdService : IPswdService
    {

       public List<dbVal> GetData(int value)
        {
          List<dbVal> kpdata= (from entry in db.RootGroup.GetEntries(true)
                         select new dbVal
                         {
                             Group = entry.ParentGroup.Name,
                             Title = entry.Strings.ReadSafe("Title"),
                             Username = entry.Strings.ReadSafe("UserName"),
                         }).ToList();

            List<dbVal> l = kpdata.ToList();
             return l;
        }

======================================

WCF客户端代码

SvcRef.PswdServiceClient wcfClient = new SvcRef.PswdServiceClient();
wcfClient.Open();
List<GetPasswords.dbVal> dbValues = wcfClient.GetData(0);

============================================== < / p>

客户端配置文件是

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IPswdService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://xxxxxxx:444/GetPasswords.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IPswdService" contract="SvcRef.IPswdService"
        name="BasicHttpBinding_IPswdService" />
    </client>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:2)

很难准确说明原因是什么,但是如果您看到服务数据合同,我可以看到您错过了数据对应字段中的DataMember属性,您的数据合同应该是:< / p>

[DataContract]
public class dbVal
{
    [DataMember]
    public string Group;
    [DataMember]
    public string Title;
    [DataMember]
    public string Username;
}