2010-04-13 24 views
3

我有一個簡單的基於REST的服務,我嘗試使用ChannelFactory創建客戶端代理。我想沒有配置文件,所以我試圖在代碼中做到這一點,我相信我有我曾經在.config中的一切,除了行爲。誰能告訴我怎樣才能得到這個配置到C#代碼:WCF使用ChannelFactory.CreateChannel與webHttp行爲

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

這裏是剝離下來的C#代碼我現在有:

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

回答

12

試試這個。您需要將行爲添加到ChannelFactory。

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

source

+0

由於柯克。我還發現WebChannelFactory ,它爲我添加了正確的綁定和行爲。 – BrettRobi 2010-04-13 16:25:04

相關問題