2010-04-13 8 views
3

J'ai un simple service basé sur REST pour lequel j'essaye de créer un proxy client en utilisant ChannelFactory. Je veux être sans fichier de configuration donc j'essaye de le faire dans le code et je crois que j'ai tout ce que j'avais dans .config sauf pour le comportement. Quelqu'un peut-il me dire comment je peux obtenir cette configuration en code C#:WCF utilisant ChannelFactory.CreateChannel avec le comportement webHttp

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

Voici le dépouillé C# code J'ai maintenant:

var endpoint = new EndpointAddress(urlCommServer); 
var binding = new WebHttpBinding(); 
return ChannelFactory<IInitialization>.CreateChannel(binding, endpoint); 

Répondre

12

essayer. Vous devez ajouter le comportement à ChannelFactory.

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

source

+0

Merci Kirk. J'ai également trouvé WebChannelFactory qui ajoute la liaison correcte et le comportement pour moi. – BrettRobi