Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Common/3.3/ClientFactory.cs @ 5706

Last change on this file since 5706 was 5706, checked in by cneumuel, 13 years ago

#1298

  • removed Disposable
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ServiceModel;
24using System.ServiceModel.Description;
25using HeuristicLab.Clients.Common.Properties;
26
27namespace HeuristicLab.Clients.Common {
28  public static class ClientFactory {
29    #region CreateClient Methods
30    public static T CreateClient<T, I>()
31      where T : ClientBase<I>, I
32      where I : class {
33      return CreateClient<T, I>(null, null);
34    }
35    public static T CreateClient<T, I>(string endpointConfigurationName)
36      where T : ClientBase<I>, I
37      where I : class {
38      return CreateClient<T, I>(endpointConfigurationName, null);
39    }
40    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
41      where T : ClientBase<I>, I
42      where I : class {
43      return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
44    }
45    public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
46      where T : ClientBase<I>, I
47      where I : class {
48      T client;
49      if (string.IsNullOrEmpty(endpointConfigurationName)) {
50        client = Activator.CreateInstance<T>();
51      } else {
52        client = (T)Activator.CreateInstance(typeof(T), endpointConfigurationName);
53      }
54
55      if (!string.IsNullOrEmpty(remoteAddress)) {
56        SetEndpointAddress(client.Endpoint, remoteAddress);
57      }
58
59      client.ClientCredentials.UserName.UserName = userName;
60      client.ClientCredentials.UserName.Password = password;
61      client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
62      return client;
63    }
64    #endregion
65
66    #region CreateChannelFactory Methods
67    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName)
68      where I : class {
69      return CreateChannelFactory<I>(endpointConfigurationName, null);
70    }
71    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress)
72      where I : class {
73      return CreateChannelFactory<I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
74    }
75    public static ChannelFactory<I> CreateChannelFactory<I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
76      where I : class {
77      ChannelFactory<I> channelFactory = new ChannelFactory<I>(endpointConfigurationName);
78      if (!string.IsNullOrEmpty(remoteAddress)) {
79        SetEndpointAddress(channelFactory.Endpoint, remoteAddress);
80      }
81
82      channelFactory.Credentials.UserName.UserName = userName;
83      channelFactory.Credentials.UserName.Password = password;
84      channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
85      return channelFactory;
86    }
87    #endregion
88
89    #region Helpers
90    private static void SetEndpointAddress(ServiceEndpoint endpoint, string remoteAddress) {
91      // change the endpoint address and preserve the identity certificate defined in the config file
92      EndpointAddressBuilder endpointAddressBuilder = new EndpointAddressBuilder(endpoint.Address);
93      UriBuilder uriBuilder = new UriBuilder(endpointAddressBuilder.Uri);
94      uriBuilder.Host = remoteAddress;
95      endpointAddressBuilder.Uri = uriBuilder.Uri;
96      endpoint.Address = endpointAddressBuilder.ToEndpointAddress();
97    }
98    #endregion
99  }
100}
Note: See TracBrowser for help on using the repository browser.