C# WCF 服务器检索“List”有 1 个条目,但客户端没有收到
我正在尝试从服务器(使用 FluentNHibernate 的服务器)检索客户端列表。 客户端对象如下:
[DataContract]
//[KnownType(typeof(System.Collections.Generic.List<ContactPerson>))]
//[KnownType(typeof(System.Collections.Generic.List<Address>))]
//[KnownType(typeof(System.Collections.Generic.List<BatchRequest>))]
//[KnownType(typeof(System.Collections.Generic.List<Discount>))]
[KnownType(typeof(EClientType))]
[KnownType(typeof(EComType))]
public class Client
{
#region Properties
[DataMember]
public virtual int ClientID { get; set; }
[DataMember]
public virtual EClientType ClientType { get; set; }
[DataMember]
public virtual string RegisterID {get; set;}
[DataMember]
public virtual string HerdCode { get; set; }
[DataMember]
public virtual string CompanyName { get; set; }
[DataMember]
public virtual bool InvoicePerBatch { get; set; }
[DataMember]
public virtual EComType ResultsComType { get; set; }
[DataMember]
public virtual EComType InvoiceComType { get; set; }
//[DataMember]
//public virtual IList<ContactPerson> Contacts { get; set; }
//[DataMember]
//public virtual IList<Address> Addresses { get; set; }
//[DataMember]
//public virtual IList<BatchRequest> Batches { get; set; }
//[DataMember]
//public virtual IList<Discount> Discounts { get; set; }
#endregion
#region Overrides
public override bool Equals(object obj)
{
var other = obj as Client;
if (other == null)
return false;
return other.GetHashCode() == this.GetHashCode();
}
public override int GetHashCode()
{
return ClientID.GetHashCode() | ClientType.GetHashCode() | RegisterID.GetHashCode() |
HerdCode.GetHashCode() | CompanyName.GetHashCode() | InvoicePerBatch.GetHashCode() |
ResultsComType.GetHashCode() | InvoiceComType.GetHashCode();// | Contacts.GetHashCode() |
//Addresses.GetHashCode() | Batches.GetHashCode() | Discounts.GetHashCode();
}
#endregion
}
我已经尝试删除子列表,但即使使用这个简化版本的客户端,我仍然遇到问题。
我的流畅映射是:
public class ClientMap : ClassMap<Client>
{
public ClientMap()
{
Table("Clients");
Id(p => p.ClientID);
Map(p => p.ClientType).CustomType<EClientType>(); ;
Map(p => p.RegisterID);
Map(p => p.HerdCode);
Map(p => p.CompanyName);
Map(p => p.InvoicePerBatch);
Map(p => p.ResultsComType).CustomType<EComType>();
Map(p => p.InvoiceComType).CustomType<EComType>();
//HasMany<ContactPerson>(p => p.Contacts)
// .KeyColumns.Add("ContactPersonID")
// .Inverse()
// .Cascade.All();
//HasMany<Address>(p => p.Addresses)
// .KeyColumns.Add("AddressID")
// .Inverse()
// .Cascade.All();
//HasMany<BatchRequest>(p => p.Batches)
// .KeyColumns.Add("BatchID")
// .Inverse()
// .Cascade.All();
//HasMany<Discount>(p => p.Discounts)
// .KeyColumns.Add("DiscountID")
// .Inverse()
// .Cascade.All();
}
客户端方法(如下所示)连接到服务器。服务器检索列表,并且所有内容看起来都在对象中,但是,当它返回时,客户端不会收到任何内容(它收到一个 List 对象,但其中没有任何内容。
这里调用方法:
public List<s.Client> GetClientList()
{
try
{
s.DataServiceClient svcClient = new s.DataServiceClient();
svcClient.Open();
List<s.Client> clients = new List<s.Client>();
clients = svcClient.GetClientList().ToList<s.Client>();
svcClient.Close(); //when receiving focus from server, the clients object has a count of 0
return clients;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return null;
}
和服务器方法:
public IList<Client> GetClientList()
{
var clients = new List<Client>();
try
{
using (var session = SessionHelper.OpenSession())
{
clients = session.Linq<Client>().Where(p => p.ClientID > 0).ToList<Client>();
}
}
catch (Exception e)
{
EventLog.WriteEntry("eCOWS.Data", e.Message);
}
return clients; //returns a list with 1 client in it
}
服务器方法接口是:
[UseNetDataContractSerializer]
[OperationContract]
IList<Client> GetClientList();
作为最终参考,这里是我的客户端 app.config 条目:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IDataService" listenBacklog="10" maxConnections="10"
transferMode="Buffered" transactionProtocol="OleTransactions"
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<readerQuotas maxDepth="51200000" maxStringContentLength="51200000"
maxArrayLength="51200000" maxBytesPerRead="51200000"
maxNameTableCharCount="51200000" />
<security mode="Transport"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:9000/eCOWS/DataService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IDataService"
contract="eCowsDataService.IDataService" name="NetTcpBinding_IDataService"
behaviorConfiguration="eCowsEndpointBehavior">
</endpoint>
<endpoint address="MEX"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="eCowsEndpointBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
和我的服务器 app.config:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding"
maxConnections="10" listenBacklog="10"
transferMode="Buffered" transactionProtocol="OleTransactions"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
sendTimeout="00:10:00" receiveTimeout="00:10:00">
<readerQuotas maxDepth="51200000" maxStringContentLength="51200000"
maxArrayLength="51200000" maxBytesPerRead="51200000"
maxNameTableCharCount="51200000" />
<security mode="Transport"/>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="eCows.Data.Services.DataService" behaviorConfiguration="eCowsServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/eCOWS/" />
<add baseAddress="net.tcp://localhost:9000/eCOWS/" />
</baseAddresses>
</host>
<endpoint address="DataService"
binding="netTcpBinding"
contract="eCows.Data.Services.IDataService"
behaviorConfiguration="eCowsEndpointBehaviour">
</endpoint>
<endpoint address="MEX"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="eCowsEndpointBehaviour">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="eCowsServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
<behavior name="MexBehaviour">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我用来遇到“套接字关闭/网络或超时”错误,并且跟踪清楚地表明,在回调中它正在寻找侦听端点,但找不到。无论如何,在添加 UseNetSerializer 后,错误消失了,但现在如果
我添加所有 PS.注释掉列表项后,我仍然从数据库中检索条目,但在客户端上仍然没有收到任何内容,
如果我删除 [UseNetDataContractSerializer],我会在 svclog 中收到以下错误:
警告:描述错误 System.ServiceModel.Channels.ServerSessionPreambleConnectionReader+ServerFramingDuplexSessionChannel
警告:描述错误 System.ServiceModel.Channels.ServiceChannel
错误: 初始化[eCows.Data.Models.Client#3] - 失败 延迟初始化集合 角色: eCows.Data.Models.Client.Addresses,否 会话或会话已关闭
...
错误:找不到默认值 引用的端点元素 合同“ILogbookManager” ServiceModel 客户端配置 部分。这可能是因为没有 已找到您的配置文件 应用程序,或者因为没有端点 与该合同匹配的元素可以 在客户端元素中找到。
如果我将 .Not.LazyLoad 添加到列表映射项,我将不会收到错误,但也不会收到任何客户端信息。
I am trying to retrieve a list of clients from the server (server using fluentNHibernate).
The client object is as follows:
[DataContract]
//[KnownType(typeof(System.Collections.Generic.List<ContactPerson>))]
//[KnownType(typeof(System.Collections.Generic.List<Address>))]
//[KnownType(typeof(System.Collections.Generic.List<BatchRequest>))]
//[KnownType(typeof(System.Collections.Generic.List<Discount>))]
[KnownType(typeof(EClientType))]
[KnownType(typeof(EComType))]
public class Client
{
#region Properties
[DataMember]
public virtual int ClientID { get; set; }
[DataMember]
public virtual EClientType ClientType { get; set; }
[DataMember]
public virtual string RegisterID {get; set;}
[DataMember]
public virtual string HerdCode { get; set; }
[DataMember]
public virtual string CompanyName { get; set; }
[DataMember]
public virtual bool InvoicePerBatch { get; set; }
[DataMember]
public virtual EComType ResultsComType { get; set; }
[DataMember]
public virtual EComType InvoiceComType { get; set; }
//[DataMember]
//public virtual IList<ContactPerson> Contacts { get; set; }
//[DataMember]
//public virtual IList<Address> Addresses { get; set; }
//[DataMember]
//public virtual IList<BatchRequest> Batches { get; set; }
//[DataMember]
//public virtual IList<Discount> Discounts { get; set; }
#endregion
#region Overrides
public override bool Equals(object obj)
{
var other = obj as Client;
if (other == null)
return false;
return other.GetHashCode() == this.GetHashCode();
}
public override int GetHashCode()
{
return ClientID.GetHashCode() | ClientType.GetHashCode() | RegisterID.GetHashCode() |
HerdCode.GetHashCode() | CompanyName.GetHashCode() | InvoicePerBatch.GetHashCode() |
ResultsComType.GetHashCode() | InvoiceComType.GetHashCode();// | Contacts.GetHashCode() |
//Addresses.GetHashCode() | Batches.GetHashCode() | Discounts.GetHashCode();
}
#endregion
}
I have already tried to remove the sub-lists, though even with this simplified version of the client I still run into the problem.
My fluent mapping is:
public class ClientMap : ClassMap<Client>
{
public ClientMap()
{
Table("Clients");
Id(p => p.ClientID);
Map(p => p.ClientType).CustomType<EClientType>(); ;
Map(p => p.RegisterID);
Map(p => p.HerdCode);
Map(p => p.CompanyName);
Map(p => p.InvoicePerBatch);
Map(p => p.ResultsComType).CustomType<EComType>();
Map(p => p.InvoiceComType).CustomType<EComType>();
//HasMany<ContactPerson>(p => p.Contacts)
// .KeyColumns.Add("ContactPersonID")
// .Inverse()
// .Cascade.All();
//HasMany<Address>(p => p.Addresses)
// .KeyColumns.Add("AddressID")
// .Inverse()
// .Cascade.All();
//HasMany<BatchRequest>(p => p.Batches)
// .KeyColumns.Add("BatchID")
// .Inverse()
// .Cascade.All();
//HasMany<Discount>(p => p.Discounts)
// .KeyColumns.Add("DiscountID")
// .Inverse()
// .Cascade.All();
}
The client method, seen below, connects to the server. The server retrieves the list, and everything looks right in the object, still, when it returns, the client doesn't receive anything (it receive a List object, but with nothing in it.
Herewith the calling method:
public List<s.Client> GetClientList()
{
try
{
s.DataServiceClient svcClient = new s.DataServiceClient();
svcClient.Open();
List<s.Client> clients = new List<s.Client>();
clients = svcClient.GetClientList().ToList<s.Client>();
svcClient.Close(); //when receiving focus from server, the clients object has a count of 0
return clients;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return null;
}
and the server method:
public IList<Client> GetClientList()
{
var clients = new List<Client>();
try
{
using (var session = SessionHelper.OpenSession())
{
clients = session.Linq<Client>().Where(p => p.ClientID > 0).ToList<Client>();
}
}
catch (Exception e)
{
EventLog.WriteEntry("eCOWS.Data", e.Message);
}
return clients; //returns a list with 1 client in it
}
The server method interface is:
[UseNetDataContractSerializer]
[OperationContract]
IList<Client> GetClientList();
For final reference, here are my client app.config entries:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IDataService" listenBacklog="10" maxConnections="10"
transferMode="Buffered" transactionProtocol="OleTransactions"
maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<readerQuotas maxDepth="51200000" maxStringContentLength="51200000"
maxArrayLength="51200000" maxBytesPerRead="51200000"
maxNameTableCharCount="51200000" />
<security mode="Transport"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:9000/eCOWS/DataService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IDataService"
contract="eCowsDataService.IDataService" name="NetTcpBinding_IDataService"
behaviorConfiguration="eCowsEndpointBehavior">
</endpoint>
<endpoint address="MEX"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="eCowsEndpointBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
and my server app.config:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding"
maxConnections="10" listenBacklog="10"
transferMode="Buffered" transactionProtocol="OleTransactions"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
sendTimeout="00:10:00" receiveTimeout="00:10:00">
<readerQuotas maxDepth="51200000" maxStringContentLength="51200000"
maxArrayLength="51200000" maxBytesPerRead="51200000"
maxNameTableCharCount="51200000" />
<security mode="Transport"/>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="eCows.Data.Services.DataService" behaviorConfiguration="eCowsServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/eCOWS/" />
<add baseAddress="net.tcp://localhost:9000/eCOWS/" />
</baseAddresses>
</host>
<endpoint address="DataService"
binding="netTcpBinding"
contract="eCows.Data.Services.IDataService"
behaviorConfiguration="eCowsEndpointBehaviour">
</endpoint>
<endpoint address="MEX"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="eCowsEndpointBehaviour">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="eCowsServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
<behavior name="MexBehaviour">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
I use to run into "socket closed / network or timeout" errors, and the trace showed clearly that on the callback it was looking for a listening endpoint, but couldn't find one. Anyway, after adding the UseNetSerializer that error went away, yet now I'm just not getting anything.
PS. If I add all the commented out List items, I still retrieve an entry from the DB, but also still not receive anything on the client.
If I remove the [UseNetDataContractSerializer] I get the following error(s) in the svclog:
WARNING: Description Faulted
System.ServiceModel.Channels.ServerSessionPreambleConnectionReader+ServerFramingDuplexSessionChannelWARNING: Description Faulted
System.ServiceModel.Channels.ServiceChannelERROR:
Initializing[eCows.Data.Models.Client#3]-failed
to lazily initialize a collection of
role:
eCows.Data.Models.Client.Addresses, no
session or session was closed...
ERROR: Could not find default
endpoint element that references
contract 'ILogbookManager' in the
ServiceModel client configuration
section. This might be because no
configuration file was found for your
application, or because no endpoint
element matching this contract could
be found in the client element.
If I add a .Not.LazyLoad to the List mapping items, I'm back at not receiving errors, but also not receiving any client information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 WCF,您需要使用具体类型而不是接口。首先使用
List
而不是IList
,或使用简单数组Client[]
。 (可以使用接口,但这需要更多工作。)这里的实际问题是 Linq 返回一个您无法在数据协定中指定的运行时类型。
服务器方法的示例副本:
With WCF you need to use concrete types not interfaces. Start off by using
List<Client>
instead ofIList<Client>
, or use simple arraysClient[]
. (It is possible to use interfaces, but thats a lot more work.)The actual problem here is that Linq returns an runtime type that you can't specify in your data contract.
Sample copy of your server method: