WCF http 绑定

编程入门 行业动态 更新时间:2024-10-18 16:48:43
本文介绍了WCF http 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I create a wcf service application and a asp mvc project(as client). I added my wcf service via Add Service Reference file to my asp 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 mvc 的 RoleList() 操作中的 UserController 中使用此服务,这是该操作代码:

And I use this service in my UserController in RoleList() action in asp 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 项目时出现此错误:

When I run asp project I get this error:

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

An error occurred while receiving the HTTP response to 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).

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

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 go.microsoft/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>

是的,我将 DataContract 和 DataMember 添加到我的类以及 ServiceContract 和 OperationContract.

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 go.microsoft/fwlink/?LinkId=301880 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit go.microsoft/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="localhost:1730/UserManagement.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserManagement" contract="UserManagement.IUserManagement" name="BasicHttpBinding_IUserManagement" /> <endpoint address="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 绑定

本文发布于:2023-06-05 23:04:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   WCF   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!