Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4913 for trunk


Ignore:
Timestamp:
11/23/10 18:18:24 (14 years ago)
Author:
cneumuel
Message:

#1298 added the possibility to use a ChannelFactory in HeuristicLab.Clients.Common.ClientFactory (needed for Hive single sign on)

Location:
trunk/sources/HeuristicLab.Clients.Common/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Clients.Common/3.3/ClientFactory.cs

    r4406 r4913  
    2323using System.ServiceModel;
    2424using HeuristicLab.Clients.Common.Properties;
     25using System.ServiceModel.Description;
     26using System.Collections;
     27using System.Collections.Generic;
     28using HeuristicLab.Common;
    2529
    2630namespace HeuristicLab.Clients.Common {
     
    3943      where T : ClientBase<I>, I
    4044      where I : class {
     45      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
     46    }
     47    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
     48      where T : ClientBase<I>, I
     49      where I : class {
    4150      T client;
    4251      if (string.IsNullOrEmpty(endpointConfigurationName)) {
     
    4756
    4857      if (!string.IsNullOrEmpty(remoteAddress)) {
    49         client.Endpoint.Address = new EndpointAddress(remoteAddress);
     58        SetEndpointAddress(client.Endpoint, remoteAddress);
    5059      }
    5160
    52       client.ClientCredentials.UserName.UserName = Settings.Default.UserName;
    53       client.ClientCredentials.UserName.Password = Settings.Default.Password;
     61      client.ClientCredentials.UserName.UserName = userName;
     62      client.ClientCredentials.UserName.Password = password;
    5463      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
    5564      return client;
    5665    }
     66
     67    public static Disposable<I> CreateClient<I>(string endpointConfigurationName) where I : class {
     68      return CreateClient<I>(endpointConfigurationName, null);
     69    }
     70    public static Disposable<I> CreateClient<I>(string endpointConfigurationName, string remoteAddress) where I : class {
     71      return CreateClient<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
     72    }
     73    public static Disposable<I> CreateClient<I>(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class {
     74      ChannelFactory<I> factory = GetChannelFactory<I>(endpointConfigurationName, userName, password);
     75
     76      if (!string.IsNullOrEmpty(remoteAddress)) {
     77        SetEndpointAddress(factory.Endpoint, remoteAddress);
     78      }
     79      Disposable<I> disposable = new Disposable<I>(factory.CreateChannel());
     80      disposable.OnDisposing += new EventHandler<EventArgs<object>>(disposable_OnDisposing);
     81
     82      return disposable;
     83    }
     84
     85    private static void disposable_OnDisposing(object sender, EventArgs<object> e) {
     86      DisposeCommunicationObject((ICommunicationObject)e.Value);
     87      ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(disposable_OnDisposing);
     88    }
     89
     90    private static IDictionary<ChannelProperties, ChannelFactory> channelFactoryCache = new Dictionary<ChannelProperties, ChannelFactory>();
     91    private static ChannelFactory<I> GetChannelFactory<I>(string endpointConfigurationName, string userName, string password) where I : class {
     92      ChannelProperties key = new ChannelProperties(typeof(I), endpointConfigurationName, userName, password);
     93      if (!channelFactoryCache.ContainsKey(key)) {
     94        channelFactoryCache.Add(key, new ChannelFactory<I>(endpointConfigurationName));
     95        channelFactoryCache[key].Credentials.UserName.UserName = userName;
     96        channelFactoryCache[key].Credentials.UserName.Password = password;
     97        channelFactoryCache[key].Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
     98      }
     99      return (ChannelFactory<I>)channelFactoryCache[key];
     100    }
     101
     102    public static void DisposeCommunicationObject(ICommunicationObject obj) {
     103      if (obj != null) {
     104        if (obj.State != CommunicationState.Faulted && obj.State != CommunicationState.Closed) {
     105          try { obj.Close(); }
     106          catch { obj.Abort(); }
     107        } else {
     108          obj.Abort();
     109        }
     110      }
     111    }
     112
     113    /// <summary>
     114    /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
     115    /// </summary>
     116    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
     117      EndpointAddressBuilder endpointAddressbuilder = new EndpointAddressBuilder(endpoint.Address);
     118      UriBuilder uriBuilder = new UriBuilder(endpointAddressbuilder.Uri);
     119      uriBuilder.Host = remoteAddress;
     120      endpointAddressbuilder.Uri = uriBuilder.Uri;
     121      endpoint.Address = endpointAddressbuilder.ToEndpointAddress();
     122    }
     123  }
     124
     125  internal struct ChannelProperties {
     126    public Type type;
     127    public string endpointConfigurationName;
     128    public string userName;
     129    public string password;
     130
     131    public ChannelProperties(Type type, string endpointConfigurationName, string userName, string password) {
     132      this.type = type;
     133      this.endpointConfigurationName = endpointConfigurationName;
     134      this.userName = userName;
     135      this.password = password;
     136    }
    57137  }
    58138}
  • trunk/sources/HeuristicLab.Clients.Common/3.3/HeuristicLab.Clients.Common-3.3.csproj

    r4387 r4913  
    115115  <ItemGroup>
    116116    <Compile Include="ClientFactory.cs" />
     117    <Compile Include="Disposable.cs" />
    117118    <Compile Include="HeuristicLabClientsCommonPlugin.cs" />
    118119    <Compile Include="PasswordDialog.cs">
     
    140141  </ItemGroup>
    141142  <ItemGroup>
     143    <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj">
     144      <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project>
     145      <Name>HeuristicLab.Common-3.3</Name>
     146    </ProjectReference>
    142147    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    143148      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • trunk/sources/HeuristicLab.Clients.Common/3.3/HeuristicLabClientsCommonPlugin.cs.frame

    r4865 r4913  
    2828  [Plugin("HeuristicLab.Clients.Common", "3.3.2.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Clients.Common-3.3.dll", PluginFileType.Assembly)]
     30  [PluginDependency("HeuristicLab.Common", "3.3")]
    3031  public class HeuristicLabClientsCommonPlugin : PluginBase {
    3132  }
Note: See TracChangeset for help on using the changeset viewer.