使用RMO的SQL Server Express 2005合并复制会导致Null引用异常

时间:2010-03-17 10:21:39

标签: sql-server-2005 merge-replication

我正在尝试使用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事件有关,但我没有定义处理程序,定义一个没有区别。

1 个答案:

答案 0 :(得分:0)

我并没有真正了解为什么会发生这种情况。我可以让它在一台机器上工作,但在另一台机器上我经常得到NullReferenceException。

事实证明我正在引用SQL Server 2005 SDK文件夹中的程序集。我删除了这些并引用了SQL Server 2008中的那些,并且从那以后工作正常。

也许这会帮助别人。