C# SqlDependency - 无效的对象名称

C# SqlDependency - Invalid object name

我想检查 Sql依赖关系,但在启动时遇到了问题。我正在使用下面的代码(来自 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/detecting-changes-with-sqldependency)。

在 运行 方法 SqlDependency.Start(connString,queue).

时抛出错误 "System.Data.SqlClient.SqlException: 'Invalid object name '[core].[intServiceClient_Queue]'.'"

我正在 Sql 服务器管理员帐户上连接 SSPI。我确定该对象是服务代理队列并且它存在。

void Initialization()  
{  
    // Create a dependency connection.  
    SqlDependency.Start(connectionString, queueName);  
}  

void SomeMethod()  
{  
    // Assume connection is an open SqlConnection.  

    // Create a new SqlCommand object.  
    using (SqlCommand command=new SqlCommand(  
        "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",   
        connection))  
    {  

        // Create a dependency and associate it with the SqlCommand.  
        SqlDependency dependency=new SqlDependency(command);  
        // Maintain the refence in a class member.  

        // Subscribe to the SqlDependency event.  
        dependency.OnChange+=new  
           OnChangeEventHandler(OnDependencyChange);  

        // Execute the command.  
        using (SqlDataReader reader = command.ExecuteReader())  
        {  
            // Process the DataReader.  
        }  
    }  
}  

// Handler method  
void OnDependencyChange(object sender,   
   SqlNotificationEventArgs e )  
{  
  // Handle the event (for example, invalidate this cache entry).  
}  

void Termination()  
{  
    // Release the dependency.  
    SqlDependency.Stop(connectionString, queueName);  
}  

这是 SqlDependency 中的 bug/shortcoming:它不支持架构名称。 [core].[intServiceClient_Queue] 被认为是一个对象的名称并被转义,产生一个无效的对象名称。将您的队列移动到您的用户的默认架构(很可能 dbo),或将默认架构设置为 core 并对其他所有内容进行架构限定。