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


i have question way access wcf. built secure wcf service returns data data base , works fine. need access web service through mvc (i not have enough knowledge it).

i checked similar questions on stack overflow did not find need. followed link said, wcf returns data sql, connect wcf sql , when used example don't expected result.

the operation invoke in mvc , return dataset type sql

[operationcontract] dataset getallbooks(string title) 

in homecontrller in mvc wrote

servicereference1.service1client obj = new servicereference1.service1client(); public actionresult index() {     dataset ds = obj.getallbooks();     viewbag.authorlist = ds.tables[0];     return view(); } 

and in view 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 result

also services provide wcf need accept input user how can

thank you.

this pretty basic question speaking can add web service reference , endpoint info in main web.config file suspect having trouble calling wcf service url, if posted example of generic class/wrapper calling wcf web services in mvc application.

add web reference visual studio 2012:

  1. right click project in solution explorer
  2. choose add–> service reference –> click advanced button... –>
  3. then click "add web reference…" button –> type address of web service url box. click green arrow , visual studio discover web services , display them.

you may have known above , might need generic wrapper class makes calling wcf web service easy in mvc. i've found using generic class works well. can't take credit it; found on internet , there no attribution. there complete example downloadable source code @ http://www.displacedguy.com/tech/powerbuilder-125-wcf-web-services-asp-net-p3 calls wcf web service made using powerbuilder 12.5.net, process of calling wcf web service in mvc same no matter if created in visual studio or powerbuilder.

here code generic wrapper class calling wcf web services in asp.net mvc

of course don't model error handling after 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 web service running");        }        catch (communicationexception e)        {           linkdbmvc.appviewpage.apperror.logerror("webservice", e, "check web service running");        }        catch (timeoutexception e)        {           linkdbmvc.appviewpage.apperror.logerror("webservice", e, "check web service running");        }        catch (exception e)        {           linkdbmvc.appviewpage.apperror.logerror("webservice", e, "check web service running");        }               {          if (!success)          {            // abort channel            ((iclientchannel)client).abort();            factory.abort();          }        }      }    }  } 

Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -