Hello,
we have an issue since a long time that as soon as we run a Workflow in Bulk we get following error:
Exception type: Microsoft.Crm.CrmException
Message: ValidateClosed - Db GetCreateConnection() should be closed on End
bei Microsoft.Crm.Workflow.Services.UpdateActivityService.Execute(ActivityContext executionContext, UpdateEntity updateEntity)
bei System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
bei System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
-- End stack trace --
Sometimes also:
The operation or property SqlTransaction is not valid in the context without calling OnBeginRequest first Line:0
And
InvalidOperationException The connection was not closed. The connection's current state is open
This happens for every single entity we have, but only if you run a workflow for multiple entries at once.
If i do it step by step it works every single time.
After spending some time on Searching the internet for this issue i found out that this issue appears if you use global variables inside Plugins.
But the strange issue is we don't do that.
Here is a example of Post Plugin for account entity:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Globalization;
using CrmSvcUtil;
using System.Diagnostics;
namespace PostAccountUpdate
{
/// <inheritdoc />
/// <summary>
/// Base class for all plug-in classes.
/// </summary>
public class PostAccountUpdate : IPlugin
{
/// <summary>
/// Plug-in context object.
/// </summary>
protected class LocalPluginContext
{
/// <summary>
/// Helper object that stores the services available in this plug-in.
/// </summary>
/// <param name="serviceProvider"></param>
internal LocalPluginContext(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
}
/// <summary>
/// Writes a trace message to the CRM trace log.
/// </summary>
/// <param name="message">Message name to trace.</param>
internal void Trace(string message, ITracingService TracingService, IPluginExecutionContext PluginExecutionContext)
{
if (string.IsNullOrWhiteSpace(message) || TracingService == null)
{
return;
}
if (PluginExecutionContext == null)
{
TracingService.Trace(message);
}
else
{
TracingService.Trace(
"{0}, Correlation Id: {1}, Initiating User: {2}",
message,
PluginExecutionContext.CorrelationId,
PluginExecutionContext.InitiatingUserId);
}
}
}
/// <inheritdoc />
/// <summary>
/// Main entry point for he business logic that the plug-in is to execute.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "CrmVSSolution411.NewProj.PluginBase+LocalPluginContext.Trace(System.String)", Justification = "Execute")]
public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
// Construct the local plug-in context.
LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
string relationshipName = String.Empty;
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Log.CRMService = service;
try
{
........
Does anyone have an Idea how to fix this?
Thanks for help