/Kilimanjaro_Trunk/HelloWorld_CLR/HelloWorldClient/cs/HelloWorldClient/HelloWorldClient.cs

# · C# · 151 lines · 95 code · 23 blank · 33 comment · 10 complexity · 1ee68c3c37e8cbf50c7871a04dea9ac6 MD5 · raw file

  1. //-----------------------------------------------------------------------
  2. // This file is part of the Microsoft Code Samples.
  3. //
  4. // Copyright (C) Microsoft Corporation. All rights reserved.
  5. //
  6. // This source code is intended only as a supplement to Microsoft
  7. // Development Tools and/or on-line documentation. See these other
  8. // materials for detailed information regarding Microsoft code samples.
  9. //
  10. // THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  11. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. // PARTICULAR PURPOSE.
  14. //-----------------------------------------------------------------------
  15. #region Using directives
  16. using System;
  17. using System.IO;
  18. using System.Text;
  19. using System.Data;
  20. using System.Data.SqlTypes;
  21. using System.Data.SqlClient;
  22. using Microsoft.Samples.SqlServer;
  23. //System.Data.SqlClient.SqlConnection
  24. //System.Data.SqlServer.SqlConnection
  25. #endregion
  26. namespace Microsoft.Samples.SqlServer
  27. {
  28. class HelloWorldClient
  29. {
  30. static void Main()
  31. {
  32. SqlConnection conn = null;
  33. SqlTransaction tran = null;
  34. TextReader reader = null;
  35. try
  36. {
  37. Console.WriteLine("Connecting to SQL Server instance");
  38. // Create a connection
  39. conn = new SqlConnection(
  40. "Initial Catalog=ssb_HelloWorld; Data Source=localhost;Integrated Security=SSPI;");
  41. // Open the connection
  42. conn.Open();
  43. Console.WriteLine("Connected to SQL Server instance");
  44. // Begin a transaction
  45. tran = conn.BeginTransaction();
  46. Console.WriteLine("\nTransaction 1 begun");
  47. // Create a service object
  48. Service client = new Service("HelloWorldClient", conn, tran);
  49. // Set the FetchSize to 1 since we will receive one message at a time
  50. // i.e. use RECEIVE TOP(1)
  51. client.FetchSize = 1;
  52. // Begin a dialog with the HelloWorld service
  53. Conversation dialog = client.BeginDialog(
  54. "HelloWorldService",
  55. null,
  56. "Greeting",
  57. TimeSpan.FromMinutes(1),
  58. false,
  59. conn,
  60. tran);
  61. Console.WriteLine("Dialog begun from service (HelloWorldClient) to service (HelloWorldService)");
  62. // Create an empty request message
  63. Message request = new Message("Request", null);
  64. // Send the message to the service
  65. dialog.Send(request, conn, tran);
  66. Console.WriteLine("Message sent of type '" + request.MessageType + "'");
  67. tran.Commit(); // Message isn't sent until transaction has been committed
  68. Console.WriteLine("Transaction 1 committed");
  69. // Begin transaction
  70. tran = conn.BeginTransaction();
  71. Console.WriteLine("\nTransaction 2 begun");
  72. // Waitfor messages on this conversation
  73. Console.WriteLine("Waiting for Response....");
  74. client.WaitforTimeout = TimeSpan.FromSeconds(5);
  75. if (client.GetConversation(dialog, conn, tran) == null)
  76. {
  77. Console.WriteLine("No message received - Ending dialog with Error");
  78. dialog.EndWithError(1, "no response within 5 seconds.", conn, tran);
  79. tran.Commit();
  80. Console.WriteLine("Transaction 2 committed");
  81. conn.Close();
  82. Console.WriteLine("\nConnection closed - exiting");
  83. return;
  84. }
  85. // Fetch the message from the conversation
  86. Message response = dialog.Receive();
  87. // Output the message to the Console
  88. //Console.WriteLine("Message received of type '" + response.Type + "'");
  89. if (response.Body != null)
  90. {
  91. Console.Write("Message contains: ");
  92. reader = new StreamReader(response.Body);
  93. Console.WriteLine(reader.ReadToEnd());
  94. }
  95. // End the conversation
  96. dialog.EndConversation(conn, tran);
  97. Console.WriteLine("Ended Dialog");
  98. //tran.Commit(); // Remember to commit again
  99. Console.WriteLine("Transaction 2 committed");
  100. // Close the database connection
  101. conn.Close();
  102. Console.WriteLine("\nConnection closed - exiting");
  103. }
  104. catch (ServiceException e)
  105. {
  106. Console.WriteLine("An exception occurred - {0}\n", e.ToString());
  107. if (tran != null)
  108. {
  109. tran.Rollback();
  110. Console.WriteLine("\nTransaction rolled back");
  111. }
  112. if (conn != null)
  113. {
  114. conn.Close();
  115. Console.WriteLine("\nConnection closed - exiting");
  116. }
  117. }
  118. finally
  119. {
  120. if (reader != null)
  121. reader.Close();
  122. Console.WriteLine();
  123. Console.WriteLine("Press Enter to Exit");
  124. Console.ReadLine();
  125. }
  126. }
  127. }
  128. }