如何在ASP.NETMVC应用程序中访问WCF服务?

如何在ASP.NETMVC应用程序中访问WCF服务?,wcf,asp.net-mvc-4,Wcf,Asp.net Mvc 4,我对访问WCF的方式有疑问。我构建了一个安全的WCF服务,它可以从数据库返回数据,并且运行良好。现在我需要通过MVC访问这个web服务(我没有足够的知识) 我检查了关于堆栈溢出的类似问题,但没有找到我需要的。我遵循了这一点,但正如我所说的,WCF从SQL返回数据,我将WCF与SQL连接,当我使用这个示例时,我没有得到预期的结果 我在MVC中调用的操作,它从SQL返回数据集类型 [OperationContract] DataSet GetAllbooks(string Title) 在MVC的

我对访问WCF的方式有疑问。我构建了一个安全的WCF服务,它可以从数据库返回数据,并且运行良好。现在我需要通过MVC访问这个web服务(我没有足够的知识)

我检查了关于堆栈溢出的类似问题,但没有找到我需要的。我遵循了这一点,但正如我所说的,WCF从SQL返回数据,我将WCF与SQL连接,当我使用这个示例时,我没有得到预期的结果

我在MVC中调用的操作,它从SQL返回数据集类型

[OperationContract]
DataSet GetAllbooks(string Title)
在MVC的Homecontrller中,我写道

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>
@{
ViewBag.Title=“AuthorList”;
}
IsbNautorprice
我没有得到任何结果

另外,WCF提供的一些服务需要接受用户的输入,我如何才能做到这一点


谢谢。

这是一个非常基本的问题,但一般来说,您可以在主web.Config文件中添加web服务引用和端点信息,但我怀疑您在调用WCF服务URL时遇到了问题,如果是这样的话,我发布了一个通用类/包装器的示例,用于在MVC应用程序中调用WCF web服务

将Web引用添加到Visual Studio 2012:

  • 在解决方案资源管理器中的项目上单击鼠标右键
  • 选择添加–>服务参考–>然后单击高级按钮…–>
  • 然后单击“添加Web引用…”按钮–>然后在URL框中键入Web服务的地址。然后单击绿色箭头,VisualStudio将发现并显示您的Web服务 您可能已经知道上面的内容,并且可能只需要一个通用的包装器类,它使在MVC中调用WCF Web服务变得容易。我发现使用泛型类效果很好。我不能因此而受到赞扬;在互联网上找到的,没有归属。有一个完整的示例,其中包含可下载的源代码,它调用了使用PowerBuilder 12.5.Net创建的WCF Web服务,但是在MVC中调用WCF Web服务的过程是相同的,无论它是在Visual Studio还是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", e, "Check that the Web Service is running");
           }
           catch (TimeoutException e)
           {
              LinkDBMvc.AppViewPage.apperror.LogError("WebService", e, "Check that the Web Service is running");
           }
           catch (Exception e)
           {
              LinkDBMvc.AppViewPage.apperror.LogError("WebService", e, "Check that the Web Service is running");
           }
           finally
           {
             if (!success)
             {
               // abort the channel
               ((IClientChannel)client).Abort();
               factory.Abort();
             }
           }
         }
       }
     }
    
    使用系统;
    使用System.ServiceModel;
    命名空间LinkDBMvc.Controllers
    {
    公共类Web服务
    {
    公共静态无效使用(操作)
    {
    ChannelFactory工厂=新的ChannelFactory(“*”);
    T client=factory.CreateChannel();
    布尔成功=假;
    尝试
    {
    行动(客户);
    ((IClientChannel)client.Close();
    工厂关闭();
    成功=真实;
    }
    捕获(EndpointNotFounde异常)
    {
    LinkDBMvc.AppViewPage.apperror.LogError(“WebService”,即“检查Web服务是否正在运行”);
    }
    捕获(通信异常e)
    {
    LinkDBMvc.AppViewPage.apperror.LogError(“WebService”,即“检查Web服务是否正在运行”);
    }
    捕获(超时异常e)
    {
    LinkDBMvc.AppViewPage.apperror.LogError(“WebService”,即“检查Web服务是否正在运行”);
    }
    捕获(例外e)
    {
    LinkDBMvc.AppViewPage.apperror.LogError(“WebService”,即“检查Web服务是否正在运行”);
    }
    最后
    {
    如果(!成功)
    {
    //中止频道
    ((IClientChannel)client.Abort();
    factory.Abort();
    }
    }
    }
    }
    }
    
    Sara你好,欢迎来到StackOverflow。您的问题缺少会员能够帮助您的所需信息量。您需要包括您收到的任何错误消息,以及您已经编写的代码的相关部分。感谢您的重播,但您的解决方案对我不起作用,如果您有任何建议,我对MVCY了解不够您可能希望查看Microsoft网站上的一些免费教程和视频,他们有一些非常好的文档,可以让您对MVC有一个坚实的理解。