Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int numThreads;

            Console.Write("Enter number of threads:");
            numThreads = Convert.ToInt32(Console.ReadLine());
            Stopwatch sw = new Stopwatch();

            sw.Start();

            var threads = new List <Thread>();

            for (int i = 0; i < numThreads; i++)
            {
                string currentUserId = "user" + i.ToString();
                Thread t             = new Thread(x =>
                {
                    LoginSessionService.ServiceClient cli = new LoginSessionService.ServiceClient();
                    string userID   = currentUserId;
                    string password = currentUserId;
                    string token    = cli.Login(userID, password);
                    Console.WriteLine($"got {token} for {userID}");
                    using (var opScope = new OperationContextScope(cli.InnerChannel))
                    {
                        var userIDHeader        = new MessageHeader <string>(userID);
                        var tokenHeader         = new MessageHeader <string>(token);
                        var untypedUserIDHeader = userIDHeader.GetUntypedHeader("UserID", AUTHNS);
                        var untypedTokenHeader  = tokenHeader.GetUntypedHeader("Token", AUTHNS);
                        OperationContext.Current.OutgoingMessageHeaders.Add(untypedUserIDHeader);
                        OperationContext.Current.OutgoingMessageHeaders.Add(untypedTokenHeader);
                        Console.WriteLine(cli.ActionA());
                        Console.WriteLine(cli.ActionB());
                        cli.Logout();
                    }
                });
                threads.Add(t);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
            sw.Stop();
            Console.WriteLine("Time:" + sw.Elapsed);
        }
 static void Main(string[] args)
 {
     LoginSessionService.ServiceClient cli = new LoginSessionService.ServiceClient();
     try
     {
         string userID   = "test";
         string password = "******";
         string token    = cli.Login(userID, password);
         using (var opScope = new OperationContextScope(cli.InnerChannel))
         {
             var userIDHeader        = new MessageHeader <string>(userID);
             var tokenHeader         = new MessageHeader <string>(token);
             var untypedUserIDHeader = userIDHeader.GetUntypedHeader("UserID", AUTHNS);
             var untypedTokenHeader  = tokenHeader.GetUntypedHeader("Token", AUTHNS);
             OperationContext.Current.OutgoingMessageHeaders.Add(untypedUserIDHeader);
             OperationContext.Current.OutgoingMessageHeaders.Add(untypedTokenHeader);
             Console.WriteLine(cli.ActionA());
             Console.WriteLine(cli.ActionB());
             cli.Logout();
             cli.Close();
         }
     }
     catch (FaultException ex)
     {
         Console.WriteLine(ex.Message);
         cli.Abort();
     }
     catch (CommunicationException ex)
     {
         Console.WriteLine(ex.Message);
         cli.Abort();
     }
     catch (TimeoutException ex)
     {
         Console.WriteLine(ex.Message);
         cli.Abort();
     }
 }