Question

I've got a Windows service that uses a Service Broker queue to get notifications that some processing is needed:

var cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MathsEngine.Properties.Settings.TargetConnectionString"].ToString());

var cmd = new SqlCommand("WAITFOR ( RECEIVE * FROM dbo.MathsEngineQueue);", cnn) {CommandTimeout = 0};

cnn.Open();

// Execute the command - we will wait here until a new entry appears in the Notification Queue
//
SqlDataReader reader = cmd.ExecuteReader();

// Get the message text from the reader
//
while (reader.Read())
{
    // Get the message body text and convert into a legible format
    //
    _messageText = Encoding.Unicode.GetString(reader.GetSqlBinary(reader.GetOrdinal("message_body")).Value);
}

reader.Close();
reader.Dispose();
cmd.Dispose();

I'm now starting to use Entity Framework 6.0 to do all my db interactions. I've been trying to find a way to monitor the queue and get any messages via EF. So far the only partial answers I've found involve very convoluted use of SQLDependency which still doesn't let me get messages out of the queue.

Is there actually a way of doing this in EF or am I stuck for now having this one area unchanged?

Was it helpful?

Solution 3

Agree to @Rob. I've spent fair bit amount of time to figure out this issue as well and got it concluded that Service Broker (SB) won't get along with Entity Framework 6 (mine is 6.1.1) at this stage. All unit tests (with LocalDB, actually) I created, using EF without SB, were successful but, once they are integrated with SB, they won't work.

To clarify more, SELECT statements work but INSERT, UPDATE and DELETE won't change anything.

OTHER TIPS

After a great deal of searching, it looks like I'm stuck with the old way of doing this. Its no real biggy, as entity framework is meant to make the normal querying easier and more logibcal, which it does.

I'm going to treat this functionality as a special case and keep it separate.

I've ported the EFAutoRefresh library (originally from CodeProject) to EF 6. It works okay on some data sets, and others fails to update. Have a play around with it, any feedback would be great.

https://github.com/wonea/EFAutoRefresh

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top