Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to use a WCF Service without adding Service Reference in MVC

5.00/5 (5 votes)
19 Jul 2016CPOL3 min read 26.2K   228  
How to use a WCF Service without adding a Proxy or Service Reference in ASP.net MVC Application

Introduction

Let’s establish what the purpose of code is in the first place.

For this article, the purpose of code is to use a WCF Service without adding a proxy or Service Reference in ASP.NET MVC Application. There is a way to do this by using the Service Model Metadata Utility Tool (SVCUTIL.EXE), but we have also one better solution by writing our code.

Using the code

First of all we need to place our Service Contracts and Data Contracts into a shared library (WCF Service Library), which can be consumed by our client application.

Step 1: Create a New Project (WCF Service Library)

1) On the File menu, click New Project.

Image 1

2) In the New Project dialog box under project types, expand Visual C#, and then click on WCF and select WCF Service Library . Then in the Name box, type "ServiceLibrary" and in the Solution name, type "WCFDemo" then click on OK.

Image 2

3) Now, delete the default created interface and class perspective "IService1.cs" and "Service1.cs"

Image 3

4) Add a New Interface by right-clicking on ServiceLibrary(project) > Add > New Item then select "Interface", name it "IUser.cs" then click on ADD.

Image 4

5) Set the public access modifier to interface IPayment

C#
public interface IPayment
{

}

6) We need to add the [ServiceContract] attribuite to interface IPayment

C#
[ServiceContract]
public interface IPayment
{

}

7) We declare a one operation named as ReverseString in IPayment interface and add the [OperationContract] attribute.

C#
[ServiceContract]
public interface IPayment
{
    [OperationContract]
    string ReverseString(string orignal);
}

Step 2: Add a new project to solution Explorer

1) Right-click on solution explorer "WCFDemo"> Add> New Project

Image 5

2) In the New Project dialog box under Project types, expand Visual C#, and then click on WCF and select WCF Service Application. Then In the Name box, type "WCFService" then click on OK.

Image 6

3) Delete default created Interface and Service perspective "IService1.cs" and "Service1.svc"

Image 7

4) Add a new WCF Service. Right-click on WCFService (project) > Add > New Item. Then Select WCF Service on Add New Dialogbox and name it "User.SVC"

Image 8

5) Delete the interface IUser.cs from WCFService (project). Because we already added this interface on ServiceLibrary (project).

Image 9

6) Add the reference of "ServiceLibrary" into "WCFService". Right-click on the reference of WCFService (project). Then click on Add Reference.

Image 10

7) On the Reference Manager Dialog box, select "Solution" under "Project" then check into "ServiceLibrary" then click on OK.

Image 11

8) Now, the reference of "ServiceLibrary" project is added to the "WCFService" project. Now it's time to implement out ReverseString operation. Paste the below code in User.svc.cs

C#
//Declare Namespace: using ServiceLibrary;
public class User : IUser
    {
        public string ReverseString(string orignal)
        {

            if (string.IsNullOrWhiteSpace(orignal))
                return string.Empty;

            char[] charArray = orignal.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
            
        }
    }

Step 3 - It's Final Step To Access WCF Service without Adding a Proxy or Service Reference in ASP.NET MVC Application

1) Right-click on solution explorer "WCFDemo" > Add > New Project

2) In the New Project dialog box under Project types, expand Visual C#, and then click Web and select ASP.NET Web Application. Then name it "WebApps" then click on OK.

3) Now in the dialog box, click on the "Empty" under the ASP.NET 4.5.2 Templates, then check on "MVC" checkbox then click on OK.

Image 12

4) Create a controller by right-clicking on the "Controller" folder > Add > Controller. Then select "MVC 5 Controller - Empty". Then click on Add, and finally name it "HomeController".

5) Default one get action method created as Index

C#
// GET: Home
public ActionResult Index()
{
     return View();
}

6) Add a reference of "ServiceLibrary" into "WebApps". Right-click on the reference of WebApps (project). Then click on Add Reference. On the Reference Manager Dialog box, select "Solution" under "Project", then check into the "ServiceLibrary" then click on OK. Now, the reference of "ServiceLibrary" project is added to "WCFService" project.

7) Finally, all done. Now it's time to access our created operation method without using proxy or Service Reference.

Paste the below code in your Index method.

C#
public ActionResult Index()
       {
           //Create a ChannelFactory
           //Required Namespace: using System.ServiceModel;
           //Required Namespace: using ServiceLibrary;
           ChannelFactory<IUser> channelFactory = null;

           try
           {
               //Create a binding of the type exposed by service
               BasicHttpBinding binding = new BasicHttpBinding();

               //Create EndPoint address
               EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50419/User.svc");

               //Pass Binding and EndPoint address to ChannelFactory
               channelFactory = new ChannelFactory<IUser>(binding, endpointAddress);

               //Now create the new channel as below
               IUser channel = channelFactory.CreateChannel();

               //Call the service method on this channel as below
               string result = channel.ReverseString("Suchit Khunt");

               return View(result);
           }
           catch (TimeoutException)
           {
               //Timeout error
               if (channelFactory != null)
                   channelFactory.Abort();

               throw;
           }
           catch (FaultException)
           {
               if (channelFactory != null)
                   channelFactory.Abort();

               throw;
           }
           catch (CommunicationException)
           {
               //Communication error
               if (channelFactory != null)
                   channelFactory.Abort();

               throw;
           }
           catch (Exception)
           {
               if (channelFactory != null)
                   channelFactory.Abort();

               throw;
           }

       }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)