3

I would like to use the ChannelFactory to create a WCF Client for a given endpoint.

The problem is that I don't have a web.config or app.config, but I do have the whole <system.serviceModel> XML block in a string.

I do not want to manually parse that out and programatically create the BIndings and Endpoints when I already have the configuration.

Is there a way to tell the ChannelFactory to just use that block as it's configuration? Or at least create a ServiceEndpoint?

Michael Stum
  • 177,530
  • 117
  • 400
  • 535

3 Answers3

1

There's a technique described in this blog entry.

It isn't as simple as one line of code, but at least it's working at a level higher than raw XML.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
0

You can manually create binding and endpoint address to create an instance of the CHannelFactory, something like:

   BasicHttpBinding binding = new BasicHttpBinding() { 
       Name = "Bindingname"
       // Goes all the necessary members to set.
   };

   EndpointAddress endpoint = new EndpointAddress("http://serviceendpoint.com");
   ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, endpoint);

Then you can try using factory.CreateChannel() to explore the members of it before your ServiceEnpoint, Behaviors, etc.

Hope this help, thanks.

Peyton Crow
  • 872
  • 4
  • 9
  • Thanks, but as I wrote: `I do not want to manually parse that out and programatically create the Bindings and Endpoints when I already have the configuration.`. I'm using the programmatic way already, but I want to stop duplicating code as I have a `` XML Block already. – Michael Stum Apr 20 '11 at 01:52
  • My bad, I thought you might convert it to generics to lessen the code in creating instance of `ChannelFactory` for every contract. – Peyton Crow Apr 20 '11 at 02:11
0

Found a better solution: ConfigurationChannelFactory<TChannel> allows creating a WCF Channel from a custom configuration.

Michael Stum
  • 177,530
  • 117
  • 400
  • 535