CRM 2011:插件 - 无法加载文件或程序集“Microsoft.Xrm.Client”

时间:2013-01-22 10:44:21

标签: c# dynamics-crm-2011

我希望建立一个相当简单的插件来计算使用的“单位”数量。剩余。 我有两个自定义的帖子bc_learninglicences& bc_llbalance,插件会在创建bc_llbalance时触发,另一个用于更新。

bc_llbalance: Contains
    bc_learninglicense (Look up field on bc_learninglicences / bc_name)
    bc_units (units that are used by this record)

bc_learninglicences: Contains 
    bc_name
    bc_unitsquantity (This is set to the total qty of units)
    bc_unitsused (this needs to inherit the sum of "bc_units" on "bc_llbalance")
    bc_unitsremaining ( simply bc_unitsquantity - bc_unitsused )

好的,所以我已经包含了显然没有完成的代码,因为我一直试图弄清楚如何让bc_unitsused继承总和... 此代码构建没有错误。但CRM错误:

  

插件出现意外异常(Execute):LearningLicenses.LearningLicenses:System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.Xrm.Client,Version = 5.0.9690.2165

我有一个很短的截止日期来构建这个,因为系统即将上线,但我不能继续前进这个我不明白的错误......

此时,对于错误或代码/解决方案的帮助将会受到赞赏,因为我在这个项目的时间框架上落后了。

p.s我是开发CRM 2011的新手,只有几周/几个项目经验。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.Text.RegularExpressions;
using System.ServiceModel;

namespace LearningLicenses
{
    public class LearningLicenses : IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {
            try
            {

                // Obtain the execution context from the service provider.
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService =
                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                if (context.InputParameters.Contains("Target") &&
                        context.InputParameters["Target"] is Entity)
                 {

                     // Obtain the target entity from the input parmameters.
                     Entity entity = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                EntityReference a = (EntityReference)entity.Attributes["bc_learninglicense"];// ((EntityReference)targetEntity.Attributes["bc_learninglicense"]).Id;


                if (entity.LogicalName == "bc_llbalance")
                        try
                        {  

                            //fetchxml to get the sum total of estimatedvalue
                            string value_sum = string.Format(@"         
                    <fetch distinct='false' mapping='logical' aggregate='true'> 
                        <entity name='bc_llbalance'>
                            <attribute name='bc_units' alias='units_sum' aggregate='sum' />
                               <filter type='and'>
                                <condition attribute='bc_learninglicense' operator='eq' value='{0}' uiname='' 
                               </filter>
                        </entity>
                    </fetch>", a.Id);

                            FetchExpression fetch = new FetchExpression(value_sum);
                            EntityCollection value_sum_result = service.RetrieveMultiple(fetch);
                            var TotalValue = "";
                            // decimal TotalValue = 0;
                            foreach (var c in value_sum_result.Entities)
                            {
                                TotalValue = ((string)((AliasedValue)c["value_sum"]).Value);
                            }

                            Entity llc = new Entity("bc_learninglicences");
                            llc.Id = a.Id;
                            llc.Attributes.Add("bc_unitsused", TotalValue);

                            service.Update(llc);

                        }
                        catch (FaultException ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                        }
                }

            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
        }
    }
 }



THE ERROR : 

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): LearningLicenses.LearningLicenses: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Xrm.Client, Version=5.0.9690.2165, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220956</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Unexpected exception from plug-in (Execute): LearningLicenses.LearningLicenses: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Xrm.Client, Version=5.0.9690.2165, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.</Message>
  <Timestamp>2013-01-22T10:09:22.0275105Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>

[LearningLicenses: LearningLicenses.LearningLicenses]
[c1b35170-c563-e211-8c6d-b499bafd5e5b: LearningLicenses.LearningLicenses: Create of bc_llbalance]


</TraceText>
</OrganizationServiceFault>

2 个答案:

答案 0 :(得分:1)

CRM不使用Microsoft.Xrm.Client,因此不存在于GAC或CRM服务器上的任何位置,它旨在与需要它的任何客户端应用程序一起重新分发。

根本不需要在插件程序集中引用它,所以我会将其删除并查看是否有任何代码引用它。如果是这样,它应该能够被替换为不使用该程序集的等效代码。

答案 1 :(得分:0)

有两种方法可以解决这个问题,首先您可能需要检查SDK是否在GAC中可用,但我们在内部使用ILMerge来引入插件所需的所有依赖项。