Esempio n. 1
0
        public void SingleClientMultipleRequestsNetTcpClientConnection()
        {
            string testString = new string('a', 3000);
            var    host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    var binding = ClientHelper.GetStreamedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Esempio n. 2
0
        public void MultipleClientsUsingPooledSocket()
        {
            var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    var binding = ClientHelper.GetStreamedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var clientIpEndpoint = channel.GetClientIpEndpoint();
                    ((IChannel)channel).Close();
                    for (int i = 0; i < 10; i++)
                    {
                        channel = factory.CreateChannel();
                        ((IChannel)channel).Open();
                        var clientIpEndpoint2 = channel.GetClientIpEndpoint();
                        Assert.Equal(clientIpEndpoint, clientIpEndpoint2);
                        ((IChannel)channel).Close();
                    }
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void MessageContract()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var message = new ClientContract.TestMessage()
                    {
                        Header = "Header",
                        Body   = new MemoryStream(Encoding.UTF8.GetBytes("Hello world"))
                    };
                    ClientContract.TestMessage result = channel.TestMessageContract(message);
                    Assert.Equal("Header from server", result.Header);
                    Assert.Equal("Hello world from server", new StreamReader(result.Body, Encoding.UTF8).ReadToEnd());
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void SimpleNetTcpClientConnectionWindowsAuth()
        {
            string   testString = new string('a', 3000);
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Esempio n. 5
0
        public void MultipleClientsNonConcurrentNetTcpClientConnection()
        {
            string testString = new string('a', 3000);
            var    host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    var binding = ClientHelper.GetStreamedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:8808/nettcp.svc")));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    ((IChannel)channel).Close();
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void ConcurrentNetTcpClientConnection()
        {
            IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.NoSecurityRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    System.Threading.Tasks.Task <bool> resultTask = channel.WaitForSecondRequestAsync();
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    channel.SecondRequest();
                    bool waitResult = resultTask.GetAwaiter().GetResult();
                    Assert.True(waitResult, $"SecondRequest wasn't executed concurrently");
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Esempio n. 7
0
        public void SingleClientsUsingPooledSocketForMultipleRequests()
        {
            var host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    var binding = ClientHelper.GetStreamedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(new Uri("net.tcp://localhost:8808/nettcp.svc")));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var clientIpEndpoint = channel.GetClientIpEndpoint();
                    for (int i = 0; i < 10; i++)
                    {
                        var clientIpEndpoint2 = channel.GetClientIpEndpoint();
                        Assert.Equal(clientIpEndpoint, clientIpEndpoint2);
                    }
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
        public void SimpleNetTcpClientConnection()
        {
            string testString = new string('a', 3000);
            var    host       = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    var binding = ClientHelper.GetBufferedModeBinding();
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(new Uri(NoAuthNetTcpServiceUri)));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    var result = channel.EchoString(testString);
                    Assert.Equal(testString, result);
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Esempio n. 9
0
 private void assertForCommon(string sourceString, IWebHost host)
 {
     using (host)
     {
         System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
         ClientContract.ITestService channel = null;
         host.Start();
         try
         {
             System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
             factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                            new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
             channel = factory.CreateChannel();
             ((IChannel)channel).Open();
             string result = channel.EchoForPermission(sourceString);
             Assert.Equal(sourceString, result);
             ((IChannel)channel).Close();
             factory.Close();
         }
         finally
         {
             ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
         }
     }
 }
Esempio n. 10
0
        public void AuthorizationBasedonRolesTest()
        {
            string   testString = "a" + PrincipalPermissionMode.Always.ToString() + "test";
            IWebHost host       = ServiceHelper.CreateWebHostBuilder <StartupForAuthorization>(_output).Build();

            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();
                    ((IChannel)channel).Open();
                    string result = channel.EchoForAuthorizarionOneRole(testString);
                    Assert.Equal(testString, result);
                    try
                    {
                        channel.EchoForAuthorizarionNoRole(testString);
                    }catch (Exception ex)
                    {
                        Assert.Contains("Access is denied", ex.Message);
                    }
                    ((IChannel)channel).Close();
                    factory.Close();
                }
                finally
                {
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }
Esempio n. 11
0
        private void assertForCommon(string sourceString, IWebHost host)
        {
            using (host)
            {
                System.ServiceModel.ChannelFactory <ClientContract.ITestService> factory = null;
                ClientContract.ITestService channel = null;
                host.Start();
                try
                {
                    System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(System.ServiceModel.SecurityMode.Transport);
                    factory = new System.ServiceModel.ChannelFactory <ClientContract.ITestService>(binding,
                                                                                                   new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.WindowsAuthRelativePath));
                    channel = factory.CreateChannel();

                    // ReSharper disable once SuspiciousTypeConversion.Global
                    ((IChannel)channel).Open();
                    string result = channel.EchoForPermission(sourceString);
                    Assert.Equal(sourceString, result);

                    //
                    // These were explicitly removed because the ServiceHelper already cleans these up, and
                    // in some cases not using proper disposal handling will result in:
                    // 'System.IO.IOException : Received an unexpected EOF or 0 bytes from the transport stream.'
                    // on the build server, causing false test failures.
                    //
                    // ((IChannel)channel).Close();
                    // factory.Close();
                }
                finally
                {
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory);
                }
            }
        }