简体   繁体   English

CRM 2011-C#插件

[英]CRM 2011 - C# PLUGIN

I have a CRM 2011 plugin using C#: Synchronus, Post-Operation and Not-Sandbox. 我有一个使用C#的CRM 2011插件:同步,操作后和非沙盒。 Whenever launched, the plugin works fine for the first time. 无论何时启动,该插件都可以首次正常运行。 But my problem is when I call the plugin the second time (with other GUID), the entity will have the first calling's values. 但是我的问题是,当我第二次(使用其他GUID)调用插件时,实体将具有第一次调用的值。 So instead of having the new entity attributes that im just calling, im having the old entity's attributes (first call). 因此,与其拥有刚刚调用的新实体属性,不如拥有旧实体的属性(首次调用)。 Is there any solution to this problem? 有什么解决办法吗? Note that I have tried the Async mode and it worked fine, but I need to display error messages on the screen after calling a Webservice so I need it to be on Sync mode. 请注意,我已经尝试了异步模式,并且可以正常工作,但是在调用Web服务后需要在屏幕上显示错误消息,因此我需要使其处于同步模式。

Plugin CLass: 插件CLass:

 namespace FNBChangeCustomerInfGroup
{
public abstract class Plugin : IPlugin
{
    public IServiceProvider _serviceProvider;
    public IOrganizationServiceFactory _organizationServiceFactory;
    public IPluginExecutionContext _pluginExecutionContext;
    public IOrganizationService _organizationService;
    public ResourceManager _resourceManager;
    public Lazy<Helper> _helper;

    public IPluginExecutionContext PluginExecutionContext
    {
        get
        {
            if (_pluginExecutionContext == null)
            {
                _pluginExecutionContext = _serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
                if (_pluginExecutionContext == null)
                {
                    throw new InvalidOperationException("Cannot get Execution Context");
                }
            }
            return _pluginExecutionContext;
        }
    }

    public IOrganizationServiceFactory OrganizationServiceFactory
    {
        get
        {
            if (_organizationServiceFactory == null)
            {
                _organizationServiceFactory = _serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
                if (_organizationServiceFactory == null)
                {
                    throw new InvalidOperationException("Cannot get Service Factory");
                }
            }
            return _organizationServiceFactory;
        }
    }

    public IOrganizationService OrganizationService
    {
        get
        {
            if (_organizationService == null)
            {
                _organizationService = OrganizationServiceFactory.CreateOrganizationService(_pluginExecutionContext.UserId);
                if (_organizationService == null)
                {
                    throw new InvalidOperationException("Cannot get Organization Service");
                }
            }
            return _organizationService;
        }
    }

    protected virtual ResourceManager ResourceManager
    {
        get
        {
            if (_resourceManager == null)
            {
                _resourceManager = new ResourceManager(Constants.File_Resources, Assembly.GetExecutingAssembly());
                if (_resourceManager == null)
                {
                    throw new InvalidOperationException("Cannot access Resources file");
                }
            }
            return _resourceManager;
        }
    }

    protected Lazy<Helper> Helper
    {
        get
        {
            if (_helper == null || !_helper.IsValueCreated)
            {
                _helper = new Lazy<Helper>(() => new Helper(_organizationService));
            }
            return _helper;
        }
    }


    public IExtensibleDataObject CheckType<ExtensibleDataObjectType>(string entityLogicalName) where ExtensibleDataObjectType : IExtensibleDataObject, new()
    {
        // Check if the input parameters property bag contains a target
        // of the operation and that target is of type EntityReference.
        if (PluginExecutionContext.InputParameters.Contains(Constants.InputParameter_Target) && PluginExecutionContext.InputParameters[Constants.InputParameter_Target] is ExtensibleDataObjectType)
        {
            // Obtain the target business entity from the input parameters.
            IExtensibleDataObject entity = (PluginExecutionContext.InputParameters[Constants.InputParameter_Target]) as IExtensibleDataObject;

            // Verify that the entity represents a risk carrier quote.

            if (typeof(Entity).IsAssignableFrom(typeof(ExtensibleDataObjectType)))
            {
                if (!String.Equals(((Entity)entity).LogicalName, entityLogicalName)) { return null; }
            }
            else if (typeof(EntityReference).IsAssignableFrom(typeof(ExtensibleDataObjectType)))
            {
                if (!String.Equals(((EntityReference)entity).LogicalName, entityLogicalName)) { return null; }
            }

            return entity;
        }
        else
        {
            return null;
        }
    }

    public virtual void Execute(IServiceProvider serviceProvider)
    {
        this._serviceProvider = serviceProvider;
    }
}

} }

This is what happens when a plugin class contains fields. 当插件类包含字段时,会发生这种情况。 Plugin classes are not allowed to be stateful, because they are instantiated once by the CRM platform and then reused. 不允许插件类为有状态的,因为它们由CRM平台实例化一次,然后再使用。 Therefore fields on these classes are not thread safe. 因此,这些类上的字段不是线程安全的。

Check your CRMPluginBase class. 检查您的CRMPluginBase类。 Looking at method base.CheckType<Entity>() chances are this class has a private field holding an IPluginExecutionContext instance. 查看方法base.CheckType<Entity>()此类可能有一个包含IPluginExecutionContext实例的私有字段。 Redesign your plugin base class and make shure it does not contain instance fields at all. 重新设计您的插件基类,并确保它根本不包含实例字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM