Kristoffer Lindén
Dec 13, 2021
(1 votes)

How to simplify initialization for your custom plugin

When your are creating a custom plugin you might want to initialize your plugin on startup. You could of course add everything you need in the Optimizely Startup.cs class but is is much nicer to have the user just adding one row of code using an extension method.

Your extension could look something like this:

public static class ServiceCollectionExtensions
    public static IServiceCollection AddMyPlugin(this IServiceCollection services, string connectionString)
        services.AddDbContext<MyDbContext>(x => x.UseSqlServer(connectionString))
            .AddScoped<Microsoft.EntityFrameworkCore.DbContext, MyDbContext>()
            .AddSingleton<IMyService1, MyService>()
            .AddSingleton<IMyService2, MyService2>();      

        return services;

And the only thing you need to add in Startup.cs is this:


I had som static content that generated 404 so I also had to create an extension to handle static content.

public static IApplicationBuilder UseMyPluginStaticContent(this IApplicationBuilder app, IWebHostEnvironment env)
    app.UseStaticFiles(new StaticFileOptions
        FileProvider = new PhysicalFileProvider(
            Path.Combine(env.ContentRootPath, "modules", "MyPlugin", "ClientResources")),
        RequestPath = "/modules/MyPlugin/ClientResources"

    return app;

and the add this row in the Startup.cs Configure method:


This is an easy way to create advanced initialization but the user only needs to add two rows of code that clearly points out that they are use by My Plugin.

Dec 13, 2021


Please login to comment.
Latest blogs
Optimizely shortcuts in CMS 12 will get a trailing slash appended!

Not all URLs will work when the trailing slash is added, and that could cause problems. Hopefully it will be fixed soon.

Tomas Hensrud Gulla | Nov 26, 2022 | Syndicated blog

One week left of the beta certification period

Today it's one week until the last beta certification day. Do you exam no later than Wednesday the 30th.  Here are the reference and exam guides:...

Karen McDougall | Nov 23, 2022

Unbelievable FREE Heatmapping With Optimizely Web

Within this guide, you will learn how to integrate a free heat mapping tool to Optimizely Web so you can turbocharge your A/B testing capabilities....

Jon Jones | Nov 22, 2022 | Syndicated blog

Import Your Localized Resources in v7.5

Import functionality has been delayed for several versions of DbLocalizationProvider. However, with the help of community member - this feature has...

valdis | Nov 21, 2022 | Syndicated blog

Optimizely CMS running on .NET 7!

Can you run Optimizely CMS on .NET 7? Yes! Can you upgrade in less than a day? Probably!

Tomas Hensrud Gulla | Nov 18, 2022 | Syndicated blog

Take experimentation velocity to the next level with Mutually Exclusive Experiments in Edge

The need for Mutually Exclusive Experiments in Edge When they visit our customers’ sites, Edge users are likely to see more than one related...

Thanh-Vi Le | Nov 15, 2022