Thursday, October 31, 2013

how to add wcf service to asp net mvc project

how to add wcf service to asp net mvc project

1) Create a New Project Select ASP.NET MVC 4 APPLICATION
2) Add New Item -> select WCF Service
         name it as "wcfservice1"

     Service Interface:

    public interface IService1
        String DoWork();

     Service Class:

  [AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed)]

    public class Service1 : IService1
        public String DoWork()
            return "HELLO WCF SERVICE";

3) just right click on wcfService.svc run it, U will get medata data.


4)  How to Consume WCF Service in ASP.NET MVC 4

5)  Add Service Reference 

right click on Reference folder -> Add Service Reference.,

enter WCF Service URL i.e got in Step 3)

It will generate WCF Service Proxy class through which service methods can be called.

6) Add Controller Class Name it as Default 2

7) Create WCF Service Client in Default2 controller class

      Service1Client client = new Service1Client();

8)  Add following code in Index Action Method

      public ViewResult Index(){
            ViewBag.WCFDATA = client.DoWork();
            return View("~/Views/Default2/Index.cshtml",(object)client.DoWork());

9) Add View to Index Method(right click on Index Method click on Add View)

Add following code to this View

How to consume WCF Service in ASP.NET MVC @ViewBag.WCFDATA
<b>Using Model Class<</b>

10) Just run the Controller



How to consume WCF Service in ASP.NET MVC HELLO WCF SERVICE

Using Model Class< HELLO WCF SERVICE