tags:

views:

703

answers:

3

Hi All, How can I create a client proxy without svcutil.exe or add service reference in wcf? I want to create a client proxy at compile time.

+4  A: 

If you have access to the service contract (the IService interface) in a separate DLL, you can add a reference to that service contract DLL and then do something like:

NetTcpBinding binding = new NetTcpBinding();
EndpointAddress address = new EndpointAddress("net.tcp://localhost:9000/YourService")

ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address);
IService proxy = factory.CreateChannel();

and then you have your programmatically created proxy, which you can now use as you wish.

Marc

marc_s
+3  A: 

You don't need to code generate (or use a configuration file full of WCF specifics).

First create the interface defining the service ([ServiceContract]) with any supporting data contracts in an assembly separate from the service implementation.

Reference the interface assembly in the client assembly.

Then need to create a client proxy, for IMyService:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(url);
ChannelFactory<IMyService> chanFac = new ChannelFactory<IMyService>(binding, endpoint);
IMyService clientProxy = chanFac.CreateChannel();
Richard
Sounds like a solution that I happen to use in one of my web services. :-)
Workshop Alex
+2  A: 

This might not be what you are looking for, but it's pretty interesting.

Vipul Modi has a library that allows you to call WCF services after downloading their WSDL, all at runtime. Vipul Modi's library (latest version)

Allows you to do this kind of thing:

Create the ProxyFactory specifying the WSDL URI of the service.

DynamicProxyFactory factory = new DynamicProxyFactory("http://localhost:8080/WcfSamples/DynamicProxy?wsdl");

Browse the endpoints, metadata, contracts etc.

factory.Endpoints factory.Metadata factory.Contracts factory.Bindings

Create DynamicProxy to an endpoint by specifying either the endpoint or contract name.

DynamicProxy proxy = factory.CreateProxy("ISimpleCalculator");

//OR

DynamicProxy proxy = factory.CreateProxy(endpoint);

Invoke operations on the DynamicProxy

dobule result = (dobule)proxy.CallMethod("Add", 1d ,2d);

Close the DynamicProxy proxy.Close();

Anderson Imes
Very cool indeed!
grenade