I have write a code base on other code.
It is a plugin for Dynamic CRM 2011.
Anyways, I wanna replace this plugin with existing Plugin
.
It is almost a mirror of another .dll Plugin
.
Anyways, In customization of CRM, I have disabled All steps of previous Plugin
and want to insert new Plugin via Plugin Registration.
I build my class library via VS, VS makes a .dll file in /bin/Debug
folder of project.
I select insert that .dll as Register new assembly.
I chose Sandbox
as Isolation mode.
And then database as location ( Not Disk
or GAC
).
It successfully updated my selected Plugin.
After that, I insert my steps( Just like the previous Plugin I wanna replace with ).
But in one the create methods, I get this 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): 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<System.__Canon>()' 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>
I googled about MethodAccessException Find out that this method occurs because of .Net version 4 and later, And some code you can add in your Assebmly.cs to avoid this exception, according to this thread. But in my case no success. I added the code below to my assembly.cs but no success :
[assembly: SecurityTransparent()]
[assembly: AllowPartiallyTrustedCallersAttribute]
Here is the code that cause the error, It is in pre-Create of custom entity :
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);
}
}
}
}
I know this is not a syntax or logical error in development ( Cause the mirror is working correctly). What other reasons can make this error? Also here is the Class that Exception throw from that, It also contains the Execute medthod :
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
}));
}
}