WCF http 绑定 [英] WCF http binding

查看:28
本文介绍了WCF http 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create a wcf service application and a asp.net mvc project(as client). I added my wcf service via Add Service Reference file to my asp.net mvc reference. I use Entity Framework to connect DB in my wcf application. I have a UserManagement.svc.cs service. This is my UserManagement.svc.cs codes:

 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 中使用此服务,这是该操作代码:

And I use this service in my UserController in RoleList() action in asp.net mvc and this is that action code:

    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();

and UserClient variable define in controller body like: UserManagement.UserManagementClient UserClient = new UserManagement.UserManagementClient();

当我运行 asp.net 项目时出现此错误:

When I run asp.net project I get this error:

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

An error occurred while receiving the HTTP response to http://localhost:1730/UserManagement.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).

我在谷歌上搜索并看到了多个答案并对其进行了测试,但没有结果,这个答案就像 thisthisthis.

I googled and see multiple answer and test them but don't have result for me and this answer like this and this and this.

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

And this is my wcf service config file:

<?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.

Yes, I add DataContract and DataMember to my class and ServiceContract and OperationContract.

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

This is my client config file:

<?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>

推荐答案

我认为这个问题是 Entity Framework 的问题.每当您在 WCF 服务中序列化实体时,它也会尝试加载子实体,而 WCF 无法序列化它们.要解决此问题,只需在 DbContext 构造函数中禁用 Proxy Creation 选项:

I think this issue is an Entity Framework matter. Whenever you serialize an entity in WCF service, it tries to load child entities as well, while WCF can't serialize them. To solve this issue simply disable Proxy Creation option in DbContext constructor:

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

这篇关于WCF http 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆