Changeset 4913
- Timestamp:
- 11/23/10 18:18:24 (14 years ago)
- 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 23 23 using System.ServiceModel; 24 24 using HeuristicLab.Clients.Common.Properties; 25 using System.ServiceModel.Description; 26 using System.Collections; 27 using System.Collections.Generic; 28 using HeuristicLab.Common; 25 29 26 30 namespace HeuristicLab.Clients.Common { … … 39 43 where T : ClientBase<I>, I 40 44 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 { 41 50 T client; 42 51 if (string.IsNullOrEmpty(endpointConfigurationName)) { … … 47 56 48 57 if (!string.IsNullOrEmpty(remoteAddress)) { 49 client.Endpoint.Address = new EndpointAddress(remoteAddress);58 SetEndpointAddress(client.Endpoint, remoteAddress); 50 59 } 51 60 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; 54 63 client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; 55 64 return client; 56 65 } 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 } 57 137 } 58 138 } -
trunk/sources/HeuristicLab.Clients.Common/3.3/HeuristicLab.Clients.Common-3.3.csproj
r4387 r4913 115 115 <ItemGroup> 116 116 <Compile Include="ClientFactory.cs" /> 117 <Compile Include="Disposable.cs" /> 117 118 <Compile Include="HeuristicLabClientsCommonPlugin.cs" /> 118 119 <Compile Include="PasswordDialog.cs"> … … 140 141 </ItemGroup> 141 142 <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> 142 147 <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj"> 143 148 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project> -
trunk/sources/HeuristicLab.Clients.Common/3.3/HeuristicLabClientsCommonPlugin.cs.frame
r4865 r4913 28 28 [Plugin("HeuristicLab.Clients.Common", "3.3.2.$WCREV$")] 29 29 [PluginFile("HeuristicLab.Clients.Common-3.3.dll", PluginFileType.Assembly)] 30 [PluginDependency("HeuristicLab.Common", "3.3")] 30 31 public class HeuristicLabClientsCommonPlugin : PluginBase { 31 32 }
Note: See TracChangeset
for help on using the changeset viewer.