Click here to Skip to main content
Click here to Skip to main content

Access WCF Service Without Service Reference

By , 27 Nov 2012
Rate this:
Please Sign up or sign in to vote.
                                          Change is inevitable
This absolutely applies to Client Requirements, but not to our code . In other words, the above statement is one of the 3 design principles of Software architecture.
                                System should be Fault Tolerant.
This means, our system should be adoptable for changes and shouldn't break for every reasonable change.
Reason for bringing up this point is a recent situation i undergo.

Situation:
I have my application consuming 4 WFC Services. And i need all 4 of them "Up and Running" all the time. In case of any failure i need to to intimated immediately. In simple words, i need a health monitoring system for all these services. Usually, i will create a system, which will consume these services via Service reference, and will invoke a method from each service and see if its responding or not.

Challenge: Service Reference
Yes, Service reference is the current challenge before us. Today, i have a set of services. May be tomorrow, all these services may be replaced by different services. Else couple of more services may got added. In both of the scenarios, i have to open my Heal monitoring system, and create service reference of new services.
This is a code change and it requires awful amount of time for testing the system.
You may think of generating a class file from wsdl using SVCUtil.exe, and then adding that class file in code. But it also required the code modification. Right?

Resolution: Access the WCF Service without service reference. 
This process is also called as "Dynamic Proxy Generation". While browsing for solution for my problem, i found a library written by a MSFT employee, which will take care of this. I would like to bring the same to your notice in this post.

1. Download the Project code attached in source.
2. Follow the steps mentioned below
3. Configure the services in config file and use the code snippet embedded below.
4. Have fun.

Source Code: Dynamic Proxy Library

1. Download code from above link. Look for "DynamicProxyLibrary"
2. Add the class library to your project. Change the Framework Version of the class library project to suite your actual project version. Else it will not allow you to refer the Project output.
3. Currently it is in 4.0 version. When you change the version, there are two 2 DLLs, which will go obsolete.
       System.ServiceModel.dll
       Syetm.Runtime.Serialization.dll
4. Remove the obsolete references and try adding those references from either GAC or from below path.
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\(version)"
5. Once you are done with refrencing the above class library, use the below piece of code to create a dynamic proxy for the WCF service.
DynamicProxyFactory factory = 
new DynamicProxyFactory("http://localhost/Dependency1WCFSvc/DateService.svc?wsdl");
DynamicProxy proxy = factory.CreateProxy("IDateService");
var result = proxy.CallMethod("GetDate", null);
In order to generate a dynamic proxy, you need to have the url, name of the Contract. For ease of implementation, i haven't dealt with security stuff right now. We will see it in upcoming posts.

Task accomplished. We are able to access a WCF Service without service reference.

Is it helpful for you? Kindly let me know your comments / questions.


License

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

About the Author

PratapReddyP
Web Developer
India India
I am an Aspiring Architect, having expertise in Design and Development. Good knowledge on TDD and DDD Approaches. Well versed with Agile methodology. Strong at Sharepoint, C# , Asp.Net, MVC, MVC Razor, WCF, Entity Framework, SQL, BI Tools.

Comments and Discussions

 
GeneralExcellent Perfect working 5 out of 5 :) Pinmemberdarshan.pandya8612-Feb-14 19:12 
QuestionCan not send any class type object PinmemberMember 331555221-Oct-13 23:59 
QuestionExelent PinmemberMember 33155526-Aug-13 22:23 
QuestionMaxReceivedMessageSize Pinmemberandreluizveras2-Jul-13 9:08 
GeneralMy vote of 5 Pinmemberwhwqs16-May-13 20:06 
Questionthis trick is not working, i getting an exception 'the associate <IContractName> endpoint address not found' PinmemberParag31222-Apr-13 0:34 
Questionmark Pinmemberwhwqs16-May-13 20:07 
QuestionIn Silverlight? PinmemberMember 842982927-Nov-12 21:37 
GeneralMy vote of 4 PinmemberSperneder Patrick27-Nov-12 18:59 
QuestionCan't download PinmemberMember 171879927-Nov-12 15:23 
AnswerRe: Can't download PinmemberPratapReddyP27-Nov-12 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 27 Nov 2012
Article Copyright 2012 by PratapReddyP
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid