This project is read-only.

Accessing WorkContext in Migration class returns null

Topics: Customizing Orchard
Jul 10, 2013 at 11:15 AM
Hi All,

I have some settings that I'd like to initialize in Migrations class.
I get the settings using IOrchardServices WorkContext. But there seems to be instances that WorkContext returns null. Are there any work arounds?

Below is my code in Migrations class that throws an error because WorkContext is null:
 public int UpdateFrom1()
            //init conn string
            PostalCodeServiceSettingsPart postalCodeServiceSettings = _services.WorkContext.CurrentSite.As<PostalCodeServiceSettingsPart>();
            postalCodeServiceSettings.ConnectionString = _config.ConnectionString;

            return 2;
Jul 10, 2013 at 12:58 PM
Try injecting IWorkContextAccessor and use _accessor.GetContext() to get the WorkContext instance, instead.
Jul 10, 2013 at 1:17 PM
Edited Jul 10, 2013 at 2:00 PM
Hi pszmyd,

I tried using IWorkContextAccessor but GetContext() also returns null.
Do you have recommendations on how to initialize Settings? Like in my code, I'm doing the initialization of the Settings in the Migration class. Is this wrong?

It seems like when in another thread, GetContext() is null.

Jul 11, 2013 at 10:02 AM
I think I finally solved the issue. An error occurred which triggered the UpdateFrom1 function to be called twice. The second time it's called, the context can no longer be retrieved.