Пример #1
0
        public void MessageSecurityAndBindings2()
        {
            var n = new NetTcpBinding()
            {
                TransferMode = TransferMode.Streamed
            };

            n.Security.Mode = SecurityMode.Message;

            Assert.AreEqual(SecurityAlgorithmSuite.Default, n.Security.Message.AlgorithmSuite, "#sec1");
            Assert.AreEqual(MessageCredentialType.Windows /*huh*/, n.Security.Message.ClientCredentialType, "#sec2");

            var bc = n.CreateBindingElements();

            Assert.AreEqual(4, bc.Count, "#bc1");
            Assert.AreEqual(typeof(TransactionFlowBindingElement), bc [0].GetType(), "#bc2");
            Assert.AreEqual(typeof(SymmetricSecurityBindingElement), bc [1].GetType(), "#bc3");
            Assert.AreEqual(typeof(BinaryMessageEncodingBindingElement), bc [2].GetType(), "#bc4");
            Assert.AreEqual(typeof(TcpTransportBindingElement), bc [3].GetType(), "#bc5");

            Assert.IsFalse(n.CanBuildChannelFactory <IRequestChannel> (), "#cbf1");
            Assert.IsFalse(n.CanBuildChannelFactory <IOutputChannel> (), "#cbf2");
            Assert.IsFalse(n.CanBuildChannelFactory <IDuplexChannel> (), "#cbf3");
            Assert.IsFalse(n.CanBuildChannelFactory <IDuplexSessionChannel> (), "#cbf4");
            Assert.IsTrue(n.CanBuildChannelFactory <IRequestSessionChannel> (), "#cbf5");
        }
Пример #2
0
        public void DefaultValues()
        {
            var n = new NetTcpBinding();

            Assert.AreEqual(HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
            Assert.AreEqual(10, n.ListenBacklog, "#2");
            Assert.AreEqual(false, n.PortSharingEnabled, "#3");

            var tr = n.CreateBindingElements().Find <TcpTransportBindingElement> ();

            Assert.IsNotNull(tr, "#tr1");
            Assert.AreEqual(false, tr.TeredoEnabled, "#tr2");
            Assert.AreEqual("net.tcp", tr.Scheme, "#tr3");

            Assert.IsFalse(n.TransactionFlow, "#4");
            var tx = n.CreateBindingElements().Find <TransactionFlowBindingElement> ();

            Assert.IsNotNull(tx, "#tx1");
        }
Пример #3
0
        public void DefaultValues()
        {
            var n = new NetTcpBinding();

            Assert.AreEqual(HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
            Assert.AreEqual(10, n.ListenBacklog, "#2");
            Assert.AreEqual(false, n.PortSharingEnabled, "#3");

            var tr = n.CreateBindingElements().Find <TcpTransportBindingElement> ();

            Assert.IsNotNull(tr, "#tr1");
            Assert.AreEqual(false, tr.TeredoEnabled, "#tr2");
            Assert.AreEqual("net.tcp", tr.Scheme, "#tr3");

            Assert.IsFalse(n.TransactionFlow, "#4");
            var tx = n.CreateBindingElements().Find <TransactionFlowBindingElement> ();

            Assert.IsNotNull(tx, "#tx1");

            Assert.AreEqual(SecurityMode.Transport, n.Security.Mode, "#sec1");
            Assert.AreEqual(ProtectionLevel.EncryptAndSign, n.Security.Transport.ProtectionLevel, "#sec2");
            Assert.AreEqual(TcpClientCredentialType.Windows /*huh*/, n.Security.Transport.ClientCredentialType, "#sec3");

            var bc = n.CreateBindingElements();

            Assert.AreEqual(4, bc.Count, "#bc1");
            Assert.AreEqual(typeof(TransactionFlowBindingElement), bc [0].GetType(), "#bc2");
            Assert.AreEqual(typeof(BinaryMessageEncodingBindingElement), bc [1].GetType(), "#bc3");
            Assert.AreEqual(typeof(WindowsStreamSecurityBindingElement), bc [2].GetType(), "#bc4");
            Assert.AreEqual(typeof(TcpTransportBindingElement), bc [3].GetType(), "#bc5");

            Assert.IsFalse(n.CanBuildChannelFactory <IRequestChannel> (), "#cbf1");
            Assert.IsFalse(n.CanBuildChannelFactory <IOutputChannel> (), "#cbf2");
            Assert.IsFalse(n.CanBuildChannelFactory <IDuplexChannel> (), "#cbf3");
            Assert.IsTrue(n.CanBuildChannelFactory <IDuplexSessionChannel> (), "#cbf4");
        }
Пример #4
0
        public void MessageSecurityAndBindings4()
        {
            var n = new NetTcpBinding();

            n.Security.Mode = SecurityMode.Message;
            n.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;

            var bc = n.CreateBindingElements();

            Assert.AreEqual(4, bc.Count, "#bc1");
            Assert.AreEqual(typeof(TransactionFlowBindingElement), bc [0].GetType(), "#bc2");
            Assert.AreEqual(typeof(SymmetricSecurityBindingElement), bc [1].GetType(), "#bc3");
            Assert.AreEqual(typeof(BinaryMessageEncodingBindingElement), bc [2].GetType(), "#bc4");
            Assert.AreEqual(typeof(TcpTransportBindingElement), bc [3].GetType(), "#bc5");

            Assert.IsFalse(n.CanBuildChannelFactory <IRequestChannel> (), "#cbf1");
            Assert.IsFalse(n.CanBuildChannelFactory <IOutputChannel> (), "#cbf2");
            Assert.IsFalse(n.CanBuildChannelFactory <IDuplexChannel> (), "#cbf3");
            Assert.IsTrue(n.CanBuildChannelFactory <IDuplexSessionChannel> (), "#cbf4");
        }
Пример #5
0
        private void TcpTransportCert()
        {
            // This string uses a function to prepend the computer name at run time.
            string addressTCP = String.Format(
                "net.tcp://{0}:8036/NetTcpSecurity/Transport/Certificate",
                System.Net.Dns.GetHostEntry("").HostName);

            // <Snippet1>
            NetTcpBinding binding = new NetTcpBinding();

            binding.Security.Mode = SecurityMode.Transport;
            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
            // </Snippet1>

            // <Snippet3>
            NetTcpBinding bSecurity = new NetTcpBinding(SecurityMode.Transport);
            // </Snippet3>

            // <Snippet4>
            NetTcpBinding bConfigurationName = new NetTcpBinding("MyConfiguration");
            // </Snippet4>

            // <Snippet5>
            NetTcpBinding bSecurityReliable = new NetTcpBinding(SecurityMode.Transport, true);
            // </Snippet5>

            // <Snippet6>
            EnvelopeVersion envelopeVersion = binding.EnvelopeVersion;
            // </Snippet6>

            // <Snippet7>
            HostNameComparisonMode hostNameComparisonMode = binding.HostNameComparisonMode;
            // </Snippet7>

            // <Snippet8>
            int listenBacklog = binding.ListenBacklog;
            // </Snippet8>

            // <Snippet9>
            long maxBufferPoolsize = binding.MaxBufferPoolSize;
            // </Snippet9>

            // <Snippet10>
            int maxBufferSize = binding.MaxBufferSize;
            // </Snippet10>

            // <Snippet11>
            int maxConnections = binding.MaxConnections;
            // </Snippet11>

            // <Snippet12>
            long MaxReceivedMessageSize = binding.MaxReceivedMessageSize;
            // </Snippet12>

            // <Snippet13>
            bool portSharingEnabled = binding.PortSharingEnabled;
            // </Snippet13>

            // <Snippet14>
            XmlDictionaryReaderQuotas xmlDictionaryReaderQuotas =
                binding.ReaderQuotas;
            // </Snippet14>

            // <Snippet15>
            OptionalReliableSession reliableSession =
                binding.ReliableSession;
            // </Snippet15>

            // <Snippet16>
            string scheme = binding.Scheme;
            // </Snippet16>

            // <Snippet17>
            NetTcpSecurity security = binding.Security;
            // </Snippet17>

            // <Snippet18>
            bool transactionFlow = binding.TransactionFlow;
            // </Snippet18>

            // <Snippet19>
            TransactionProtocol transactionProtocol =
                binding.TransactionProtocol;
            // </Snippet19>

            // <Snippet20>
            BindingElementCollection elementCollection =
                binding.CreateBindingElements();
            // </Snippet20>

            // <Snippet21>
            // P:System.ServiceModel.NetTcpBinding.System.ServiceModel.Channels.
            // IBindingRuntimePreferences.ReceiveSynchronously
            // Private, no example needed
            // </Snippet21>

            // <Snippet22>
            TransferMode transferMode = binding.TransferMode;
            // </Snippet22>



            // You must create an array of URI objects to have a base address.
            Uri a = new Uri(addressTCP);

            Uri[] baseAddresses = new Uri[] { a };

            // Create the ServiceHost. The service type (Calculator) is not
            // shown here.
            ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddresses);

            // Add an endpoint to the service. Insert the thumbprint of an X.509
            // certificate found on your computer.
            Type c = typeof(ICalculator);

            //sh.AddServiceEndpoint(c, b, "Aloha");
            sh.Credentials.ServiceCertificate.SetCertificate(
                StoreLocation.LocalMachine,
                StoreName.My,
                X509FindType.FindByThumbprint,
                "af1f51b25cd413ed9cd00c315bbb6dc1c08da5e6");

            // This next line is optional. It specifies that the client's certificate
            // does not have to be issued by a trusted authority, but can be issued
            // by a peer if it is in the Trusted People store. Do not use this setting
            // for production code.
            // sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
            //X509CertificateValidationMode.PeerOrChainTrust;
            sh.Open();

            string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;

            Console.WriteLine("Listening @ {0}", address);
            Console.WriteLine("Press enter to close the service");
            Console.ReadLine();
        }
Пример #6
0
        private NetTcpBinding CreateTcpBinding()
        {
            NetTcpBinding tcpBinding = new NetTcpBinding();

            System.ServiceModel.Channels.BindingElementCollection bElementCollection = tcpBinding.CreateBindingElements();

            tcpBinding.Name                   = "SeymourSkinner";
            tcpBinding.CloseTimeout           = TimeSpan.FromHours(2); // new TimeSpan(1, 0, 0); // 1 hora
            tcpBinding.OpenTimeout            = TimeSpan.FromHours(2); // new TimeSpan(1, 0, 0); // 1 hora
            tcpBinding.ReceiveTimeout         = TimeSpan.FromHours(2); // new TimeSpan(1, 0, 0); // 1 hora
            tcpBinding.SendTimeout            = TimeSpan.FromHours(2); // new TimeSpan(1, 0, 0); // 1 hora
            tcpBinding.MaxBufferPoolSize      = 2147483647;
            tcpBinding.MaxBufferSize          = 2147483647;
            tcpBinding.MaxReceivedMessageSize = 2147483647;

            tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(1, 0, 0);
            tcpBinding.Security.Mode = SecurityMode.None;

            System.ServiceModel.Channels.TcpTransportBindingElement tcp = bElementCollection.Find <System.ServiceModel.Channels.TcpTransportBindingElement>();
            tcp.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = 500;
            tcp.ConnectionPoolSettings.LeaseTimeout = TimeSpan.MaxValue;
            tcp.ConnectionPoolSettings.IdleTimeout  = TimeSpan.MaxValue;
            tcp.ChannelInitializationTimeout        = TimeSpan.MaxValue;
            tcp.ConnectionBufferSize   = int.MaxValue;
            tcp.MaxBufferPoolSize      = long.MaxValue;
            tcp.MaxBufferSize          = int.MaxValue;
            tcp.MaxReceivedMessageSize = long.MaxValue;

            XmlDictionaryReaderQuotas quotas = tcpBinding.ReaderQuotas;

            quotas.MaxStringContentLength = int.MaxValue;
            quotas.MaxArrayLength         = int.MaxValue;
            quotas.MaxBytesPerRead        = int.MaxValue;
            quotas.MaxDepth = int.MaxValue;
            quotas.MaxNameTableCharCount = int.MaxValue;

            tcpBinding.GetType().GetProperty("ReaderQuotas").SetValue(tcpBinding, quotas, null);

            return(tcpBinding);
        }
Пример #7
0
        static void Main()
        {
            // use Steeltoe to parse VCAP_APPLICATION env variables into config object
            var builder    = new ConfigurationBuilder().AddCloudFoundry();
            var config     = builder.Build();
            var opts       = new CloudFoundryApplicationOptions();
            var appSection = config.GetSection(CloudFoundryApplicationOptions.CONFIGURATION_PREFIX);

            appSection.Bind(opts);

            // get external TCP route (format: ["fullyqualifieddomainname.com:80000"])
            var appRouteHostAndExternalPort = opts.ApplicationUris.FirstOrDefault().Split(':');
            var appRouteHost    = appRouteHostAndExternalPort.ElementAtOrDefault(0);
            var appExternalPort = appRouteHostAndExternalPort.ElementAtOrDefault(1);

            if (appRouteHost == "" || appExternalPort == "")
            {
                throw new System.ArgumentException("Invalid VCAP_APPLICATION route or port");
            }

            // ensure external TCP port and internal listening $PORT are the same
            var appInternalPort = opts.Port.ToString();

            if (appInternalPort != appExternalPort)
            {
                throw new System.ArgumentException($"Internal listening port must match External Route port : {appInternalPort} != {appExternalPort}");
            }
            Console.WriteLine($"URI: {appRouteHost}:{appInternalPort}");


            // have endpoints listen on public URI
            var baseAddress = new Uri($"net.tcp://{appRouteHost}:{appInternalPort}/example/service");
            var svcHost     = new ServiceHost(typeof(HelloWorld), baseAddress);

            // enable verbose errors
            ServiceDebugBehavior debug = svcHost.Description.Behaviors.Find <ServiceDebugBehavior>();

            debug.IncludeExceptionDetailInFaults = true;

            var netTcpBinding = new NetTcpBinding();

            netTcpBinding.Security.Mode = SecurityMode.None;

            // use custom binding to reduce connection pool settings to work better in load-balanced scenario (https://stackoverflow.com/questions/9714426/disable-connection-pooling-for-wcf-net-tcp-bindings)
            BindingElementCollection   bindingElementCollection = netTcpBinding.CreateBindingElements();
            TcpTransportBindingElement transport = bindingElementCollection.Find <TcpTransportBindingElement>();

            transport.ConnectionPoolSettings.IdleTimeout  = TimeSpan.Zero;
            transport.ConnectionPoolSettings.LeaseTimeout = TimeSpan.Zero;
            transport.ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint = 0;

            CustomBinding balancedTcpBinding = new CustomBinding();

            balancedTcpBinding.Elements.AddRange(bindingElementCollection.ToArray());
            balancedTcpBinding.Name = "NetTcpBinding";


            // add metadata endpoint
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            svcHost.Description.Behaviors.Add(smb);

            svcHost.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName,
                balancedTcpBinding,
                "mex"
                );

            // add service endpoint
            svcHost.AddServiceEndpoint(
                typeof(IHelloWorld),
                balancedTcpBinding,
                "IHelloWorld"
                );

            svcHost.Open();
            Console.WriteLine($"svcHost is {svcHost.State}.  Press enter to close.");

            Thread.Sleep(Timeout.Infinite);
            svcHost.Close();
        }