How To Use A WCF Service Without Adding Service Reference

In this article, you will learn how to use a WCF Service without adding service reference.

Let’s first establish what the purpose of this code is.
 
For this article, the purpose of the 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 one better solution also; i.e., by writing our 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.



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



  3. Now, delete the default created Interface and Class perspective "IService1.cs" and "Service1.cs".



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



  5. Set "public" access modifier to interface IPayment.
    1. public interface IPayment  
    2. {  
    3.   
    4. }   
  6. We need to add [ServiceContract] attribute to interface IPayment.
    1. [ServiceContract]  
    2. public interface IPayment {  
    3.   
    4. }  
  7. We declared one operation named "ReverseString" in IPayment interface and added [OperationContract] attribute.
    1. [ServiceContract]  
    2. public interface IPayment {  
    3.     [OperationContract]  
    4.     string ReverseString(string orignal);  
    5. }  

Step 2 - Add a new project to Solution Explorer

  1. Right click on the Solution Explorer of "WCFDemo" and go to Add > New Project.


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



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



  4. Add new WCF Service. Right click on WCFService (project) > Add > New Item. Then, select WCF Service on "Add New" dialog box and name it "User.SVC".



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



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



  7. On the "Reference Manager" dialog box, select "Solution" under "Project". Then, check into "ServiceLibrary" and click OK.



  8. Now, Reference of "ServiceLibrary" project is added to the "WCFService" project. It is time to implement our "ReverseString" operation. Paste the below code in User.svc.cs.
    1. //Declare Namespace: using ServiceLibrary;  
    2. public class User: IUser {  
    3.     public string ReverseString(string orignal) {  
    4.   
    5.         if (string.IsNullOrWhiteSpace(orignal))  
    6.             return string.Empty;  
    7.   
    8.         char[] charArray = orignal.ToCharArray();  
    9.         Array.Reverse(charArray);  
    10.         return new string(charArray);  
    11.   
    12.     }  
    13. }  

Step 3 - Access WCF Service without adding a proxy or Service Reference


It's the 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#. Then click Web >> ASP.NET Web Application. Name it as "WebApps" and click OK.

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



  4. Create a Controller by right clicking on "Controller" folder > Add > Controller. Then, select "MVC 5 Controller - Empty" >> Add >> Name it as "HomeController".

  5. By default, the "Get" Action Method is created as "Index".
    1. // GET: Home  
    2. public ActionResult Index() {  
    3.     return View();  
    4. }  
  6. Add Reference of "ServiceLibrary" into "WebApps". Right click on Reference of WebApps (project).
    Click on "Add Reference". On the "Reference Manager" dialog box, select "Solution" under "Project", check into "ServiceLibrary", and then click on OK.

    Now, the Reference of "ServiceLibrary" project is added to "WCFService" project.

  7. Finally, we are 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.
    1. public ActionResult Index() {  
    2.     //Create a ChannelFactory  
    3.     //Required Namespace: using System.ServiceModel;  
    4.     //Required Namespace: using ServiceLibrary;  
    5.     ChannelFactory < IUser > channelFactory = null;  
    6.   
    7.     try {  
    8.         //Create a binding of the type exposed by service  
    9.         BasicHttpBinding binding = new BasicHttpBinding();  
    10.   
    11.         //Create EndPoint address  
    12.         EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50419/User.svc");  
    13.   
    14.         //Pass Binding and EndPoint address to ChannelFactory  
    15.         channelFactory = new ChannelFactory < IUser > (binding, endpointAddress);  
    16.   
    17.         //Now create the new channel as below  
    18.         IUser channel = channelFactory.CreateChannel();  
    19.   
    20.         //Call the service method on this channel as below  
    21.         string result = channel.ReverseString("Suchit Khunt");  
    22.   
    23.         return View(result);  
    24.     } catch (TimeoutException) {  
    25.         //Timeout error  
    26.         if (channelFactory != null)  
    27.             channelFactory.Abort();  
    28.   
    29.         throw;  
    30.     } catch (FaultException) {  
    31.         if (channelFactory != null)  
    32.             channelFactory.Abort();  
    33.   
    34.         throw;  
    35.     } catch (CommunicationException) {  
    36.         //Communication error  
    37.         if (channelFactory != null)  
    38.             channelFactory.Abort();  
    39.   
    40.         throw;  
    41.     } catch (Exception) {  
    42.         if (channelFactory != null)  
    43.             channelFactory.Abort();  
    44.   
    45.         throw;  
    46.     }  
    47.   
    48. }  
That's it; it's all been done successfully.