Ejemplo n.º 1
0
        public void Connect(ConnectionConfiguration configuration)
        {
            configuration.IsNotNull(nameof(configuration));
            configuration.Protocol.IsIn("net.tcp", "http");

            // Remember last config
            _lastConfiguration = configuration;

            // Search for the end of host name
            string hostname = configuration.ServerName.Split('/').First();
            string path     = configuration.ServerName.Substring(hostname.Length);

            var address =
                new EndpointAddress($"{configuration.Protocol}://{hostname}:{configuration.Port}{path}/Service.svc");

            Binding binding;

            switch (configuration.Protocol)
            {
            case "net.tcp":
                binding = configuration.UseSsl
                        ? new NetTcpBinding(SecurityMode.Transport)
                {
                    Security =
                    {
                        Mode      = SecurityMode.Transport,
                        Transport =
                        {
                            ClientCredentialType = TcpClientCredentialType.None
                        }
                    }
                }
                        : new NetTcpBinding(SecurityMode.None);
                break;

            case "http":
                binding = new WSDualHttpBinding(WSDualHttpSecurityMode.None);
                break;

            default:
                throw new NotSupportedException("Supported protocols are only net.tcp and http.");
            }

            Service = DuplexChannelFactory <IChatService> .CreateChannel(new InstanceContext(this), binding, address);

            ServerName = configuration.ServerName;
            UsesSsl    = configuration.UseSsl;

            _timeout = TimeSpan.FromMilliseconds(configuration.TimeOut);
        }