2014-09-26 56 views
1

我正在遷移服務引用以使用通道工廠。遷移到Channel Factory的服務引用

我將接口從服務實現分離到單獨的類庫中。

  • IIb類:IService
  • IIb類:Service
  • Web應用程序:參考IService

代碼:

配置

<bindings> 
     <basicHttpBinding> 
     <binding name="basicHttpBindingEndpoint" maxReceivedMessageSize="5242880"> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

類:

public class ProxyManager 
{ 
     internal static ConcurrentDictionary<string, ChannelFactory<IService>> proxies = new ConcurrentDictionary<string, ChannelFactory<IService>>(); 

     internal static ChannelFactory<IService> CreateChannelFactory() 
     { 
      Global.Logger.Info("ProxyManager:CreateChannelFactory"); 
      BasicHttpBinding basicHttpBinding = new BasicHttpBinding(); 
      basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 
      basicHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;    
      EndpointAddress endpointAddress = new EndpointAddress("http://domain/Service.svc"); 
      var channelFactory = new ChannelFactory<IService>(basicHttpBinding, endpointAddress); 
      channelFactory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; 
      return channelFactory; 
     } 

     internal static IService GetProxy(string key) 
     { 
      Global.Logger.Info("ProxyManager:GetProxy"); 
      return proxies.GetOrAdd(key, m => CreateChannelFactory()).CreateChannel(); 
     } 

     internal static bool RemoveProxy(string key) 
     { 
      Global.Logger.Info("ProxyManager:RemoveProxy"); 
      ChannelFactory<IService> proxy; 
      return proxies.TryRemove(key, out proxy); 
     } 
} 

全球:

public static IService ServiceProxy 
{ 
      get 
      { 
       return ProxyManager.GetProxy("Service"); 
      } 
} 

用法:

ServiceProxy.Method(); 

錯誤:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

我在這裏錯過了什麼?

回答

1

如果你看看你的配置,你會發現你已經定義了basicHttpBinding,其安全模式爲TransportCredentialOnly

但是,當您在代碼中創建basicHttpBinding時,您沒有指定該安全模式(並且默認值爲BasicHttpSecurityMode.None)。我認爲你需要你的電話施工變更爲:

BasicHttpBinding basicHttpBinding = 
    new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); 

那麼你應該有相同的設置,在你的配置