Skip to content

IServiceProvider Extensions

Artem Grunin edited this page Dec 24, 2024 · 2 revisions

IServiceProvider Extensions

Set of extension methods for Microsoft.Xrm.Sdk.IServiceProvider base class. Just shortcut methods to save you few lines of code during plugin development.

GetPluginExecutionContext

Gets IPluginExecutionContext from service provider.

public IPluginExecutionContext GetPluginExecutionContext();

GetOrganizationServiceFactory

Gets IOrganizationServiceFactory from service provider.

public IOrganizationServiceFactory GetOrganizationServiceFactory();

GetTracingService

Gets ITracingService extension from service provider.

public ITracingService GetTracingService();

Introduced in version 2.0.0

GetPluginExecutionTraceContext

Gets PluginExecutionTraceContext from service provider.

public PluginExecutionTraceContext GetPluginExecutionTraceContext(PluginExecutionTraceContextSettings settings = null)

PluginExecutionTraceContext is a disposable wrapper around IPluginExecutionContext and ITracingService that simplifies tracing of plugin execution context properties in memory efficient way. Usage:

using (var traceContext = serviceProvider.GetPluginExecutionTraceContext()
{
    traceContext.Trace("test message");

    //by default we will trace only if error occurs
    throw new InvalidPluginExecutionException();
}

Plugin trace log output will be similar to the following:

MessageName: "Create"
Stage: 0
PrimaryEntityId: "{GUID}"
PrimaryEntityName: "{entityName}"
serId: "{GUID}"
InitiatingUserId: "{GUID}"
Depth: 1
Mode: 0
test message

Real output will also include Input and Output parameters, Pre and Post Images and Shared Variables. You can also customize the behavior by passing PluginExecutionTraceContextSettings into GetPluginExecutionTraceContext