客户端获取服务端自定义类数据
问题一:超时问题,在最后获取数据的时候突然提示服务超时,服务已断开
解决:配置文件添加:
<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~~结果如预期出来!!
4.代理类
/// < summary>
/// 用于调用服务的类
/// < /summary>
public class MyClient : ClientBase< ITest>, ITest
{
public MyClient(System.ServiceModel.Channels.Binding binding, EndpointAddress edpAddr)
: base(binding, edpAddr) { }
public int Add(int a, int b)
{
return base.Channel.Add(a, b);
}