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

I have a question on how to access WCF. I built a secure WCF service that returns data from a database and it works great. Now I need to access this web service through MVC (I don't have enough knowledge about this).

I checked similar stack overflow questions, but I did not find what I needed. I followed this link but, as I said, WCF returns data from SQL, I connect my WCF to SQL, and when I used this example, I do not get the expected result.

operation I call in MVC and returns 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 mind 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 get no result

Also some services provided by WCF must accept user input, how can I do this

Thanks.

+6
source share
1 answer

This is a pretty simple question, but as a rule, you can add a link to the web service and endpoint information to the main Web.Config file, but I suspect you are having trouble calling the WCF service URL, so I posted An example is a common class / wrapper for invoking WCF web services in an MVC application.

Add Web Link in Visual Studio 2012:

  • Right-click the project in Solution Explorer
  • Select Add-> Service Reference → Then click the Advanced ... button →
  • Then click the "Add Web Link ..." button → then enter the web service address in the URL field. Then click the green arrow and Visual Studio will open your web services and display them.

You may already have known above, and you might need a generic wrapper class that makes it easier to call the WCF web service in MVC. I found that using a generic class works well. I cannot take responsibility for this; found it on the internet and there was no attribution. A complete downloadable example is at http://www.displacedguy.com/tech/powerbuilder-125-wcf-web-services-asp-net-p3 , which calls the WCF web service that was created using PowerBuilder 12.5. Net, but the process of invoking the WCF Web service in MVC is the same, regardless of whether it was created in Visual Studio or PowerBuilder.

Here is the code for a generic wrapper class to call WCF web services in ASP.NET MVC

Of course, don't model 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(); } } } } } 
+6
source

Source: https://habr.com/ru/post/944956/


All Articles