方法x尝试访问方法y失败

时间:2018-10-31 08:52:31

标签: c# dynamic dynamics-crm-2011 crm methodaccessexception

我已经基于其他代码编写了一个代码。 它是Dynamic CRM 2011.的插件 无论如何,我想用现有的Plugin替换此插件。 它几乎是另一个.dll Plugin的镜像。 无论如何,在CRM的自定义中,我已禁用了前Plugin的所有步骤,并希望通过插件注册来插入新的插件。 我通过VS构建类库,VS在项目的/bin/Debug文件夹中创建一个.dll文件。 我选择插入.dll作为注册新程序集。 我选择了Sandbox作为隔离模式。 然后将数据库作为位置(不是DiskGAC)。 它成功更新了我选择的插件。 之后,插入我的步骤(就像我想替换为以前的插件一样)。 但是在一种创建方法中,我遇到了这个错误:

    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): OrderReceiptStepPlugin: System.MethodAccessException:
         Attempt by method 'Microsoft.Xrm.Sdk.Entity.ToEntity<System.__Canon>()' to access method 'StepType..ctor()' failed.Detail: 
        <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" 
        xmlns="schemas.microsoft.com/.../Contracts">
          <ErrorCode>-2147220956</ErrorCode>
          <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
          <Message>Unexpected exception from plug-in (Execute): OrderReceiptStepPlugin: System.MethodAccessException: 
        Attempt by method 'Microsoft.Xrm.Sdk.Entity.ToEntity&lt;System.__Canon&gt;()' to access method 'StepType..ctor()' failed.</Message>
          <Timestamp>xxxxxxxxxx.4628797Z</Timestamp>
          <InnerFault i:nil="true" />
          <TraceText>

[OrderReceiptStep: OrderReceiptStepPlugin]
[xxxxxx-xxxx-xxxx11-xxx-xxxxxxxxxxxx: OrderReceiptStepPlugin: Create of bmsd_orderreceiptstep]

Entered .Execute(), Correlation Id: 47c5ddcb-9290-4945-9ac0-fd88925480b1, Initiating User: 5b6b27be-ca0f-e811-bebe-000c2964dd77
 is firing for Entity: bmsd_orderreceiptstep, Message: Create, Correlation Id: 47c5ddcb-9290-4945-9ac0-fd88925480b1, Initiating User: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
Exiting .Execute(), Correlation Id: xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, Initiating User: xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx


</TraceText>
</OrganizationServiceFault>

我搜索了MethodAccessException,发现此方法是由于.Net版本4和更高版本引起的, 根据该线程,您可以在Assebmly.cs中添加一些代码来避免此异常。 但就我而言,没有成功。 我将以下代码添加到我的assembly.cs中,但没有成功:

    [assembly: SecurityTransparent()]
[assembly: AllowPartiallyTrustedCallersAttribute]

这是导致错误的代码,位于自定义实体的预先创建中:

  private void OrderReceiptStepPreCreateEventHandler(LocalPluginContext context)
{
    SalesOrderReceiptStepRepository salesOrderReceiptStepRepository = new SalesOrderReceiptStepRepository(context);
    SalesOrderReceiptStep salesOrderReceiptStep = salesOrderReceiptStepRepository.CreateInstance();
    salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value + salesOrderReceiptStep.FreightAmount.Value);
    salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value - salesOrderReceiptStep.DiscountAmount.Value);
    switch (salesOrderReceiptStep.ReceiptStepType)
    {
        case ReceiptStepType.TimeOriented:
        case ReceiptStepType.PrePayment:
        case ReceiptStepType.Others:
            salesOrderReceiptStep.TotalAmountWithoutTax = new Money(salesOrderReceiptStep.TotalDetailAmount.Value + salesOrderReceiptStep.FreightAmount.Value - salesOrderReceiptStep.DiscountAmount.Value);
            salesOrderReceiptStep.TotalAmount = new Money(salesOrderReceiptStep.TotalAmountWithoutTax.Value + salesOrderReceiptStep.TotalTax.Value);
            break;
    }
    OrderReceiptStepCalculationPlugin orderReceiptStepCalculationPlugin = new OrderReceiptStepCalculationPlugin();
    orderReceiptStepCalculationPlugin.CreateORderReceiptStep(context, salesOrderReceiptStep);
    if (salesOrderReceiptStep.Contains("bmsd_totallineitemamount") && salesOrderReceiptStep.Contains("bmsd_orderid"))
    {
        EntityReference attributeValue = salesOrderReceiptStep.GetAttributeValue<EntityReference>("bmsd_orderid");
        if (attributeValue != null)
        {
            SalesOrderAdapter salesOrderAdapter = new SalesOrderAdapter();
            OrganizationServiceContext context2 = new OrganizationServiceContext(context.OrganizationService);
            Entity ordersById = salesOrderAdapter.GetOrdersById(context2, attributeValue.Id);
            if (ordersById != null)
            {
                CalculationHelper calculationHelper = new CalculationHelper();
                Entity entity = calculationHelper.CalculateOrdersAndReturnEntity(context, ordersById, ordersById);
                entity.Attributes["statuscode"] = null;
                entity.Attributes["statecode"] = null;
                context.OrganizationService.Update(entity);
            }
        }
    }
    if (salesOrderReceiptStep.Contains("bmsd_invoiceid") || salesOrderReceiptStep.Contains("statuscode") || salesOrderReceiptStep.Contains("statecode") || salesOrderReceiptStep.Contains("bmsd_totallineitemamount"))
    {
        EntityReference attributeValue2 = salesOrderReceiptStep.GetAttributeValue<EntityReference>("bmsd_invoiceid");
        if (attributeValue2 != null)
        {
            OrganizationServiceContext context2 = new OrganizationServiceContext(context.OrganizationService);
            OrderReceiptItemAdapter orderReceiptItemAdapter = new OrderReceiptItemAdapter();
            Entity invoiceById = orderReceiptItemAdapter.GetInvoiceById(context2, attributeValue2.Id);
            if (invoiceById != null)
            {
                CalculationHelper calculationHelper = new CalculationHelper();
                Entity entity2 = calculationHelper.CalculateInvoiceAndReturnEntity(context, invoiceById, invoiceById);
                entity2.Attributes["statuscode"] = null;
                entity2.Attributes["statecode"] = null;
                context.OrganizationService.Update(entity2);
            }
        }
    }
}

我知道这不是开发中的语法或逻辑错误(因为镜像正常工作)。 还有哪些其他原因可以导致此错误? 这也是Exception抛出的类,它还包含Execute方法:

    public void Execute(IServiceProvider serviceProvider)
{
    if (serviceProvider == null)
    {
        throw new ArgumentNullException("serviceProvider");
    }
    LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);
    localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", new object[1]
    {
        ChildClassName
    }));
    try
    {
        Action<LocalPluginContext> action = (from a in RegisteredEvents
                                             where a.Item1 == localcontext.PluginExecutionContext.Stage && a.Item2 == localcontext.PluginExecutionContext.MessageName &&
                                             (string.IsNullOrWhiteSpace(a.Item3) || a.Item3 == localcontext.PluginExecutionContext.PrimaryEntityName)
                                             select a.Item4).FirstOrDefault();
        if (action != null)
        {
            localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "{0} is firing for Entity: {1}, Message: {2}", new object[3]
            {
                ChildClassName,
                localcontext.PluginExecutionContext.PrimaryEntityName,
                localcontext.PluginExecutionContext.MessageName
            }));
            action(localcontext);
        }
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", new object[1]
        {
            ex.ToString()
        }));
        throw;
    }
    finally
    {
        localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", new object[1]
        {
            ChildClassName
        }));
    }
}

0 个答案:

没有答案