2014-09-26 55 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); 

那么你应该有相同的设置,在你的配置