/// <summary> /// Handles the operation of buying a selected book /// </summary> private void btnBuy_Click(object sender, EventArgs e) { // check if any book is selected for purchase if (lstBooks.SelectedItem == null) { MessageBox.Show("No book selected for purchase", "BookStore Client"); } else { // get the selected book ID string selectedBookItem = lstBooks.SelectedItem.ToString(); int startPos = selectedBookItem.IndexOf('.') + 1; int endPos = selectedBookItem.IndexOf(','); string bookName = selectedBookItem.Substring(startPos, endPos - startPos); bookName = bookName.Trim(); BuyBookClient myBuyBookClient = new BuyBookClient(); try { // Add the book name as a "resource" header to the endpoint address for the service EndpointAddressBuilder myEndpointAddressBuilder = new EndpointAddressBuilder(myBuyBookClient.Endpoint.Address); myEndpointAddressBuilder.Headers.Add(AddressHeader.CreateAddressHeader(Constants.BookNameHeaderName, Constants.BookNameHeaderNamespace, bookName)); myBuyBookClient.Endpoint.Address = myEndpointAddressBuilder.ToEndpointAddress(); // Send the request to the service. This will result in the following steps; // 1. Request a security token from HomeRealmSTS authenticating using Windows credentials // 2. Request a security token from BookStoreSTS authenticating using token from 1. // 3. Send the BuyBook request to the BookStoreService authenticating using token from 2. string response = myBuyBookClient.BuyBook("*****@*****.**", "One Microsoft Way, Redmond, WA 98052"); MessageBox.Show(response, "BookStore Client"); myBuyBookClient.Close(); } catch (Exception ex) { myBuyBookClient.Abort(); // see if a fault has been sent back FaultException inner = GetInnerException(ex) as FaultException; if (inner != null) { MessageFault fault = inner.CreateMessageFault(); MessageBox.Show(String.Format("The server sent back a fault: {0}", fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture).Text)); } else { MessageBox.Show(String.Format("Exception while trying to purchase the selected book: {0}", ex), "BookStore Client"); } } } }
/// <summary> /// Handles the operation of buying a selected book /// </summary> private void btnBuy_Click(object sender, EventArgs e) { // check if any book is selected for purchase if (lstBooks.SelectedItem == null) { MessageBox.Show("No book selected for purchase", "BookStore Client"); } else { // get the selected book ID string selectedBookItem = lstBooks.SelectedItem.ToString(); int startPos = selectedBookItem.IndexOf('.') + 1; int endPos = selectedBookItem.IndexOf(','); string bookName = selectedBookItem.Substring(startPos, endPos - startPos); bookName = bookName.Trim(); BuyBookClient myBuyBookClient = new BuyBookClient(); try { // Send the request to the service. This will result in the following steps; // 1. Request a security token from HomeRealmSTS authenticating using Windows credentials // 2. Request a security token from BookStoreSTS authenticating using token from 1. // 3. Send the BuyBook request to the BookStoreService authenticating using token from 2. string response = myBuyBookClient.BuyBook(bookName, "*****@*****.**", "123 Main St, Buffalo, NY 94135"); MessageBox.Show(response, "BookStore Client"); myBuyBookClient.Close(); } catch (Exception ex) { myBuyBookClient.Abort(); // see if a fault has been sent back FaultException inner = GetInnerException(ex) as FaultException; if (inner != null) { MessageFault fault = inner.CreateMessageFault(); MessageBox.Show(String.Format("The server sent back a fault: {0}", fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture).Text)); } else { MessageBox.Show(String.Format("Exception while trying to purchase the selected book: {0}", ex), "BookStore Client"); } } } }