WCF http绑定

时间:2016-08-09 17:23:32

标签: wcf wcf-binding

我创建了一个wcf服务应用程序和一个asp.net mvc项目(作为客户端)。我通过添加服务引用文件将我的wcf服务添加到我的asp.net mvc引用中。我使用Entity Framework连接我的wcf应用程序中的DB。我有一个UserManagement.svc.cs服务。 这是我的UserManagement.svc.cs代码:

 public class UserManagement : IUserManagement
{
    iFlowEntities db = new iFlowEntities();

    public void AddRole(role role)
    {
        db.roles.Add(role);
        db.SaveChanges();
    }

    public List<role> RoleList()
    {
        List<role> roles;
        roles =  db.roles.ToList();
        return roles;
    }

}

我在asp.net mvc中的RoleList()动作的UserController中使用此服务,这就是动作代码:

    public ActionResult RoleList()
    {
        IList<UserManagement.role> roles = new List<UserManagement.role>();
        roles = UserClient.RoleList();
        return View("_RoleList",roles);
    }

和UserClient变量在控制器主体中定义,如:UserManagement.UserManagementClient UserClient = new UserManagement.UserManagementClient();

当我运行asp.net项目时,我收到此错误:

  

接收HTTP响应时发生错误   http://localhost:1730/UserManagement.svc。这可能是由于   服务端点绑定不使用HTTP协议。这也可以   是由于服务器中止了HTTP请求上下文   (可能是由于服务关闭)。

我用Google搜索并查看了多个答案并对其进行了测试,但对我没有结果,这个答案如thisthis以及this

这是我的wcf服务配置文件:

<?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 maxRequestLength ="262144" executionTimeout="103600" 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" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <!-- 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="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="wsHttpBinding" 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="iFlowEntities" connectionString="metadata=res://*/Model.DBContext.csdl|res://*/Model.DBContext.ssdl|res://*/Model.DBContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=iFlow;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

编辑:

是的,我将DataContractDataMember添加到我的班级ServiceContractOperationContract

这是我的客户端配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<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="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IUserManagement" />
        <binding name="BasicHttpBinding_IDepartmentManagement" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1730/UserManagement.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserManagement"
        contract="UserManagement.IUserManagement" name="BasicHttpBinding_IUserManagement" />
      <endpoint address="http://localhost:1730/DepartmentManagement.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDepartmentManagement"
        contract="DepartmentManagement.IDepartmentManagement" name="BasicHttpBinding_IDepartmentManagement" />
    </client>
  </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:2)

根据我的经验,我认为这个问题是Entity Framework问题。每当您在WCF服务中返回实体时,它都会尝试返回具有关系的实体,而WCF无法为客户端序列化实体。要解决此问题,只需在DbContext构造函数中禁用Proxy Creation选项:

public class iFlowEntities : DbContext
{
    public iFlowEntities()
    {
        Configuration.ProxyCreationEnabled = false;
    }
}