Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/16/11 00:23:01 (13 years ago)
Author:
swagner
Message:

Restructured and formatted code of ClientFactory (#1298)

File:
1 edited

Legend:

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

    r5527 r5701  
    2828namespace HeuristicLab.Clients.Common {
    2929  public static class ClientFactory {
     30    #region CreateClient Methods
    3031    public static T CreateClient<T, I>()
    3132      where T : ClientBase<I>, I
     
    6263      return client;
    6364    }
     65    #endregion
    6466
    65     public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName) where I : class {
     67    #region CreateChannelFactory Methods
     68    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName)
     69      where I : class {
    6670      return CreateChannelFactory<I>(endpointConfigurationName, null);
    6771    }
    68     public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress) where I : class {
     72    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
     73      where I : class {
    6974      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
    7075    }
    71     public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password) where I : class {
    72       ChannelFactory<I> factory = GetChannelFactory<I>(endpointConfigurationName, userName, password);
    73 
    74       if (!string.IsNullOrEmpty(remoteAddress)) {
    75         SetEndpointAddress(factory.Endpoint, remoteAddress);
    76       }
    77       var disposable = new Disposable<ChannelFactory<I>>(factory);
    78       disposable.OnDisposing += new EventHandler<EventArgs<object>>(disposable_OnDisposing);
    79       return disposable;
    80     }
    81 
    82     private static void disposable_OnDisposing(object sender, EventArgs<object> e) {
    83       DisposeCommunicationObject((ICommunicationObject)e.Value);
    84       ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(disposable_OnDisposing);
    85     }
    86 
    87     private static ChannelFactory<I> GetChannelFactory<I>(string endpointConfigurationName, string userName, string password) where I : class {
    88       var channelFactory = new ChannelFactory<I>(endpointConfigurationName);
     76    public static Disposable<ChannelFactory<I>> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
     77      where I : class {
     78      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
    8979      channelFactory.Credentials.UserName.UserName = userName;
    9080      channelFactory.Credentials.UserName.Password = password;
    9181      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
    92       return channelFactory;
     82
     83      if (!string.IsNullOrEmpty(remoteAddress)) {
     84        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
     85      }
     86
     87      Disposable<ChannelFactory<I>> disposableChannelFactory = new Disposable<ChannelFactory<I>>(channelFactory);
     88      disposableChannelFactory.OnDisposing += new EventHandler<EventArgs<object>>(DisposableChannelFactory_OnDisposing);
     89      return disposableChannelFactory;
    9390    }
     91
     92    private static void DisposableChannelFactory_OnDisposing(object sender, EventArgs<object> e) {
     93      DisposeCommunicationObject((ICommunicationObject)e.Value);
     94      ((Disposable)sender).OnDisposing -= new EventHandler<EventArgs<object>>(DisposableChannelFactory_OnDisposing);
     95    }
     96    #endregion
    9497
    9598    public static void DisposeCommunicationObject(ICommunicationObject obj) {
     
    104107    }
    105108
    106     /// <summary>
    107     /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
    108     /// </summary>
     109    #region Helpers
    109110    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
    110       EndpointAddressBuilder endpointAddressbuilder = new EndpointAddressBuilder(endpoint.Address);
    111       UriBuilder uriBuilder = new UriBuilder(endpointAddressbuilder.Uri);
     111      // change the endpoint address and preserve the identity certificate defined in the config file
     112      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
     113      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
    112114      uriBuilder.Host = remoteAddress;
    113       endpointAddressbuilder.Uri = uriBuilder.Uri;
    114       endpoint.Address = endpointAddressbuilder.ToEndpointAddress();
     115      endpointAddressBuilder.Uri = uriBuilder.Uri;
     116      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
    115117    }
     118    #endregion
    116119  }
    117120}
Note: See TracChangeset for help on using the changeset viewer.