public void CloseSession()
 {
     try
     {
         if (client != null)
         {
             if (client.State == System.ServiceModel.CommunicationState.Opened)
             {
                 client.Close();
             }
             else if (client.State == System.ServiceModel.CommunicationState.Faulted)
             {
                 client.Abort();
             }
         }
     }
     catch (FaultException)
     {
         client.Abort();
     }
     catch (CommunicationException)
     {
         client.Abort();
     }
     catch (TimeoutException)
     {
         client.Abort();
     }
     catch { throw; }
 }
        public static void RunTest()
        {
            PermissiveCertificatePolicy.Enact("CN=HTTPS-Server");
            Console.Write("Initializing Sales Proxy...");
            BicycleWorldSalesServiceClient proxy = new BicycleWorldSalesServiceClient("wsHttpBinding_BicycleWorldSalesService");
            Console.WriteLine(" done");

            proxy.ClientCredentials.UserName.UserName = "******";
            proxy.ClientCredentials.UserName.Password = "******";

            // Test the operations in the service

            try
            {
                Console.WriteLine(proxy.Test());

                // The encrypted list will timeout for slightly large lists (in the hundreds), so the list is
                // being grabbed in smaller chunks.  Transaction logic is used to lock the list while the list is being
                // grabbed.
                TransactionOptions transactionOptions = new TransactionOptions();
                transactionOptions.IsolationLevel = IsolationLevel.RepeatableRead;
                transactionOptions.Timeout = new TimeSpan(0, 2, 0);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions))
                {
                    Console.WriteLine("Test 1: List all Sales Orders");
                    int take = 5;
                    int count = proxy.GetSalesOrderListCount();
                    for (int skip = 0; skip < count; skip = skip + take)
                    {
                        var salesOrders = proxy.GetSalesOrderList(take, skip);
                        foreach (var saleOrder in salesOrders)
                        {
                            Console.WriteLine(" Sales Order ID: {0:000}", saleOrder.SalesOrderID);
                            foreach (var item in saleOrder.SalesOrderItems)
                            {
                                Console.WriteLine("     Item Price: {0}, Item Quantity: {1}", item.ActualCost, item.Quantity);
                                Console.WriteLine("     Name: {0}, Product Number: {1}", item.Product.Name, item.Product.ProductNumber);
                            }
                        }
                    }

                    //No scope.Complete here as no write tasks are being scoped.

                    scope.Dispose();
                }

                Console.WriteLine();

                // Disconnect from the service
                proxy.Close();
            }
            catch (FaultException<SystemFault> sf)
            {
                Console.WriteLine("SystemFault {0}: {1}\n{2}",
                    sf.Detail.SystemOperation, sf.Detail.SystemMessage,
                    sf.Detail.SystemReason);
            }
            catch (FaultException<DatabaseFault> dbf)
            {
                Console.WriteLine("DatabaseFault {0}: {1}\n{2}",
                    dbf.Detail.DbOperation, dbf.Detail.DbMessage,
                    dbf.Detail.DbReason);
            }
            catch (FaultException e)
            {
                Console.WriteLine("{0}: {1}", e.Code.Name, e.Reason);
            }
            catch (Exception e)
            {
                Console.WriteLine("General exception: {0}", e.Message);
                Console.WriteLine("Inner Exception: {0}", e.InnerException);
            }

            //Console.WriteLine("Press ENTER to continue");
            //Console.ReadLine();
        }
Ejemplo n.º 3
0
        public static void RunTest()
        {
            PermissiveCertificatePolicy.Enact("CN=HTTPS-Server");
            Console.Write("Initializing Sales Proxy...");
            BicycleWorldSalesServiceClient proxy = new BicycleWorldSalesServiceClient("wsHttpBinding_BicycleWorldSalesService");

            Console.WriteLine(" done");

            proxy.ClientCredentials.UserName.UserName = "******";
            proxy.ClientCredentials.UserName.Password = "******";

            // Test the operations in the service

            try
            {
                Console.WriteLine(proxy.Test());

                // The encrypted list will timeout for slightly large lists (in the hundreds), so the list is
                // being grabbed in smaller chunks.  Transaction logic is used to lock the list while the list is being
                // grabbed.
                TransactionOptions transactionOptions = new TransactionOptions();
                transactionOptions.IsolationLevel = IsolationLevel.RepeatableRead;
                transactionOptions.Timeout        = new TimeSpan(0, 2, 0);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions))
                {
                    Console.WriteLine("Test 1: List all Sales Orders");
                    int take  = 5;
                    int count = proxy.GetSalesOrderListCount();
                    for (int skip = 0; skip < count; skip = skip + take)
                    {
                        var salesOrders = proxy.GetSalesOrderList(take, skip);
                        foreach (var saleOrder in salesOrders)
                        {
                            Console.WriteLine(" Sales Order ID: {0:000}", saleOrder.SalesOrderID);
                            foreach (var item in saleOrder.SalesOrderItems)
                            {
                                Console.WriteLine("     Item Price: {0}, Item Quantity: {1}", item.ActualCost, item.Quantity);
                                Console.WriteLine("     Name: {0}, Product Number: {1}", item.Product.Name, item.Product.ProductNumber);
                            }
                        }
                    }

                    //No scope.Complete here as no write tasks are being scoped.

                    scope.Dispose();
                }

                Console.WriteLine();

                // Disconnect from the service
                proxy.Close();
            }
            catch (FaultException <SystemFault> sf)
            {
                Console.WriteLine("SystemFault {0}: {1}\n{2}",
                                  sf.Detail.SystemOperation, sf.Detail.SystemMessage,
                                  sf.Detail.SystemReason);
            }
            catch (FaultException <DatabaseFault> dbf)
            {
                Console.WriteLine("DatabaseFault {0}: {1}\n{2}",
                                  dbf.Detail.DbOperation, dbf.Detail.DbMessage,
                                  dbf.Detail.DbReason);
            }
            catch (FaultException e)
            {
                Console.WriteLine("{0}: {1}", e.Code.Name, e.Reason);
            }
            catch (Exception e)
            {
                Console.WriteLine("General exception: {0}", e.Message);
                Console.WriteLine("Inner Exception: {0}", e.InnerException);
            }

            //Console.WriteLine("Press ENTER to continue");
            //Console.ReadLine();
        }
        public static void RunTestRemoveAndCheckoutItems()
        {
            PermissiveCertificatePolicy.Enact("CN=HTTPS-Server");
            Console.Write("Initializing Sales Proxy...");
            BicycleWorldSalesServiceClient proxy = new BicycleWorldSalesServiceClient("wsHttpBinding_BicycleWorldSalesService");
            Console.WriteLine(" done");

            proxy.ClientCredentials.UserName.UserName = "******";
            proxy.ClientCredentials.UserName.Password = "******";

            // Test the operations in the service

            try
            {
                Console.WriteLine("Press ENTER to start shopping cart");
                Console.ReadLine();

                TransactionOptions transactionOptions = new TransactionOptions();
                transactionOptions.IsolationLevel = IsolationLevel.RepeatableRead;
                transactionOptions.Timeout = new TimeSpan(0, 1, 0);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions))
                {
                    IDictionary<string, string> context = null;
                    IContextManager contextManager = proxy.InnerChannel.GetProperty<IContextManager>();
                    if (context != null) { contextManager.SetContext(context); }

                    proxy.AddItemToCart("BK-M18B-42");

                    if (context == null)
                    {
                        context = contextManager.GetContext();
                    }

                    Console.WriteLine(proxy.GetShoppingCart());

                    proxy.RemoveItemFromCart("BK-R19B-44");

                    Console.WriteLine(proxy.GetShoppingCart());

                    if (proxy.Checkout())
                    {
                        scope.Complete();
                        Console.WriteLine("Goods purchased.");
                    }
                }

                Console.WriteLine();

                // Disconnect from the service
                proxy.Close();
            }
            catch (FaultException<SystemFault> sf)
            {
                Console.WriteLine("SystemFault {0}: {1}\n{2}",
                    sf.Detail.SystemOperation, sf.Detail.SystemMessage,
                    sf.Detail.SystemReason);
            }
            catch (FaultException<DatabaseFault> dbf)
            {
                Console.WriteLine("DatabaseFault {0}: {1}\n{2}",
                    dbf.Detail.DbOperation, dbf.Detail.DbMessage,
                    dbf.Detail.DbReason);
            }
            catch (FaultException e)
            {
                Console.WriteLine("{0}: {1}", e.Code.Name, e.Reason);
            }
            catch (Exception e)
            {
                Console.WriteLine("General exception: {0}", e.Message);
                Console.WriteLine("Inner Exception: {0}", e.InnerException);
            }

            Console.WriteLine("Press ENTER to continue");
            Console.ReadLine();
        }
        public static void RunTestRemoveAndCheckoutItems()
        {
            PermissiveCertificatePolicy.Enact("CN=HTTPS-Server");
            Console.Write("Initializing Sales Proxy...");
            BicycleWorldSalesServiceClient proxy = new BicycleWorldSalesServiceClient("wsHttpBinding_BicycleWorldSalesService");

            Console.WriteLine(" done");

            proxy.ClientCredentials.UserName.UserName = "******";
            proxy.ClientCredentials.UserName.Password = "******";

            // Test the operations in the service

            try
            {
                //Console.WriteLine(proxy.Login());


                Console.WriteLine("Press ENTER to start shopping cart");
                Console.ReadLine();

                TransactionOptions transactionOptions = new TransactionOptions();
                transactionOptions.IsolationLevel = IsolationLevel.RepeatableRead;
                transactionOptions.Timeout        = new TimeSpan(0, 1, 0);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions))
                {
                    IDictionary <string, string> context = null;
                    IContextManager contextManager       = proxy.InnerChannel.GetProperty <IContextManager>();
                    if (context != null)
                    {
                        contextManager.SetContext(context);
                    }

                    proxy.AddItemToCart("BK-M18B-42");

                    if (context == null)
                    {
                        context = contextManager.GetContext();
                    }

                    Console.WriteLine(proxy.GetShoppingCart());

                    proxy.RemoveItemFromCart("BK-R19B-44");

                    Console.WriteLine(proxy.GetShoppingCart());

                    if (proxy.Checkout())
                    {
                        scope.Complete();
                        Console.WriteLine("Goods purchased.");
                    }
                }

                Console.WriteLine();

                // Disconnect from the service
                proxy.Close();
            }
            catch (FaultException <SystemFault> sf)
            {
                Console.WriteLine("SystemFault {0}: {1}\n{2}",
                                  sf.Detail.SystemOperation, sf.Detail.SystemMessage,
                                  sf.Detail.SystemReason);
            }
            catch (FaultException <DatabaseFault> dbf)
            {
                Console.WriteLine("DatabaseFault {0}: {1}\n{2}",
                                  dbf.Detail.DbOperation, dbf.Detail.DbMessage,
                                  dbf.Detail.DbReason);
            }
            catch (FaultException e)
            {
                Console.WriteLine("{0}: {1}", e.Code.Name, e.Reason);
            }
            catch (Exception e)
            {
                Console.WriteLine("General exception: {0}", e.Message);
                Console.WriteLine("Inner Exception: {0}", e.InnerException);
            }

            Console.WriteLine("Press ENTER to continue");
            Console.ReadLine();
        }