如何在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必需的字段验证器“丢失焦点”行为
- asp.net – JQGrid不显示数据
- asp.net-mvc-3 – 在MVC应用程序中将配置设置注入Javascrip
- ASP.NET %=% vs %:%
- .net-4.0 – Asp.Net 4.0 CacheItemPolicy滑动到期不正确?
- asp.net-mvc – 存储库模式中的纯POCO实体更新问题
- asp.net – 如何在Ember.js应用程序中使用ASP .NET Web API
- asp.net-mvc-3 – 在MVC视图中使用条件编译符号
- asp.net-web-api – 默认使用ASP.NET Web API返回json
- ASP.NET MVC – 查看主页,如何设置标题?
- iis-6 – 如何防止IIS覆盖我在ASP.NET中设置的文
- asp.net-mvc-4 – 当模型中存在一对多关系时,MVC
- ASP.net MVC Webforms视图引擎的缺点?
- asp.net – 使用表单验证模拟
- asp.net – Microsoft MVC“echo / print / outp
- asp.net – NewRelic – 如何忽略Web应用程序的一
- asp.net-mvc – 如何重定向HTTP到HTTPS在MVC应用
- 是否可以优化ASP.NET WebForms以便像ASP.NET MVC
- asp.net – 无法从代码隐藏中调用App_Code类
- asp.net-core – 将绝对文件路径转换为相对路径
