我想在ASP.NET MVC 3 App中使用OracleChangeNotifications
。我使用here中的示例代码创建了一个简单的控制台应用程序,它按预期工作。如果我更改已注册的数据库表,则会在控制台应用程序中触发通知。
然后我使用相同的示例代码创建了一个ASP.NET MVC 3应用程序,但MVC应用程序没有收到任何通知。我正在使用oracle 11g。显然,监听器在oracle数据库中注册。如果我运行查询:
SELECT * FROM user_change_notification_regs;
我得到以下结果:
REGID: 127
REGFLAGS: 4
CALLBACK: net8://(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=59747)?PR=0
OPERATIONS_FILTER: 0
CHANGELAG: 0
TIMEOUT: 48556
TABLE_NAME: MyTable
我的猜测是IIS以某种方式阻止了回调,但我无法弄清楚为什么? 有什么想法吗?
Heres是我正在使用的代码:
using System;
using System.Data;
using NLog;
using Oracle.DataAccess.Client;
namespace CacheTestWebApp.Services
{
public class Notification
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private const string ConnectionString = "<connection_string>";
private const string TableName = "MyTable";
private const string QueryString = "select * from " + TableName;
public static void RegisterNotification()
{
try
{
using (var con = new OracleConnection(ConnectionString))
{
con.Open();
var cmd = new OracleCommand(QueryString, con);
var dependency = new OracleDependency();
dependency.OnChange += dependency_OnChange;
dependency.AddCommandDependency(cmd);
cmd.Notification.IsNotifiedOnce = false;
cmd.AddRowid = true;
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}
}
catch (Exception e)
{
_logger.Error(e.Message);
}
}
private static void dependency_OnChange(object sender, OracleNotificationEventArgs eventArgs)
{
// handle notification
}
}
}
Notification.RegisterNotification()在Global.asax.cs的Application_Start()中执行:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Notification.RegisterNotification();
}