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.
views:
703answers:
3If 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
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();
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();