如何在ASP.Net MVC应用程序中访问WCF服务?
我对访问WCF的方式有疑问.我构建了一个安全的WCF服务,从数据库返回数据,它工作正常.现在我需要通过MVC访问这个Web服务(我对它没有足够的了解).
我在Stack Overflow上查了类似的问题,但是我找不到我需要的东西.我跟着这个link,但正如我所说,WCF从SQL返回数据,我用SQL连接我的WCF,当我使用这个例子时,我没有得到预期的结果. 我在MVC中调用的操作,它从SQL返回数据集类型 [OperationContract] DataSet GetAllbooks(string Title) 在Homecontrller的MVC我写道 ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); public ActionResult Index() { DataSet ds = obj.GetAllbooks(); ViewBag.AuthorList = ds.Tables[0]; return View(); } 在我看来,我写道 @{ ViewBag.Title = "AuthorList"; } <table> <tr><td>ISBN</td><td>Author</td><td>Price</td></tr> <%foreach (System.Data.DataRow dr in ViewBag.AuthorList.Rows) {%> <tr> <td><%=dr["ISBN"].ToString()%></td> <td><%=dr["Author"].ToString() %></td> <td><%=dr["Price"].ToString() %></td> </tr> <% } %> </table> 我没有得到任何结果 此外,WCF提供的一些服务需要接受来自用户的输入我如何能够做到这一点 谢谢. 解决方法
这是一个非常基本的问题,但一般来说,您可以在主Web.Config文件中添加Web服务引用和端点信息,但我怀疑您在调用WCF服务URL时遇到问题,如果是这样,我发布了一个泛型类的示例/ wrapper用于在MVC应用程序中调用WCF Web服务.
将Web引用添加到Visual Studio 2012: >在解决方案资源管理器中右键单击项目 您可能已经知道上述内容,可能只需要一个通用的包装类,这使得在MVC中轻松调用WCF Web服务.我发现使用泛型类效果很好.我不能相信它;在互联网上找到它并且没有归属.在http://www.displacedguy.com/tech/powerbuilder-125-wcf-web-services-asp-net-p3有一个完整的可下载源代码示例,它调用使用PowerBuilder 12.5.Net创建的WCF Web服务,但无论是在Visual Studio中创建还是在Visual Studio中创建的,在MVC中调用WCF Web服务的过程都是相同的. PowerBuilder的. 下面是在ASP.NET MVC中调用WCF Web服务的通用包装类的代码 当然,在我的不完整的例子后,不要模拟你的错误处理… using System; using System.ServiceModel; namespace LinkDBMvc.Controllers { public class WebService<T> { public static void Use(Action<T> action) { ChannelFactory<T> factory = new ChannelFactory<T>("*"); T client = factory.CreateChannel(); bool success = false; try { action(client); ((IClientChannel)client).Close(); factory.Close(); success = true; } catch (EndpointNotFoundException e) { LinkDBMvc.AppViewPage.apperror.LogError("WebService",e,"Check that the Web Service is running"); } catch (CommunicationException e) { LinkDBMvc.AppViewPage.apperror.LogError("WebService","Check that the Web Service is running"); } catch (TimeoutException e) { LinkDBMvc.AppViewPage.apperror.LogError("WebService","Check that the Web Service is running"); } catch (Exception e) { LinkDBMvc.AppViewPage.apperror.LogError("WebService","Check that the Web Service is running"); } finally { if (!success) { // abort the channel ((IClientChannel)client).Abort(); factory.Abort(); } } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-web-api – GlobalConfiguration – 尝试访问字段’
- .net – System.Management访问被拒绝
- asp.net-mvc-3 – 不能使用NuGet PagedList ASP.NET MVC#查
- asp.net-mvc-3 – 为什么会话是ASP.NET MVC应用程序的灾难?
- asp.net-mvc – 如何在ASP.net MVC中限制对控制器中某些操作
- 我到底是什么时候需要在经典的asp中将对象设置为空?
- 将ASP.NET MVC 3项目升级到ASP.NET MVC 4
- Windows Server AppFabric分布式缓存研究
- asp.net-mvc – 为什么IIS没有清理池回收导致网站内存异常的
- ASP.NET MVC中的QR代码生成
- asp.net-mvc-5 – 在MVC5中启用和使用角色管理
- asp.net – 如何在没有phsyical .ASMX文件的情况
- asp.net – .net MVC将linq数据从控制器传递到视
- ASP.NET Core技术研究-探秘依赖注入框架
- asp.net-mvc-3 – MVC 3客户端验证,模型绑定十进
- asp.net-core – 如何在.Net Core中将App.config
- entity-framework – ASP.NET动态数据:如何在下
- asp.net-mvc – 使用模型更改更新scaffolded控制
- asp.net-mvc – 验证URL是否映射到ASP.Net MVC中
- asp.net – HTTP错误500.23之后添加到我的本地网