none
How to consume a default WCF web service in a default ASP.NET MVC project?

    Question

  • How can I consume a default WCF web service, right out of the box from VS 2010 in a default ASP.NET MVC project right out of the box from VS 2010?

    I know how to start up the WCF web service and add the service reference in the ASP.NET MVC project. So what I want to know is exactly what code I shall write in the ASP.NET MVC project to be able to consume the WCF service?

    This question should be extremely simple, I am making a student project and I want to use a default ASP.NET MVC project as a client for a default WCF service. Basically I am just asking for a "Hello World" of an ASP.NET MVC project consuming a default WCF project. No configuration, no security, just right out of the box!


    rune007
    Friday, April 01, 2011 3:25 PM

Answers

  • Hi Tanvir Huda

    Thank you for your response, I found the solution to my problem in this article which is a ASP.NET MVC project which is consuming a WCF web service:

    http://www.codeproject.com/KB/aspnet/aspnetmvc_bugtracker_v4.aspx

    I was then able to finally write my "Hello World" ASP.NET MVC - WCF. In a default ASP.NET MVC project I wrote the code:

    public ActionResult About()
        {
          ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
          ViewData["result"] = proxy.GetData(4);
          return View(ViewData);
        }
    
    This was the code I was looking for, extremely simple, I admit. Now I am interested in a more clever way of writing the code, so I don't have to write the same code in every action method.
    rune007
    • Marked as answer by rune007 Friday, April 01, 2011 4:44 PM
    Friday, April 01, 2011 4:44 PM

All replies

  • you should add the wcf service as an web service referencre.

     

    then in your mvc application, this web service client will act as a Model.

    now from your controller object invoke this model functionality. 

    Hope that helps. 


    Tanvir Huda
    Friday, April 01, 2011 4:28 PM
  • Hi Tanvir Huda

    Thank you for your response, I found the solution to my problem in this article which is a ASP.NET MVC project which is consuming a WCF web service:

    http://www.codeproject.com/KB/aspnet/aspnetmvc_bugtracker_v4.aspx

    I was then able to finally write my "Hello World" ASP.NET MVC - WCF. In a default ASP.NET MVC project I wrote the code:

    public ActionResult About()
        {
          ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
          ViewData["result"] = proxy.GetData(4);
          return View(ViewData);
        }
    
    This was the code I was looking for, extremely simple, I admit. Now I am interested in a more clever way of writing the code, so I don't have to write the same code in every action method.
    rune007
    • Marked as answer by rune007 Friday, April 01, 2011 4:44 PM
    Friday, April 01, 2011 4:44 PM
  • Hi

    My experience is that you should have a using around the call. The reason for this is that errors can cause the connection to be kept up.

    Example

    ResultBE resut = null;

    using (ServiceProxy proxy = new ServiceProxy())

    {

    result = proxy.GetData(input)

    }

    In this way you are sure that connection is closed.

     

    Sunday, April 03, 2011 5:24 PM
  • I know that my question is too late 

    but I want to ask , where I must write the configuration of the service  in web asp.net application mvc

    I have 2 web.config files 

    thanks 


    • Edited by RProgrammer Tuesday, April 23, 2013 7:28 PM
    Tuesday, April 23, 2013 7:27 PM
  • In the root Web.config file at the solution level.
    Thursday, May 02, 2013 12:43 PM

  • It's talking about the root web.config, and you may not be using a ASP.NET UI Web.config. But nevertheless, the entries need to be in the root Web.config ow a ASP.NET Web service.

    http://msdn.microsoft.com/en-us/library/we2zh3e0(v=vs.71).aspx

    Thursday, May 02, 2013 12:58 PM