Groups | Blog | Home
all groups > sql server replication > august 2005 >

sql server replication : SQL 2005 and SQL Express VS2005 July CTP Merge Replication


msmith
8/28/2005 7:55:03 PM
When I execute
(MergePullSubscription)mPullSub.SynchronizationAgent.Synchronize() I get an
error {"The MergeSynchronizationAgent class must be instantiated on a
Single-Threaded Apartment (STA) thread."}, but as far as I can tell it is. I
even added the attribure [STAThread] and still get the error.

Raymond Mak [MSFT]
8/29/2005 10:07:01 AM
Hi Mike,

For the [STAThread] property to work, it must be used on the "first" method
of a thread. If you want to change the main thread of a process to be an
STAThread, you would have to decorate the Main method of the program with the
[STAThread] attribute. I am guessing that in your case, only the method
immediately wrapping the call to mPullSub.SynchronizationAgent is decorated
with the [STAThread] attribute. In any case, we can probably figure out
exactly what is going if you can post a few relevant code-snippets here.

HTH

-Ryamond


[quoted text, click to view]
Hilary Cotter
8/29/2005 11:55:42 AM
did you post this to microsoft.private.sqlserver2005.replication?

--
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html

Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
[quoted text, click to view]

msmith
8/29/2005 2:37:39 PM
I did add the attribute to the Main method of the program.
There are no other Thread attributes anywhere else in the program.

Mike

[quoted text, click to view]
msmith
8/29/2005 3:11:03 PM
Actually, it wasn't on the Main thread, that's fixed, now it says "The
subscription to publication 'MTSMaster' has expired or does not exist."

Code:

public bool ReplSyncWin(bool init)
{
pubDB = replMasterPublisherDb;
//public Form1()
//{
//InitializeComponent();
//}

//private void button1_Click(object sender, EventArgs e)

//{
//Cursor.Current = Cursors.WaitCursor;
if (_synchronizeMergePullSubscription())
{
MobileTech.Core.Configuration.AppStart = false;
return true;
}
else
{
return false;
}

}
private bool _synchronizeMergePullSubscription()
{
try
{
//localConn = "Server=localhost;Integrated
Security=True;Database=C:\\ROUTENET\\MobileTech.UI.WinCE\\DatabaseMobileTech.sdf";
//subscriberSQLConn = new SqlConnection(localConn);
//subscriberSQLConn.Open();
subscriberConn = new ServerConnection(subServer);

subscriberConn.Connect();
MergePullSubscription mPullSub = new MergePullSubscription();
mPullSub.ConnectionContext = subscriberConn;
mPullSub.DatabaseName = subDB;
mPullSub.PublisherName = replMasterPublisher;
mPullSub.PublicationDBName = pubDB;
mPullSub.PublicationName = replMasterPublication;
//mPullSub.InternetLogin = replInternetLogin;
//mPullSub.InternetPassword = replInternetPassword;
//mPullSub.InternetSecurityMode =
AuthenticationMethod.WindowsAuthentication;
//mPullSub.InternetUrl = replInternetUrl;
////mPullSub.PublisherSecurity =
AuthenticationMethod.WindowsAuthentication;
//mPullSub.UseWebSynchronization = true;
// if pull subscription exists, start the sync

if (mPullSub.LoadProperties())

{
mPullSub.SynchronizationAgent.ExchangeType =
MergeExchangeType.Bidirectional;
mPullSub.SynchronizationAgent.Synchronize();
}
else
{
subscriberConn.Disconnect();
// create the pull subscription and retry

if (_createMergePullSubscription())

_synchronizeMergePullSubscription();

}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString() + " " + ex.Message);
return false;
}
finally
{
subscriberConn.Disconnect();

//Cursor.Current = Cursors.Default;
}
return true;
}

private bool _createMergePullSubscription()
{

bool retVal = false;

// To create a pull subscription, you need a connection to the
Subscriber
//and Publisher.

//subscriberSQLConn = new SqlConnection(localConn);
//subscriberSQLConn.Open();
subscriberConn = new ServerConnection(subServer);

//publisherSQLConn = new SqlConnection(localConn);
//publisherSQLConn.
//publisherSQLConn.Open();
publisherConn = new
ServerConnection("10.0.0.49");//publisherSQLConn);

try

{

subscriberConn.Connect();
publisherConn.Connect();

// Register a new Subscriber at the Publisher, unless it is
already
//registered.

RegisteredSubscriber subscriber;

subscriber = new RegisteredSubscriber(subServer,
publisherConn);

if (!subscriber.IsExistingObject)
{
subscriber.Create();
subscriber.Refresh();
}

MergePublication mergePublication = new MergePublication();
mergePublication.Name = replMasterPublication;
mergePublication.DatabaseName = pubDB;
mergePublication.ConnectionContext = publisherConn;

// If the publication exists, define the subscription at the
Subscriber.

if (mergePublication.LoadProperties())
{

// If the publication does not support pull
subscriptions, allow them.

// You must do this bitwise because
Publication.Attributes is really a
//bitmask.

if ((mergePublication.Attributes &
PublicationAttributes.AllowPull) == 0)

{
mergePublication.Attributes =
mergePublication.Attributes |
PublicationAttributes.AllowPull;
mergePublication.CommitPropertyChanges();
mergePublication.Refresh();
}

MergePullSubscription mergePullSubscription = new
MergePullSubscription();
// Define pull subscription properties.
mergePullSubscription.ConnectionContext = subscriberConn;
mergePullSubscription.DatabaseName = subDB;
mergePullSubscription.PublisherName = replMasterPublisher;
mergePullSubscription.PublicationDBName = pubDB;
mergePullSubscription.PublicationName =
replMasterPublication;
mergePullSubscription.SubscriberType =
MergeSubscriberType.Local;
// Specify the Windows account under which the Merge
Agent job runs.

mergePullSubscription.SynchronizationAgentProcessSecurity.Login = "msmith";

mergePullSubscription.SynchronizationAgentProcessSecurity.Password =
"Giles1002590";
mergePullSubscription.CreateSyncAgentByDefault = true;

// Create the subscription.

if (!mergePullSubscription.IsExistingObject) // DDS
{
mergePullSubscription.Create();
mergePullSubscription.Refresh();
// Register the pull subscription at the Publisher.

mergePublication.MakePullSubscriptionWellKnown(subServer,
subDB,
mergePullSubscription.SyncType,
mergePullSubscription.SubscriberType,
mergePullSubscription.Priority);
Raymond Mak [MSFT]
8/30/2005 10:05:01 AM
Hi Mike,

I am admittedly no expert in SQL2005 websync so I would encourage you to
forward your new problem to the SQL2005 replication newsgroup. That said, the
following tricks may help you troubleshoot further:

1) Instead of chaining your call directly to the Synchronize() method from
the subscription object (i.e. mPullSub.SynchronizationAgent.Synchronize()),
you may want to save off a reference to the (Merge)SynchronizationAgent in a
variable and then hook up the Status event handler with something like this:

MergeSynchronizationAgent syncAgent = mPullSub.SynchronizationAgent
syncAgent.ExchangeType = MergeExchangeType.BiDirectional
syncAgent.Status += new AgentCore.StatusEventHandler(yourStatusEventHandler);
syncAgent.Syncrhonize();

(Warning: the snippet above will not compile)

2) Use SQL Profiler to trace the calls from the merge agent and see if
publisher\publisherdb\publication\subscriber\subscriberdb names are what you
expect them to be.

-Raymond

[quoted text, click to view]
AddThis Social Bookmark Button