Hot to create proxy class on the fly dynamically

0 votes
asked Jun 13, 2013 in WCF by anonymous

I am new to WCF and started creating some service, consuming some services etc. To consume a wcf service, I am using "Add Web Reference". But it is creating proxy class including so many other files in the application, also whenever there is an update I need update proxy by updating the web reference. Is there a way to create the proxy class through the code without adding web reference?


1 Answer

+1 vote
answered Jun 15, 2013 by anonymous
selected Jun 18, 2013 by administrator
Best answer

Creating proxy on the fly is very easy. But one thing to keep in mind is, we need to have copy of the Service Contract and the DataContracts in the client application. So that, we can call the service and get the result back. If you choose the Operation Contracts to reture data in string or xml or some onther common format, then the client application should take care of reading data from the result.

To answer your question, creating proxy from the code can be donw using ChannelFactory for normal binding. If you use duplex binding, then you may want to use DuplexChannelFactory.

Below is the sample code

class Program
        static void Main(string[] args)
            var channel = new ChannelFactory<ICalcServices>(new WSHttpBinding(), new EndpointAddress("http://localhost:8000/CalcServices"));
            var proxy = channel.CreateChannel();
            Console.WriteLine(proxy.Add(1, 2));
            Console.WriteLine(proxy.Sub(2, 1));

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015