gpt4 book ai didi

c# - 通过 WCF 服务调用 IEnumerable 时,我收到 The underlying connection was closed 错误

转载 作者:行者123 更新时间:2023-11-30 13:41:18 25 4
gpt4 key购买 nike

关于我的问题的一些背景知识...

我有一个网站和多个控制台应用程序,它们访问同一数据层 (Linq-To-Sql) 以设置/获取数据。控制台应用程序在我们的网络中运行,它们都通过 WCF 服务更新中央数据库。该网站用于向用户显示应用程序数据捕获的数据。

当我返回简单类型和返回我的自定义对象列表时,我的 WCF 服务可以正常工作,但每当我尝试返回 IEnumerable/IQueryable 中的任何内容时,我的服务都会因“底层连接已关闭错误”而崩溃.

我认为可以通过 WCF 服务返回 IEnumerables/IQueryables?是可能的,还是我只是错误地配置了我的服务?我使用的是 basicHttpBinding 而不是 wsHttpBinding,但我仍然不是 100% 在什么情况下最好使用不同的类型。

我的设置是这样的:

public class CageService : ICageRepository
{
public IEnumerable<Cage> GetAll()
{
var dc = new DataContext();
return dc .GetAll();
}
}


[ServiceContract]
public interface ICageRepository : IRepository<Cage>
{
[OperationContract]
IEnumerable<Cage> GetAll();

}

服务配置:

  <system.serviceModel>

<services>
<!-- Note: the service name must match the configuration name for the service implementation. -->
<service name="CageService" behaviorConfiguration="CageServiceTypeBehaviors" >
<host>
<baseAddresses>
<add baseAddress = "http://localhost/UHFWebsite/" />
</baseAddresses>
</host>
<!-- Add the following endpoint. -->
<!-- Note: your service must have an http base address to add this endpoint. -->
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />


<endpoint address ="" binding="basicHttpBinding" contract="Wavin.CageTracking.DataLayer.Interfaces.ICageRepository" />
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior name="CageServiceTypeBehaviors" >
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>

客户端配置:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICageRepository1" 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://localhost:2000/UHFServices/CageService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICageRepository1"
contract="CageRepositoryClient.ICageRepository" name="BasicHttpBinding_ICageRepository1" />
</client>
</system.serviceModel>

调用代码如下:

void method()
{
var client = new CageRepositoryClient();
CageListView.DataSource = client.GetAll();;
CageListView.DataBind();
}

最佳答案

您需要在接口(interface)的实现中将 Cage 类添加到服务的已知类型中。

[ServiceBehavior]
[ServiceKnownType(typeof(Cage))]
public class CageRepository: ICageRepository
{
...

关于c# - 通过 WCF 服务调用 IEnumerable 时,我收到 The underlying connection was closed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5553371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com