kentico

Why integration bus executes 3 times on CreateObject event and why sitename is null during outgoing synchronisation in Kentico?


I've installed a fresh new version of Kentico v12 and I'm using the basic goat template.

I would like to be able to synchronize the creation of users and updating of personal information of those users in the frontend application with a SAP webservice.

I've added a new custom field in the user object "SAPID" and created a connector to manage the synchronization with SAP webservices.

Here is my poc code:

public class CMSIntegrationConnector : BaseIntegrationConnector
{
    /// <summary>
    /// Initializes the connector name.
    /// </summary>
    public override void Init()
    {
        // Initializes the connector name (must match the code name of the connector object in the system)
        // GetType().Name uses the name of the class as the ConnectorName
        ConnectorName = GetType().Name;
         SubscribeToObjects(TaskProcessTypeEnum.AsyncSimple, PredefinedObjectType.USER);
    }
    public override IntegrationProcessResultEnum ProcessInternalTaskAsync(GeneralizedInfo infoObj, TranslationHelper translations, TaskTypeEnum taskType, TaskDataTypeEnum dataType, string siteName, out string errorMessage)
    {
        try
        {
            if (siteName == "DancingGoat")
            {
                if (infoObj.TypeInfo.ObjectType == PredefinedObjectType.USER.ToString())
                {
                    if (taskType == TaskTypeEnum.CreateObject)
                    {
                        EventLogProvider.LogInformation("Connector", "CreateUser", "User created on SAP !!!!!");
                        UserInfo user = infoObj.MainObject as UserInfo;
                        // Call SAP webservice
                        user.SetValue("SAPID", Guid.NewGuid());
                        UserInfoProvider.SetUserInfo(user);
                    }
                    else if (taskType == TaskTypeEnum.UpdateObject)
                    {
                        EventLogProvider.LogInformation("Connector", "CreateUser", "User updated on SAP !!!!!");
                        // Call SAP webservice
                    }
                }
            }
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("Connector", "CreateUser", ex);
            errorMessage = ex.Message;
            return IntegrationProcessResultEnum.Error;
        }
        errorMessage = null;
        return IntegrationProcessResultEnum.OK;
    }
}

Here is a dump of the values of parameters I get when I debug on a CreateObject event:

enter image description here

I have 2 issues.

I've checked this post: Kentico 12 DancingGoat MVC SiteName is empty or null

Adding "localhost" in the domain alias of the site didn't work.

Thank you by advance !


Solution

  • With the comment of Enn I understood that my problem came from this instruction "UserInfoProvider.SetUserInfo(user);" I subscribed to apply a logic on any new User objects and update it again in the logic, that's why I it was executed more than once.

    To solve it, I applied the proposition of Michal

    using (CMSActionContext context = new CMSActionContext())
    {
        context.LogWebFarmTasks = false;
        context.LogEvents = false;
        context.LogExport = false;
        context.LogIntegration = false;
        context.LogSynchronization = false;
    
        UserInfo user = infoObj.MainObject as UserInfo;
        user.SetValue("SAPID", Guid.NewGuid());
        UserInfoProvider.SetUserInfo(user);
    }
    

    Thank you !