我正在使用.NET 4.5,MVC,EF 6的项目
我天真地使用HttpRuntime缓存实现了一个缓存系统,并且需要使我对该数据更新缓存的数据无效;除了我忘了考虑我们的生产服务器发布到两个服务器的负载平衡集......:|
因此在生产时,数据更新后,应用程序有时会提供正确的数据,有时旧的数据取决于请求所针对的服务器。坏消息。
所以我决定在SQL表 AcademicTerms 上定义依赖,这是我的数据来源。但我做错了什么,我不确定是什么。
我在启用Service Broker后运行以设置权限的SQL
EXEC sp_addrole 'sql_dependency_role'
GRANT CREATE PROCEDURE to sql_dependency_role
GRANT CREATE QUEUE to sql_dependency_role
GRANT CREATE SERVICE to sql_dependency_role
GRANT REFERENCES on
CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]
to sql_dependency_role
GRANT VIEW DEFINITION TO sql_dependency_role
GRANT SELECT to sql_dependency_role
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO sql_dependency_role
GRANT RECEIVE ON QueryNotificationErrorsQueue TO sql_dependency_role
EXEC sp_addrolemember 'sql_dependency_role', 'MY_ASPNET_APP_USERNAME'
我在获取后插入新数据的实现,从而设置了SqlDependency(希望不那么天真!):
private void insertIntoCache(
AcademicTermLockingInfo newItem,
string itemKey,
Guid termID) {
var dbContextConnection = db.Database.Connection;
var connectionString = dbContextConnection.ConnectionString;
// important step otherwise it won't work
SqlDependency.Start(connectionString);
CacheItemPolicy policy = new CacheItemPolicy {
AbsoluteExpiration = DateTime.UtcNow.AddMonths(6)
};
CacheItem item = new CacheItem(itemKey, newItem);
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
// command which will be used to notify updates - probably want to parametrize this
using (SqlCommand command =
new SqlCommand(
String.Format("SELECT Name, LockDate FROM dbo.AcademicTerms WHERE ID = '{0}'",
termID),
connection)) {
SqlDependency dependency = new SqlDependency(command);
SqlChangeMonitor monitor = new SqlChangeMonitor(dependency);
policy.ChangeMonitors.Add(monitor);
MemoryCache.Default.Set(item, policy);
// execute once otherwise dependency not registered
command.ExecuteNonQuery();
}
}
}
非常感谢任何帮助!
我做过的事情:
net
和sanba
net
用户添加了每次NT *登录和sa登录,并将net
添加到sql_dependency_role
grant alter on schema::sql_dependency_role to net
和grant alter on schema::dbo to net
Broker Enabled
True
选项是否为Service Broker
DevUMS.dbo.AcademicTerms
和dbo.AcademicTerms
sys.dm_qn_subscriptions
并看到我有一个订阅,好!DevUMS.sys.transmission_queue
并发现了一个问题!
An exception occurred while enqueueing a message in the target
queue. Error: 15517, State: 1. Cannot execute as the database
principal because the principal "dbo" does not exist, this type of
principal cannot be impersonated, or you do not have permission.
答案 0 :(得分:1)
我遗失的秘诀是alter authorization on database::DevUMS to [sa];
,我在the linked SO post's answer找到了。{/ p>
还有许多其他步骤,例如添加角色以使用适当的登录,但老实说,我真的不确定这些步骤是否真的有必要。
我将在今天晚些时候发布,然后我将尝试执行最少量的步骤并在此处记录。我发现野外的文档非常分散且很差,所以我希望将这个答案作为未来可以参考的权威地点