/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 16#region Using directives 17 18using System; 19using System.IO; 20using System.Text; 21using System.Data; 22using System.Data.SqlTypes; 23using System.Data.SqlClient; 24using Microsoft.Samples.SqlServer; 25 26//System.Data.SqlClient.SqlConnection 27//System.Data.SqlServer.SqlConnection 28 29#endregion 30 31namespace Microsoft.Samples.SqlServer 32{ 33 class HelloWorldClient 34 { 35 static void Main() 36 { 37 SqlConnection conn = null; 38 SqlTransaction tran = null; 39 TextReader reader = null; 40 41 try 42 { 43 Console.WriteLine("Connecting to SQL Server instance"); 44 45 // Create a connection 46 conn = new SqlConnection( 47 "Initial Catalog=ssb_HelloWorld; Data Source=localhost;Integrated Security=SSPI;"); 48 // Open the connection 49 conn.Open(); 50 Console.WriteLine("Connected to SQL Server instance"); 51 52 // Begin a transaction 53 tran = conn.BeginTransaction(); 54 Console.WriteLine("\nTransaction 1 begun"); 55 56 // Create a service object 57 Service client = new Service("HelloWorldClient", conn, tran); 58 59 // Set the FetchSize to 1 since we will receive one message at a time 60 // i.e. use RECEIVE TOP(1) 61 client.FetchSize = 1; 62 63 // Begin a dialog with the HelloWorld service 64 Conversation dialog = client.BeginDialog( 65 "HelloWorldService", 66 null, 67 "Greeting", 68 TimeSpan.FromMinutes(1), 69 false, 70 conn, 71 tran); 72 Console.WriteLine("Dialog begun from service (HelloWorldClient) to service (HelloWorldService)"); 73 74 // Create an empty request message 75 Message request = new Message("Request", null); 76 77 // Send the message to the service 78 dialog.Send(request, conn, tran); 79 Console.WriteLine("Message sent of type '" + request.MessageType + "'"); 80 81 tran.Commit(); // Message isn't sent until transaction has been committed 82 Console.WriteLine("Transaction 1 committed"); 83 84 // Begin transaction 85 tran = conn.BeginTransaction(); 86 Console.WriteLine("\nTransaction 2 begun"); 87 88 // Waitfor messages on this conversation 89 Console.WriteLine("Waiting for Response...."); 90 91 client.WaitforTimeout = TimeSpan.FromSeconds(5); 92 if (client.GetConversation(dialog, conn, tran) == null) 93 { 94 Console.WriteLine("No message received - Ending dialog with Error"); 95 dialog.EndWithError(1, "no response within 5 seconds.", conn, tran); 96 tran.Commit(); 97 Console.WriteLine("Transaction 2 committed"); 98 conn.Close(); 99 Console.WriteLine("\nConnection closed - exiting"); 100 return; 101 } 102 103 // Fetch the message from the conversation 104 Message response = dialog.Receive(); 105 106 // Output the message to the Console 107 //Console.WriteLine("Message received of type '" + response.Type + "'"); 108 if (response.Body != null) 109 { 110 Console.Write("Message contains: "); 111 reader = new StreamReader(response.Body); 112 Console.WriteLine(reader.ReadToEnd()); 113 } 114 115 // End the conversation 116 dialog.EndConversation(conn, tran); 117 Console.WriteLine("Ended Dialog"); 118 119 //tran.Commit(); // Remember to commit again 120 Console.WriteLine("Transaction 2 committed"); 121 122 // Close the database connection 123 conn.Close(); 124 Console.WriteLine("\nConnection closed - exiting"); 125 } 126 catch (ServiceException e) 127 { 128 Console.WriteLine("An exception occurred - {0}\n", e.ToString()); 129 if (tran != null) 130 { 131 tran.Rollback(); 132 Console.WriteLine("\nTransaction rolled back"); 133 } 134 if (conn != null) 135 { 136 conn.Close(); 137 Console.WriteLine("\nConnection closed - exiting"); 138 } 139 } 140 finally 141 { 142 if (reader != null) 143 reader.Close(); 144 145 Console.WriteLine(); 146 Console.WriteLine("Press Enter to Exit"); 147 Console.ReadLine(); 148 } 149 } 150 } 151}