예제 #1
0
        public static ServiceHost GetServiceHost(BindingType bindingType, string externalEndpoint, string authKey, StoreLocation storeLocation, StoreName storeName, string subjectName)
        {
            // Create an instance of the backup service
            BackupService backupService = new BackupService(authKey);

            // Add instance to service host
            ServiceHost serviceHost = new ServiceHost(backupService);

            // Create binding with transport security
            Binding binding;
            string protocol;

            switch (bindingType)
            {
                case BindingType.Https:
                    WebHttpBinding httpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
                    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                    binding = httpBinding;
                    protocol = "https";
                    break;

                default: //Tcp
                    NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.Transport);
                    tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
                    binding = tcpBinding;
                    protocol = "net.tcp";
                    break;
            }

            // Add the service endpoint
            ServiceEndpoint ep = serviceHost.AddServiceEndpoint(
               typeof(IBackupService),
               binding,
               String.Format(protocol + "://{0}/BackupService", externalEndpoint));

            // Set the x.509 certificate
            serviceHost.Credentials.ServiceCertificate.SetCertificate(
                storeLocation,
                storeName,
                X509FindType.FindBySubjectName,
                subjectName);

            return serviceHost;
        }