大佬教程收集整理的这篇文章主要介绍了如何在ASP.Net MVC应用程序中访问WCF服务?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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引用添加到Visual studio 2012:
>在解决方案资源管理器中右键单击项目
>选择添加 – >;服务参考 – >然后单击高级按钮… – >
>然后单击“添加Web引用…”按钮 – >然后在URL框中键入Web服务的地址.然后单击绿色箭头,Visual studio将发现您的Web服务并显示它们.
您可能已经知道上述内容,可能只需要一个通用的包装类,这使得在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 MVC应用程序中访问WCF服务?全部内容,希望文章能够帮你解决如何在ASP.Net MVC应用程序中访问WCF服务?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。