Skip to content

Instantly share code, notes, and snippets.

@msusur
Last active May 13, 2020 10:34
Show Gist options
  • Save msusur/e34be94cbceac20ea364 to your computer and use it in GitHub Desktop.
Save msusur/e34be94cbceac20ea364 to your computer and use it in GitHub Desktop.
Dynamics CRM Dependency Injection Workaround
public abstract class SuperPlugin : IPlugin{
private static Lazy<IDependencyResolver> DependencyResolverTrunk = new Lazy<IDependencyResolver>(ResolverFactory);
public void Execute(IServiceProvider serviceProvider){
// initialize a static container instance if not available
var containerWrapper = new ContainerWrapper{
Container = serviceProvider.GetService(typeof(IPluginExecutionContext)),
Resolver = DependencyResolverTrunk.Value
};
OnExecution(containerWrapper);
}
public abstract void OnExecution(IDependencyResolver resolver);
private static IDependencyResolver ResolverFactory(){
var resolver = new Resolver();
Setup(resolver);
return resolver;
}
private static void Setup(IDependencyResolver resolver){
resolver.Register<IService, Service>();
}
}
public class MyPlugin : SuperPlugin, IPlugin{ //you need to specify the IPlugin here as well because of a bug on Plugin Registration Tool.
public abstract void OnExecution(IDependencyResolver resolver){};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment