Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF using ChannelFactory.CreateChannel with webHttp behavior

Tags:

wcf

wcf-client

I've got a simple REST based service for which I am trying to create a client proxy using ChannelFactory. I want to be without a configuration file so I am trying to do this in code and I believe I have everything I used to have in .config except for the behavior. Can anyone tell me how I can get this config into c# code:

  <behaviors>
   <endpointBehaviors>
    <behavior name="InitBehavior">
     <webHttp />
    </behavior>
   </endpointBehaviors>
  </behaviors>

Here is the stripped down c# code I have now:

var endpoint = new EndpointAddress(urlCommServer);
var binding = new WebHttpBinding();
return ChannelFactory<IInitialization>.CreateChannel(binding, endpoint);
like image 681
BrettRobi Avatar asked Feb 04 '26 18:02

BrettRobi


1 Answers

Try this. You need to add the behavior to the ChannelFactory.

var factory = new ChannelFactory<IInitialization>(binding, endpoint);
var behavior = new WebHttpBehavior();
factory.Endpoint.Behaviors.Add(behavior);
var channel = factory.CreateChannel();

source

like image 166
Kirk Broadhurst Avatar answered Feb 06 '26 08:02

Kirk Broadhurst