Access WCF Service Without 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.

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 2 dlls, which will go obsolete.
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 refreing above class library, use the below piece of code to create a Dyanmic 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.

No comments:

Post a Comment