SQL Server Express 2005 Merge Replication using RMO causes Null Reference exception
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(s开发者_Python百科ubscriberName);
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.
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.
精彩评论