WCF服务通信异常

时间:2015-04-07 15:48:22

标签: wcf model-view-controller soap service

我正在尝试从我的mvc网站访问我在服务库中创建的WCF服务,但是我一直收到通信异常,这个方法应该只从数据库返回一行,因此我不会#39 ; t相信它是一个内存问题,我设法使用这个服务来执行数据库的插入,但任何检索方法都失败了。

我的代码可能会在下面找到。

UserService

IEnumerable<COUNTRY> IUserService.getCountries()
{
    return new UsersRepository().getCountries();
}

IUserService

[OperationContract]
IEnumerable<COUNTRY> getCountries();

服务网络配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="DSAEntities" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=HOODED;initial catalog=DSA;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

网站webconfig

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IUserService" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="32"
          maxArrayLength="200000000"
          maxStringContentLength="200000000"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:50004/UserService.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IUserService" contract="UserReference.IUserService"
        name="BasicHttpBinding_IUserService" />
    </client>
  </system.serviceModel>

我的代码

 public UserReference.UserServiceClient client = new UserReference.UserServiceClient();
   IEnumerable<ROLE> roles = client.getRoles();

1 个答案:

答案 0 :(得分:0)

在数据上下文丢失之前强制枚举数据。 而不是

IEnumerable<COUNTRY> IUserService.getCountries()
{
    return new UsersRepository().getCountries();
}

DO

IEnumerable<COUNTRY> IUserService.getCountries()
{
    return new UsersRepository().getCountries().ToList();
}