Ejemplo n.º 1
0
        public static void InvokeEchoServiceUsingWcf(
            Settings settings,
            Action <string> log)
        {
            var echo = (Func <IEchoService, string>)(channel =>
                                                     channel.Echo("Hello"));
            var echoFault = (Func <IEchoService, bool>)(channel =>
            {
                try
                {
                    channel.FailEcho("Hello Fault");
                }
                catch (FaultException <EchoFault> e)
                {
                    Console.WriteLine("FaultException<EchoFault>: fault with " + e.Detail.Text);
                    ((IClientChannel)channel).Abort();
                }
                return(false);
            });

            log($"BasicHttp:\n\tEcho(\"Hello\") => "
                + echo.WcfInvoke(new BasicHttpBinding(BasicHttpSecurityMode.None), settings.basicHttpAddress));

            log($"BasicHttp:\nFailEcho(\"Hello Fault\") => "
                + echoFault.WcfInvoke(new BasicHttpBinding(BasicHttpSecurityMode.None), settings.basicHttpAddress));

            log($"WsHttp:\n\tEcho(\"Hello\") => "
                + echo.WcfInvoke(new WSHttpBinding(SecurityMode.None), settings.wsHttpAddress));

            log($"NetHttp:\n\tEcho(\"Hello\") => "
                + echo.WcfInvoke(new NetTcpBinding(), settings.netTcpAddress));

            void RunExampleWsHttpsTransportWithMessageCredential()
            {
                WSHttpBinding binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);

                binding.ApplyDebugTimeouts();
                binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
                log($"WsHttps TransportWithMessageCredential:\n\tEcho(\"Hello\") => "
                    + echo.WcfInvoke(binding,
                                     settings.wsHttpAddressValidateUserPassword,
                                     channel => {
                    var clientCredentials = (ClientCredentials)channel.Endpoint.EndpointBehaviors[typeof(ClientCredentials)];
                    clientCredentials.UserName.UserName = "******";
                    clientCredentials.UserName.Password = "******";
                    channel.Credentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication
                    {
                        CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None
                    };
                }
                                     )
                    );
            }

            if (settings.UseHttps)
            {
                log($"BasicHttps:\n\tEcho(\"Hello\") => "
                    + echo.WcfInvoke(new BasicHttpsBinding(BasicHttpsSecurityMode.Transport), settings.basicHttpsAddress));

                log($"WsHttps:\n\tEcho(\"Hello\") => "
                    + echo.WcfInvoke(new WSHttpBinding(SecurityMode.Transport), settings.wsHttpsAddress));

                RunExampleWsHttpsTransportWithMessageCredential();
            }

            var echoComplex = (Func <IEchoService, string>)((IEchoService channel) =>
                                                            channel.ComplexEcho(new EchoMessage()
            {
                Text = "Complex Hello"
            }));

            log($"BasicHttp with Complex Object:\n\tEcho(\"Hello\") => "
                + echoComplex.WcfInvoke(new NetTcpBinding(), settings.netTcpAddress));
        }