using System; using System.ServiceModel; using System.Security.Cryptography.X509Certificates; namespace Samples { public class Test { public static void Main() { try { Test t = new Test(); //t.TcpMessageWithCredentialWindows(); t.AllConfig(); } catch (System.Exception exc) { Console.WriteLine(exc.Message); Console.ReadLine(); } } private void AllConfig() { ServiceHost sh = new ServiceHost(typeof(Calculator)); sh.Open(); Console.WriteLine("Listening"); Console.ReadLine(); } private void MakeTransportBinding() { // // WSHttpBinding b = new WSHttpBinding(); b.Security.Mode = SecurityMode.Transport; // b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; // } private void MakeMessageBinding() { // // WSHttpBinding b = new WSHttpBinding(); b.Security.Mode = SecurityMode.Message; // b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; // } private void TransportWithMessageBinding() { // WSHttpBinding b = new WSHttpBinding(); b.Security.Mode = SecurityMode.TransportWithMessageCredential; // } private void SetModeViaConstructor() { // WSHttpBinding b = new WSHttpBinding(SecurityMode.Message); // } private void HttpMessageWithCredential() { // WSHttpBinding b = new WSHttpBinding(); b.Security.Mode = SecurityMode.TransportWithMessageCredential; b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; // The SSL certificate is bound to port 8006 using the HttpCfg.exe tool. Uri httpsAddress = new Uri("https://localMachineName:8006/base"); ServiceHost sh = new ServiceHost(typeof(Calculator), httpsAddress); sh.AddServiceEndpoint(typeof(ICalculator), b, "HttpsCalculator"); sh.Open(); Console.WriteLine("Listening"); Console.ReadLine(); // } private void TcpMessageWithCredential() { // NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential); b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; Uri netTcpAddress = new Uri("net.tcp://baseAddress"); ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress); sh.Credentials.ServiceCertificate.SetCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com"); sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator"); sh.Open(); Console.WriteLine("Listening"); Console.ReadLine(); // } private void TcpMessageWithCredentialWindows() { // NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential); b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; Uri netTcpAddress = new Uri("net.tcp://Tcp"); ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress); sh.Credentials.ServiceCertificate.SetCertificate( StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com"); sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator"); sh.Open(); Console.WriteLine("Listening"); Console.ReadLine(); // } } [ServiceContract] interface ICalculator { [OperationContract] double Add(double a, double b); } public class Calculator : ICalculator { public double Add(double a, double b) { return a + b; } } }