Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Contracts/3.3/WcfSettings.cs @ 4305

Last change on this file since 4305 was 4305, checked in by cneumuel, 14 years ago

added streamedHttpEndpoit binding (without message-security (for now)) (#1168)

File size: 5.0 KB
Line 
1//#define USE_MSG_BINDING
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.ServiceModel.Channels;
7using System.ServiceModel;
8using System.Security.Cryptography.X509Certificates;
9using System.Net;
10using System.ServiceModel.Description;
11
12namespace HeuristicLab.Hive.Contracts {
13  public static class WcfSettings {
14    public const string SlaveServiceName = "SlaveService";
15    public const string ServerConsoleServiceName = "ServerConsoleService";
16    public const string ClientServiceName = "ClientService";
17    public const string ClientStreamedServiceName = "ClientServiceStreamed";
18
19    public const int DefaultPort = 9000;
20   
21    /// <summary>
22    /// Gets a pre-defined binding using TCP for secure transport.
23    /// </summary>
24    /// <returns>A binding type of <see cref="NetTcpBinding"/></returns>
25    public static Binding GetBinding() {
26#if USE_MSG_BINDING
27      NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
28#else
29      NetTcpBinding binding = new NetTcpBinding();
30#endif
31      binding.MaxBufferSize = int.MaxValue;
32      binding.MaxReceivedMessageSize = int.MaxValue;
33      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
34      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
35      binding.CloseTimeout = new TimeSpan(0, 5, 0);
36      binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
37      binding.SendTimeout = new TimeSpan(0, 5, 0);
38      return binding;
39    }
40
41    public static Binding GetStreamedBinding() {
42#if USE_MSG_BINDING
43      NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
44#else
45      NetTcpBinding binding = new NetTcpBinding();
46#endif
47      binding.TransferMode = TransferMode.Streamed;
48      binding.MaxReceivedMessageSize = int.MaxValue;
49      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
50      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
51      binding.CloseTimeout = new TimeSpan(0, 5, 0);
52      binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
53      binding.SendTimeout = new TimeSpan(0, 5, 0);
54      //Disabling security mode, for the moment
55      binding.Security.Mode = SecurityMode.None;
56     
57      return binding;
58    }
59
60//    /// <summary>
61//    /// Defines the used certificate for authentification located in a certification store.
62//    /// </summary>
63//    /// <param name="svchost">A service for which this certificate is applicable.</param>
64//    public static void SetServiceCertificate(ServiceHost svchost) {
65//#if USE_MSG_BINDING
66//      svchost.Credentials.ServiceCertificate.SetCertificate(
67//        StoreLocation.LocalMachine,
68//        StoreName.My,
69//        X509FindType.FindBySubjectName,
70//        SERVERCERT);
71//#endif
72//    }
73
74    /// <summary>
75    /// Gets the currently active IP address.
76    /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
77    /// </summary>
78    /// <returns></returns>
79    public static IPAddress GetActiveIP() {
80      return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address;
81    }
82
83    /// <summary>
84    /// Gets the default port used for HIVE services.
85    /// </summary>
86    /// <returns></returns>
87    public static int GetDefaultPort() {
88      return DefaultPort;
89    }
90   
91    /// <summary>
92    /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
93    /// </summary>
94    public static void SetEndpointAddress(ServiceEndpoint endpoint, string address) {
95      EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address);
96      builder.Uri = new Uri(address);
97      endpoint.Address = builder.ToEndpointAddress();
98    }
99
100  }
101
102  /// <summary>
103  /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
104  /// the verification process is managed by the underlying operating system.
105  /// </summary>
106  /// <remarks>
107  /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
108  /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
109  /// </remarks>
110  /*
111  public class PermissiveCertificatePolicy {
112    string subjectName;
113    static PermissiveCertificatePolicy currentPolicy;
114    PermissiveCertificatePolicy(string subjectName) {
115      this.subjectName = subjectName;
116      ServicePointManager.ServerCertificateValidationCallback +=
117          new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
118    }
119
120    public static void Enact(string subjectName) {
121      currentPolicy = new PermissiveCertificatePolicy(subjectName);
122    }
123
124    bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
125      if (cert.Subject == subjectName) {
126        return true;
127      }
128      return false;
129    }
130  }             */
131}
Note: See TracBrowser for help on using the repository browser.