Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a WCF service in an ASP.Net MVC application?

I have question about the way to access the WCF. I built a secure WCF service that returns data from a data base and it works fine. Now I need to access this web service through MVC (I do not have enough knowledge about it).

I checked similar questions on Stack Overflow but I did not find what I need. I followed this link but as I said, WCF returns data from SQL, I connect my WCF with SQL and when I used this example I don't get the expected result.

the operation that i invoke in MVC and it return dataset type from SQL

[OperationContract]
DataSet GetAllbooks(string Title)

in Homecontrller in MVC i wrote

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
public ActionResult Index()
{
    DataSet ds = obj.GetAllbooks();
    ViewBag.AuthorList = ds.Tables[0];
    return View();
}

and in view i wrote

     @{
    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>

i don't get any result

Also some services that provide by WCF need to accept input from user how i can do it

Thank you.

like image 274
sara white Avatar asked May 14 '13 09:05

sara white


People also ask

How can I access WCF service?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.

What is WCF MVC?

What is WCF? WCF stands for Windows Communication Foundation. It is used to create a distributed and interoperable Applications. WCF is an effective platform for developing service-oriented applications.


1 Answers

This is a pretty basic question but generally speaking you can add a web service reference and endpoint info in the main Web.Config file but I suspect you are having trouble with calling the WCF service URL, if so I posted an example of a generic class/wrapper for calling WCF web services in an MVC application.

Add the Web Reference to Visual Studio 2012:

  1. Right click the project in the Solution Explorer
  2. Choose Add–> Service Reference –> Then click the Advanced Button... –>
  3. Then click the "Add Web Reference…" button –> then type the address of your Web Service into the URL box. Then click the green arrow and Visual Studio will discover your Web Services and display them.

You may have known the above already and might just need a generic wrapper class which makes calling the WCF Web Service easy in MVC. I've found that using the generic class works well. I can't take credit for it; found it on the internet and there was no attribution. There is a complete example with downloadable source code at http://www.displacedguy.com/tech/powerbuilder-125-wcf-web-services-asp-net-p3 that calls a WCF Web service that was made using PowerBuilder 12.5.Net, but the process of calling the WCF Web service in MVC is the same no matter if it was created in Visual Studio or PowerBuilder.

Here is the code for a generic wrapper class for calling WCF Web Services in ASP.NET MVC

Of course don't model your error handling after my incomplete example...

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();
         }
       }
     }
   }
 }
like image 177
Rich Bianco Avatar answered Jan 18 '23 10:01

Rich Bianco