WCF学习心得--客户端获取服务端自定义类数据

因项目需求,需要一个WCF服务,赶鸭子上架吧!下面直接切入正题!

首先创建WCF应用程序,具体如何创建就不赘述了,网上一大篇,我主要说说自己遇到的问题

问题一:超时问题,在最后获取数据的时候突然提示服务超时,服务已断开

解决:配置文件添加:

<bindings>

      <wsHttpBinding>

        <binding name="BindConfig" closeTimeout="00:01:00"

            openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"

            bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"

            maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

            messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"

            allowCookies="false">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

              maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <reliableSession ordered="true" inactivityTimeout="00:10:00"

              enabled="false" />

          <security mode="None">

            <transport clientCredentialType="Windows" proxyCredentialType="None"

                realm="" />

            <message clientCredentialType="Windows" negotiateServiceCredential="true"

                algorithmSuite="Default" establishSecurityContext="true" />

          </security>

        </binding>


当然你也可以在代码中修改WSHttpBinding对象的一些属性

问题二:解决上述问题之后又出现了新问题,在服务端可以获取得到数据,但是到了客户端总是空,然后准备测试数据,在服务端只给一个int型的变量,客户端获取的到,但是一旦换成自己的自定义类,客户端就获取不到

解决:在服务端和客户端都要有自定义类,代码要一样,另外还要保证两个类的命名空间一致

如:服务端

[DataContract(Namespace = "Rostering.BO")]

    [Serializable]

    public class NewAttendancePlan

    {

        [DataMember]

        public int AttendancePlan_Id { get; set; }

    }


客户端一样:

[DataContract(Namespace = "Rostering.BO")]

    [Serializable]

    public class NewAttendancePlan

    {

        [DataMember]

        public int AttendancePlan_Id { get; set; }

    }


OK~~结果如预期出来!!

 

 

你可能感兴趣的:(WCF)