tags:

views:

1223

answers:

2

I need to consume a wcf service dynamically when all i know is its URL. I do not have the option of creating a service reference or web reference as my client side code picks up the URL from a config file. What classes and methods can i use from the System.ServiceModel namespace for doing so.

A: 

If you know the contract then you can do something like:

using (WebChannelFactory<IService> wcf = new WebChannelFactory<IService>(new Uri("http://localhost:8000/Web")))

More here

Rinat Abdullin
This needs the client side to know the IService interface
Addi
+2  A: 

If you don't have the service interface, you must, at the very least, have an idea as to what the messages the server expects look like; otherwise it be pretty hard to do :)

But there is certainly a way to do that. You can start by creating the raw message the server expects as input, and create it in a Message object (I mean System.ServiceModel.Channels.Message). Make sure that you set all the necessary headers to it, depending on what binding you expect to use to call the client (like setting the right credentials, the right MessageVersion and so on).

Then you can simply create a channel factory using one of the standard, generic channel shapes like IRequestChannel or IInputChannel (for one-way services), and use the channel factory to create a new channel and invoke the service. I.e. something like:

Message input = Message.CreateMessage( .... );

ChannelFactory<IRequestChannel> factory = new ChannelFactory<IRequestChannel>(binding, endpoint);
IRequestChannel channel - factory.CreateChannel();

Message output = channel.Send(input);
tomasr