通过WCF服务调用IEnumerable时,我收到底层连接已关闭错误

Jon*_*nes 3 c# wcf

我的问题有点背景......

我有一个网站和多个控制台应用程序访问相同的数据层(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();

}
Run Code Online (Sandbox Code Playgroud)

服务配置:

  <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>
Run Code Online (Sandbox Code Playgroud)

客户端配置:

<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>
Run Code Online (Sandbox Code Playgroud)

调用代码看起来像:

void method()
{
            var client = new CageRepositoryClient();
            CageListView.DataSource = client.GetAll();;
            CageListView.DataBind();
}
Run Code Online (Sandbox Code Playgroud)

fla*_*ayn 6

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

[ServiceBehavior]
[ServiceKnownType(typeof(Cage))] 
public class CageRepository: ICageRepository
{
...
Run Code Online (Sandbox Code Playgroud)