Published on, Time to read
🕒 2 min read

Optimizely / Episerver: How to fix System.ArgumentException: Unable to load type (IPlugin type)

Optimizely / Episerver: How to fix System.ArgumentException: Unable to load type (IPlugin type)

Introduction

Recently, I have encountered the following error: System.ArgumentException: Unable to load type (IPlugin type). This was happening in two actions:

  • On saving contact via code: CustomerContact.SaveChanges(), or,
  • Saving contact in the Commerce UI.

The difficulty was that the error was not very descriptive and didn't occur on each save operation.

The UI was saying:

Unable to load type ContactUpdatedPlugin

While the code has the following error:

System.ArgumentException: Unable to load type Foo.Bar.ContactUpdatedPlugin

With the call stack:

System.ArgumentException: Unable to load type Foo.Bar.ContactUpdatedPlugin
   at Mediachase.BusinessFoundation.Data.AssemblyUtil.LoadObject(String type, String interfaceName)
   at Mediachase.BusinessFoundation.Data.AssemblyUtil.LoadObject(String type, Type interfaceType)
   at Mediachase.BusinessFoundation.Data.AssemblyUtil.LoadObject[T](String type)
   at System.Linq.Enumerable.SelectArrayIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.<>c__DisplayClass18_0.<FindPluginsByRequest>b__0(String name)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.FindPluginsByRequest(Request request, EventPipeLineStage eventStage)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.ExecutePlugins(EventPipeLineStage eventPipeLineStage)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Execute(Request request)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Update(EntityObject target)
   at Mediachase.Commerce.Customers.CustomerContact.SaveChanges()
   at Foo.Bar.CustomContact.SaveChanges()

Solution

The error happened because the plugin's namespace was changed, and the plugin's registration had a fixed definition of the plugin namespace.

So, to fix the error, we may update the plugin's registration. However, the better way is to add the typeof to the plugin registration in the way like:

services.Configure<BusinessManagerOptions>(options =>
{
    options.Plugins.Add(new RequestPlugin
    {
        MetaClass = "Contact",
        Method = "Update",
        EventStage = EventPipeLineStage.PostMainOperation,
        TypeName = $"{typeof(ContactUpdatedPlugin).FullName}, Foobar"
    });
});

External links