Click here to Skip to main content
15,886,799 members
Articles / WCF
Technical Blog

Access WCF Service Without Service Reference

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
27 Nov 2012CPOL3 min read 24K   18   12
Access a WCF Service without a service reference.
                                          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.


This article was originally posted at http://pratapreddypilaka.blogspot.com/feeds/posts/default

License

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



Comments and Discussions

 
QuestionClient certificate authentication Pin
dikahong7-Nov-16 18:13
dikahong7-Nov-16 18:13 
GeneralExcellent Perfect working 5 out of 5 :) Pin
darshan.pandya8612-Feb-14 19:12
darshan.pandya8612-Feb-14 19:12 
QuestionCan not send any class type object Pin
Member 331555221-Oct-13 23:59
Member 331555221-Oct-13 23:59 
QuestionExelent Pin
Member 33155526-Aug-13 22:23
Member 33155526-Aug-13 22:23 
QuestionMaxReceivedMessageSize Pin
andreluizveras2-Jul-13 9:08
andreluizveras2-Jul-13 9:08 
GeneralMy vote of 5 Pin
whwqs16-May-13 20:06
whwqs16-May-13 20:06 
Questionthis trick is not working, i getting an exception 'the associate <IContractName> endpoint address not found' Pin
Parag31222-Apr-13 0:34
Parag31222-Apr-13 0:34 
Questionmark Pin
whwqs16-May-13 20:07
whwqs16-May-13 20:07 
QuestionIn Silverlight? Pin
Giovanni Caputo27-Nov-12 21:37
Giovanni Caputo27-Nov-12 21:37 
GeneralMy vote of 4 Pin
Sperneder Patrick27-Nov-12 18:59
professionalSperneder Patrick27-Nov-12 18:59 
nice idea,
but... only because it's possible, is it a good idea to call proxy generated methods ? further how to deal with DataContracts ...
QuestionCan't download Pin
Denis Ibragimov27-Nov-12 15:23
professionalDenis Ibragimov27-Nov-12 15:23 
AnswerRe: Can't download Pin
PratapReddyP27-Nov-12 15:33
PratapReddyP27-Nov-12 15:33 

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

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