使用 RMO 的 SQL Server Express 2005 合并复制导致空引用异常
我正在尝试使用 RMO 以编程方式执行合并同步。我基本上复制了 SQL Server 示例代码,如下所示:
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new MergePullSubscription(subscriptionDbName, publisherName, publicationDbName,
publicationName, conn, false);
// If the pull subscription exists, then start the synchronization.
if (subscription.LoadProperties())
{
// Check that we have enough metadata to start the agent.
if (subscription.PublisherSecurity != null || subscription.DistributorSecurity != null)
{
subscription.SynchronizationAgent.Synchronize();
}
else
{
throw new ApplicationException("There is insufficent metadata to " +
"synchronize the subscription. Recreate the subscription with " +
"the agent job or supply the required agent properties at run time.");
}
}
else
{
// Do something here if the pull subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
throw new ApplicationException("The subscription could not be " +
"synchronized. Verify that the subscription has " +
"been defined correctly.", ex);
}
finally
{
conn.Disconnect();
}
我已经正确定义了服务器合并发布,但是当我运行上述代码时,我在调用时收到空引用异常:
subscription.SynchronizationAgent.Synchronize();
堆栈跟踪如下:
at Microsoft.SqlServer.Replication.MergeSynchronizationAgent.StatusEventSinkMethod(String message, Int32 percent, Int32* returnValue)
at Test.ConsoleTest.Program.SynchronizePullSubscription() in F:\Visual Studio Projects\Test\source\Test.ConsoleTest\Program.cs:line 124
从堆栈跟踪来看,似乎与 Status 事件有关,但我没有定义处理程序,定义一个处理程序没有什么区别。
I'm trying to use RMO to programmatically perform merge synchronization. I've basically copied the SQL Server example code, as follows:
// Create a connection to the Subscriber.
ServerConnection conn = new ServerConnection(subscriberName);
MergePullSubscription subscription;
try
{
// Connect to the Subscriber.
conn.Connect();
// Define the pull subscription.
subscription = new MergePullSubscription(subscriptionDbName, publisherName, publicationDbName,
publicationName, conn, false);
// If the pull subscription exists, then start the synchronization.
if (subscription.LoadProperties())
{
// Check that we have enough metadata to start the agent.
if (subscription.PublisherSecurity != null || subscription.DistributorSecurity != null)
{
subscription.SynchronizationAgent.Synchronize();
}
else
{
throw new ApplicationException("There is insufficent metadata to " +
"synchronize the subscription. Recreate the subscription with " +
"the agent job or supply the required agent properties at run time.");
}
}
else
{
// Do something here if the pull subscription does not exist.
throw new ApplicationException(String.Format(
"A subscription to '{0}' does not exist on {1}",
publicationName, subscriberName));
}
}
catch (Exception ex)
{
// Implement appropriate error handling here.
throw new ApplicationException("The subscription could not be " +
"synchronized. Verify that the subscription has " +
"been defined correctly.", ex);
}
finally
{
conn.Disconnect();
}
I've got the server merge publication defined correctly, but when I run the above code, I get a null reference exception on the call to:
subscription.SynchronizationAgent.Synchronize();
The stack trace is as follows:
at Microsoft.SqlServer.Replication.MergeSynchronizationAgent.StatusEventSinkMethod(String message, Int32 percent, Int32* returnValue)
at Test.ConsoleTest.Program.SynchronizePullSubscription() in F:\Visual Studio Projects\Test\source\Test.ConsoleTest\Program.cs:line 124
It seems, from the stack trace, like something to do with the Status event, but I don't have a handler defined, and defining one makes no difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我并没有真正弄清楚为什么会发生这种情况。我可以让它在一台机器上工作,但在另一台机器上我不断收到 NullReferenceException。
事实证明,我正在引用 SQL Server 2005 SDK 文件夹中的程序集。我删除了这些并引用了 SQL Server 2008 中的那些,从那以后它工作得很好。
也许这会帮助别人。
I didn't really get to the bottom of why this was happening. I could get it to work on one machine, but on another machine I constantly got the NullReferenceException.
It turns out that I was referencing the assemblies in the SQL Server 2005 SDK folder. I removed these and referenced the ones from SQL Server 2008 instead and it's worked fine since.
Maybe this will help somebody else.