SQL Server Express 2005 Merge Replication using RMO causes Null Reference exception

Posted by Craig Shearer on Stack Overflow See other posts from Stack Overflow or by Craig Shearer
Published on 2010-03-17T10:21:39Z Indexed on 2010/03/18 0:21 UTC
Read the original article Hit count: 701

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.

© Stack Overflow or respective owner

Related posts about sqlserver-2005

Related posts about merge-replication