using System;
using System.Collections.Generic;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace MyMessageApplication
{
class Program
{
/// <summary>
/// Application name used when connecting to Sql Server and writing Windows Event Log entries.
/// </summary>
private const string ApplicationName = "MyMessageApplication";
/// <summary>
/// Defines how many milliseconds the <c>RECEIVE</c> statement should wait for messages.
/// </summary>
private const int WaitforTimeout = 5000;
/// <summary>
/// Predefined Sql Server message type name for end dialog messages.
/// </summary>
private const string EndDialogMessageType = "http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog";
/// <summary>
/// Predefined Sql Server message type name for conversation error messages.
/// </summary>
private const string ErrorMessageType = "http://schemas.microsoft.com/SQL/ServiceBroker/Error";
/// <summary>
/// Services request messages by doing all the necessary computation and preparing the payload of a reply message.
/// </summary>
///
///Create a message logger function
///
public static void LogMessageToFile(string message)
{
StreamWriter sw = File.AppendText(@"D:\SQLDataFiles\MyMessageApplicationLog.txt");
string entry = String.Format("{0:G}: {1}.", System.DateTime.Now, message);
sw.WriteLine(entry);
sw.Close();
}
static void Main()
{
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.ApplicationName = ApplicationName;
csb.DataSource = "<ServerName>";
csb.InitialCatalog = "SSBEA";
csb.UserID = "UserAccount";
csb.Password = "Password for the User Account above";
csb.MultipleActiveResultSets = true;
try
{
SqlConnection conn = new SqlConnection(csb.ToString());
conn.Open();
bool MsgRcv;
do
{
MsgRcv = false;
SqlTransaction tran = conn.BeginTransaction();
using (tran)
{
SqlCommand comm = conn.CreateCommand();
comm.Transaction = tran;
//LogMessageToFile("Querying from the Queue");
comm.CommandText = string.Format(
"WAITFOR (RECEIVE TOP (1) conversation_handle, message_type_name, convert(xml,message_body) FROM {0}), TIMEOUT {1}",
"MySSBEAQueue", WaitforTimeout);
SqlDataReader reader = comm.ExecuteReader();
while(reader.Read())
{
MsgRcv = true;
System.Data.SqlTypes.SqlGuid handle = reader.GetSqlGuid(0);
System.Data.SqlTypes.SqlString TypeName = reader.GetSqlString(1);
System.Data.SqlTypes.SqlXml message = reader.GetSqlXml(2);
if(TypeName == EndDialogMessageType || TypeName == ErrorMessageType)
{
if (TypeName == ErrorMessageType)
{
LogMessageToFile(message.ToString());
}
SqlCommand endconv = conn.CreateCommand();
endconv.Transaction = tran;
endconv.CommandText = "End Conversation @handle";
endconv.Parameters.Add("@handle", System.Data.SqlDbType.UniqueIdentifier);
endconv.Parameters["@handle"].Value = handle;
endconv.ExecuteNonQuery();
}
else
{
SqlCommand com = conn.CreateCommand();
//LogMessageToFile("Inserting into the table");
com.CommandText = "Insert into SSBEA.dbo.ReceievedMassage(RecievedMessage) values (convert(varchar(1000),@payload))";
com.Parameters.Add("@payload", System.Data.SqlDbType.Xml);
com.Parameters["@payload"].Value = message;
com.Transaction = tran;
com.ExecuteNonQuery();
//LogMessageToFile("Inserted this message to the table:- " + message.Value);
}
}
tran.Commit();
}
} while (MsgRcv);
}
catch (Exception e)
{
LogMessageToFile(e.Message.ToString());
}
}
}
}