WCF using ChannelFactory.CreateChannel with webHttp behavior

I have 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 try to do this in code, and I believe that I have everything I had in .config, except for the behavior. Can someone tell me how I can get this config in C # code:

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

Here is the stripped-down C # code that I have now:

var endpoint = new EndpointAddress(urlCommServer);
var binding = new WebHttpBinding();
return ChannelFactory<IInitialization>.CreateChannel(binding, endpoint);
+3
source share
1 answer

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

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

source

+12
source

Source: https://habr.com/ru/post/1740853/


All Articles