1

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

4

1 回答 1

0

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

事实证明,我引用了 SQL Server 2005 SDK 文件夹中的程序集。我删除了这些并引用了 SQL Server 2008 中的那些,并且从那以后它运行良好。

也许这会帮助别人。

于 2010-03-18T00:14:26.213 回答