Class CrmAgentContextExtensions
Layers configuration extension methods onto IAgentContext appropriate to the Microsoft Dynamics CRM 2011 environment.
See EnsureConfigured(IAgentContext, Func<IAgentCommenceConfigurationPhase, IAgentCompletedConfigurationPhase>) for a usage example.
Namespace: Sp.Agent.Configuration
Assembly: Sp.Agent.Crm.dll
Methods
EnsureConfigured(IAgentContext, Func<IAgentCommenceConfigurationPhase, IAgentCompletedConfigurationPhase>)
Enables one to provide a Configuration Expression specifying the details appropriate to the execution and/or licensing of your Microsoft Dynamics CRM 2011 Component.
Declaration
public static void EnsureConfigured(this IAgentContext that, Func<IAgentCommenceConfigurationPhase, IAgentCompletedConfigurationPhase> compose)
Parameters
Type | Name | Description |
---|---|---|
IAgentContext | that | IAgentContext to be configured. See For(String) for details of obtaining an instance. |
System.Func<IAgentCommenceConfigurationPhase, IAgentCompletedConfigurationPhase> | compose | IAgentCommenceConfigurationPhase for details of composing a Configuration Expression appropriate to your application style/environment using the fluent expression composition sequence. |
Remarks
This method can be called any number of times; the implementation ensures that the underlying .Configure()
method only gets triggered once.
Examples
// NB an Assembly Reference to Sp.Agent.Crm.dll is required for this to work
using Sp.Agent.Configuration; // AgentContext.For(string), IAgentContext.EnsureConfigured
using Sp.Agent; // IProductContext
using System; // IServiceProvider
class SpAgent
{
static readonly IAgentContext _agentContext = AgentContext.For( "abc12" );
static IProductContext _productContext;
// Maintains a copy of the Dynamics CRM Service Context for the current processing thread.
// NB For this to work correctly, it is important that Sp.Agent Apis are only called from the same processing thread that invokes SpAgent.Initialize
[ThreadStatic]
static IServiceProvider _currentServiceProvider;
// Provides access to Product-level information from the Software Potential Agent
// Should not be accessed without first calling SpAgent.Initialize on the calling thread.
public static IProductContext Product
{
get
{
if ( _productContext == null )
_productContext = _agentContext.ProductContextFor( "My Plugin", "1.0" );
return _productContext;
}
}
// Should be called from your Plugin.cs IPlugin.Execute implementation, i.e., as follows:-
//
// public void Execute( IServiceProvider serviceProvider )
// {
// SpAgent.Initialize(serviceProvider);
// // ... rest of method ....
public static void Initialize( IServiceProvider serviceProvider )
{
// Update thread-relative
_currentServiceProvider = serviceProvider;
// Applies the configuration (if it has not already been applied for this AppDomain).
// NB the _currentServiceProvider reference below is triggered on the fly as the Sp.Agent components require it. See comment beside the field above.
_agentContext.EnsureConfigured( x => x
.WithCrmOnlineOrganizationStore( () => _currentServiceProvider, "new" )
.CompleteWithDefaults()
);
}
// Triggers an Online Activation from the Software Potential service of the license with the specified activationKey identifier.
// Should not be accessed without first calling SpAgent.Initialize on the calling thread.
public static void Activate( string activationKey )
{
Product.Activation.OnlineActivate( activationKey );
}
}