繁体   English   中英

插件CRM动态2015

[英]plugin crm dynamics 2015

我正在尝试创建一个简单的插件并将其注册到我的 MSDN CRM - 解决方案。 我有 Microsoft CRM SDK 和 Microsoft 解决方案管理器来帮助我解决这个问题。

当我创建一个项目时,CRM 解决方案管理器为此提供了一个基本插件,但缺少任何逻辑 (ofc)。

在下面的基本插件执行方法中,我应该将逻辑放在何处? 如果可能的话?

 /// <summary>
    /// Executes the plug-in.
    /// </summary>
    /// <param name="serviceProvider">The service provider.</param>
    /// <remarks>
    /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. 
    /// The plug-in's Execute method should be written to be stateless as the constructor 
    /// is not called for every invocation of the plug-in. Also, multiple system threads 
    /// could execute the plug-in at the same time. All per invocation state information 
    /// is stored in the context. This means that you should not use global variables in plug-ins.
    /// </remarks>
    public void Execute(IServiceProvider serviceProvider)
    {
        if (serviceProvider == null)
        {
            throw new ArgumentNullException("serviceProvider");
        }

        // Construct the Local plug-in context.
        LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);

        localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", this.ChildClassName));

        try
        {
            // Iterate over all of the expected registered events to ensure that the plugin
            // has been invoked by an expected event
            // For any given plug-in event at an instance in time, we would expect at most 1 result to match.
            Action<LocalPluginContext> entityAction =
                (from a in this.RegisteredEvents
                 where (
                 a.Item1 == localcontext.PluginExecutionContext.Stage &&
                 a.Item2 == localcontext.PluginExecutionContext.MessageName &&
                 (string.IsNullOrWhiteSpace(a.Item3) ? true : a.Item3 == localcontext.PluginExecutionContext.PrimaryEntityName)
                 )
                 select a.Item4).FirstOrDefault();

            if (entityAction != null)
            {
                localcontext.Trace(string.Format(
                    CultureInfo.InvariantCulture,
                    "{0} is firing for Entity: {1}, Message: {2}",
                    this.ChildClassName,
                    localcontext.PluginExecutionContext.PrimaryEntityName,
                    localcontext.PluginExecutionContext.MessageName));

                entityAction.Invoke(localcontext);

                // now exit - if the derived plug-in has incorrectly registered overlapping event registrations,
                // guard against multiple executions.
                return;
            }
        }
        catch (FaultException<OrganizationServiceFault> e)
        {
            localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));

            // Handle the exception.
            throw;
        }
        finally
        {
            localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
        }
    }

您可以将代码剥离为仅此:

public void Execute(IServiceProvider serviceProvider)
{
    LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);

    localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", this.ChildClassName));

    try
    {

    }
    catch (FaultException<OrganizationServiceFault> e)
    {
        localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));

        // Handle the exception.
        throw;
    }
    finally
    {
        localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
    }
}

并将您的代码添加到try块。

IServiceProvider为您提供了您需要的IPluginExecutionContextIOrganizationServiceFactory实例。 获取您的插件正在执行的上下文和 b。 访问组织服务界面。

暂无
暂无

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

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