拉订阅(RMO编程)

Pull Subscription (RMO Programming)

I created transaction publication and tried to create subscription for this publication. Creating subscription in MS Studio works fine, but when I'm using RMO, the table in my subscription base never appear.

  1. What is wrong in my "at first time synchronize initialization"?

  2. How to set "immediately initialization"?

My code:

public static void CreateSub(string publicationName, string publisherName, string subscriberName, string subscriptionDbName, string publicationDbName)
{
    ServerConnection subscriberConn = new ServerConnection(subscriberName);
    ServerConnection publisherConn = new ServerConnection(publisherName);
    TransPublication publication;
    TransPullSubscription subscription;

    try
    {
        subscriberConn.Connect();
        publisherConn.Connect();

        publication = new TransPublication();
        publication.Name = publicationName;
        publication.DatabaseName = publicationDbName;
        publication.ConnectionContext = publisherConn;

        if (publication.IsExistingObject)
        {
            if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)
            {
                publication.Attributes |= PublicationAttributes.AllowPull;
            }
            publication.Attributes |= PublicationAttributes.ImmediateSync;
            subscription = new TransPullSubscription();
            subscription.ConnectionContext = subscriberConn;
            subscription.PublisherName = publisherName;
            subscription.PublicationName = publicationName;
            subscription.PublicationDBName = publicationDbName;
            subscription.DatabaseName = subscriptionDbName;
            subscription.SynchronizationAgentProcessSecurity.Login = @"*****";
            subscription.SynchronizationAgentProcessSecurity.Password = "****";

            subscription.CreateSyncAgentByDefault = true;

            subscription.AgentSchedule.FrequencyType = ScheduleFrequencyType.Continuously;
            subscription.Create();



            Boolean registered = false;

            foreach (TransSubscription existing in publication.EnumSubscriptions())
            {
                if (existing.SubscriberName == subscriberName&& existing.SubscriptionDBName == subscriptionDbName)
                {
                    registered = true;
                }
            }
            if (!registered)
            {
                publication.MakePullSubscriptionWellKnown(
                    subscriberName, subscriptionDbName,
                    SubscriptionSyncType.Automatic,
                    TransSubscriberType.ReadOnly);
            }
            if (subscription.LoadProperties())
            {
                if (subscription.PublisherSecurity != null)
                {
                    subscription.SynchronizationAgent.Synchronize();// class is not registered error
                }
                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
        {
            throw new ApplicationException(String.Format(
                "The publication '{0}' does not exist on {1}.",
                publicationName, publisherName));
        }
    }
    catch (Exception ex)
    {
        throw new ApplicationException(string.Format(
            "the subscription to {0} could not be created.", publisherName), ex);

    }
    finally
    {
        subscriberConn.Disconnect();
        publisherConn.Disconnect();
    }
}