Exemple #1
0
        static void Main()
        {
            ServiceHost            serviceHost        = null;
            ChannelFactory <IEcho> echoChannelFactory = null;
            WSTrustServiceHost     trustServiceHost   = null;

            try
            {
                CustomTokenHandler handler = new CustomTokenHandler();

                //
                // Start the service
                //
                serviceHost = new ServiceHost(typeof(EchoService));
                string serviceAddress = "http://" + Environment.MachineName + ":8080/EchoService";

                ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetEnabled = true;
                metadataBehavior.HttpGetUrl     = new Uri(serviceAddress);
                serviceHost.Description.Behaviors.Add(metadataBehavior);
                serviceHost.AddServiceEndpoint(typeof(IEcho), GetServiceBinding(), serviceAddress);
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), serviceAddress + "/mex");
                serviceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, thumbprint);

                FederatedServiceCredentials.ConfigureServiceHost(serviceHost);
                //
                // Update the service credentials so that it can deserialize the custom token
                //
                (( FederatedServiceCredentials )serviceHost.Credentials).SecurityTokenHandlers.Add(handler);
                serviceHost.Open();
                Console.WriteLine("The echo service has started at {0}.\n", serviceAddress);

                //
                // Start the SecurityTokenService
                //
                X509Certificate2   certificate = CertificateUtil.GetCertificate(StoreName.Root, StoreLocation.LocalMachine, thumbprint);
                SigningCredentials credentials = new X509SigningCredentials(certificate);
                SecurityTokenServiceConfiguration securityTokenServiceConfiguration = new SecurityTokenServiceConfiguration(securityTokenServiceAddress, credentials);
                securityTokenServiceConfiguration.SecurityTokenService = typeof(SampleTokenService);

                // register a handler to the SecurityTokenService here so that it can issue the custom token
                securityTokenServiceConfiguration.SecurityTokenHandlers.Add(handler);

                // Add the STS endpoint information
                securityTokenServiceConfiguration.TrustEndpoints.Add(
                    new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), GetSecurityTokenServiceBinding(), securityTokenServiceAddress));

                securityTokenServiceConfiguration.ServiceCertificate = certificate;

                trustServiceHost = new WSTrustServiceHost(securityTokenServiceConfiguration, new Uri(securityTokenServiceAddress));
                trustServiceHost.Open();
                Console.WriteLine("The security token service has started at {0}.\n", securityTokenServiceAddress);

                //
                // Invoke the client
                //



                echoChannelFactory = new ChannelFactory <IEcho>(GetClientBinding(),
                                                                new EndpointAddress(new Uri(serviceAddress),
                                                                                    EndpointIdentity.CreateDnsIdentity("localhost")));

                IEcho client = echoChannelFactory.CreateChannel();
                ((IClientChannel)client).OperationTimeout = TimeSpan.MaxValue;


                string echoedString = client.Echo("Hello");
                Console.WriteLine("The echo service returns '{0}'. \n", echoedString);


                Console.WriteLine("Press [Enter] to close service.");
                Console.ReadLine();

                echoChannelFactory.Close();

                Console.WriteLine("Press [Enter] to continue.");
                Console.ReadLine();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
                if (echoChannelFactory != null)
                {
                    echoChannelFactory.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
                if (echoChannelFactory != null)
                {
                    echoChannelFactory.Abort();
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.InnerException.Message);
            }
            finally
            {
                if (serviceHost != null && serviceHost.State != CommunicationState.Faulted)
                {
                    serviceHost.Close();
                }

                if (trustServiceHost != null && trustServiceHost.State != CommunicationState.Faulted)
                {
                    trustServiceHost.Close();
                }
            }
        }