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());
}
}
}
}
i try it but not understand application,
please source sample for me ?? i need it and tks you
All the source are in the blog itself.
where is download it ???
I need run demo please help me!
“In my case, there were some issues with launching the application from the External Activator which were resolved after setting some policies and UAC controls. ”
Could you elaborate on the issues you had please, i tried the sample and it does not execute the program, it’s probably my 5th attempt at runinng samples and the Main not being called. I dont have any errors in event log.
Thanks.
In my case, it’s probably because nothing is being writtent to MyNotificationQueue.
I noticed that c# has a typo there, maybe there is a typo in sql as well?
Your help is much apreciated.
Thanks.
Sourabh, have you managed to get this to work when using more than one database? The example is relating to multiple queues. So SSBEAS is installed on a server and there are two identical queues on two different databases which will invoke two different applications. I can’t find any examples of how this in implemented in the config. Only the first NotificationService entry set to true in the EAService.config can be used. The second is ignored.
Hope you can help.