I'm inheriting a long-running stored procedure to be brought into a new application using Entity Framework. The stored procedure displays informative messages as it runs, through the use of
RAISERROR (@MSG, 0, 1) WITH NOWAIT
to put something out as a message.
Generally to call stored procedures I use
context.Database.ExecuteSqlCommand("exec dbo.LongStoredProcedure", parameters);
and I figured I could intercept the messages by setting up an event listener, as:
var sqlConn = context.Database.Connection as SqlConnection;
if (sqlConn != null) sqlConn.InfoMessage += sqlConn_InfoMessage;
but what that gets me, in this case, is a single hit on sqlConn_InfoMessage once the stored procedure has finished running, with all the messages concatenated with newlines in the SqlInfoMessageEventArgs message.
What I want is to capture the messages as they're being emitted in the stored procedure. What should I be doing in this case? Working directly through sqlConn for the entire thing?