2sxc DnnEnvironment.get_DefaultLanguage()

A few days ago we updated 2sxc from version 9.33.0 to 11.7.3. And when running ToSic.SexyContent.Environment.Dnn7.Factory.App(appId, portalSettings), in the scheduled tasks of a custom dnn module, it gives the following error.

EXCEPTIONSystem.NullReferenceException: Object reference not set to an instance of an object. at ToSic.SexyContent.Environment.DnnEnvironment.get_DefaultLanguage() in C:\Projects\2sxc-dnn742\Website\DesktopModules\ToSIC_SexyContent\2sxc Dnn\Environment\DnnEnvironment.cs:line 28 at ToSic.Eav.Persistence.Efc.Efc11Loader.LoadEntities(AppDataPackage app, Int32[] entityIds) at ToSic.Eav.Persistence.Efc.Efc11Loader.<>c__DisplayClass5_0.b__0() at ToSic.Eav.App.AppDataPackage.Load(Log parentLog, Action loader) in C:\Projects\eav-server\ToSic.Eav.Core\App\AppDataPackage.cs:line 181 at ToSic.Eav.Persistence.Efc.Efc11Loader.Update(AppDataPackage app, AppPackageLoadingSteps startAt, Int32[] entityIds, Log parentLog) at ToSic.Eav.Persistence.Efc.Efc11Loader.AppPackage(Int32 appId, Int32[] entityIds, Log parentLog) at ToSic.Eav.DataSources.Caches.BaseCache.EnsureCache() in C:\Projects\eav-server\ToSic.Eav.DataSources\Caches\BaseCache.cs:line 96 at ToSic.Eav.Apps.App..ctor(Int32 zoneId, Int32 appId, Boolean allowSideEffects, Func`2 buildConfiguration, Log parentLog, String logMsg) at ToSic.Eav.Apps.App..ctor(IAppEnvironment env, ITenant tenant, Int32 zoneId, Int32 appId, Boolean allowSideEffects, Func`2 buildConfiguration, Log parentLog) at ToSic.SexyContent.Environment.Dnn7.Factory.App(Int32 appId, PortalSettings ownerPortalSettings, Boolean versioningEnabled, Boolean showDrafts) in C:\Projects\2sxc-dnn742\Website\DesktopModules\ToSIC_SexyContent\2sxc Dnn\Environment\Dnn7\Factory.cs:line 55 

We have detected a very similar error when updating from version 9.33.0 to 9.35.

Environment:

Upvotes: 0

Views: 82

Answers (3)

aaronsglz
aaronsglz

Reputation: 16

As @iJungleBoy says, when a scheduled task is executed the system doesn't have a current PortalSettings. I understand that SxcApiController needs a DNN context and at the same time it inherits from DnnApiController. The only way to ensure that a DNN context exists is to publish an endpoint and make a request from the scheduled task. This is because when you call an endpoint from a controller that inherits from DnnApiController, DNN uses the request to create a context and a current PortalSettings.

You can do something like this from your scheduled task:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("YOUR_CONTROLLER_ENDPOINT");
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamResponse = response.GetResponseStream();

// Deserialize response
var myEndpointResponse = JsonConvert.DeserializeObject<MyResponseObject>(new StreamReader(streamResponse).ReadToEnd());

Upvotes: 0

Thank you for your response.

I have been debugging 2sxc source code and it seems that the issue appears when you run the following in Factory.cs

var appStuff = app.Init(new AppIdentity(zoneId, appId), 
                ConfigurationProvider.Build(showDrafts, publishingEnabled, new LookUpEngine(parentLog)),
                true, parentLog);

That generates the error in DnnTenant.cs in the following line.

public override string DefaultLanguage => _defaultLanguage ?? (_defaultLanguage = UnwrappedContents.DefaultLanguage.ToLowerInvariant());

The error message is as follows:

EXCEPTIONSystem.NullReferenceException: Object reference not set to an instance of an object. at ToSic.SexyContent.Environment.DnnEnvironment.get_DefaultLanguage() in C:\Projects\2sxc-dnn742\Website\DesktopModules\ToSIC_SexyContent\2sxc Dnn\Environment\DnnEnvironment.cs:line 28

I emphasize that the error seems to be solved after some time, but it reappears when the cache is cleaned and Dnn is restarted.

I suspect it has to do with the UnwrappedContents variable.

I have not found in the source code the way to debug the Eav.Factory.Resolve<App>() method. Where is the code of the Eav.Factory.Resolve<App>() method?

Upvotes: 0

iJungleBoy
iJungleBoy

Reputation: 5638

welcome to StackOverflow :)

The error you posted is almost unreadable (all on one line), so it's hard to figure out anything.

My guess is that since you're running in a schedule task, the system doesn't have a current PortalSettings, which is usually needed for things like getting the current language. These errors sometimes happen after bigger 2sxc changes and then usually pop up in the search (which is a scheduled task). But your case may be a bit more complex, so I don't know.

Without knowing the specifics, it's hard to give a tip, but it could be a bug in 2sxc not handling a fallback in this situation. I suggest you try to figure it out more in detail or get someone to help you on your site.

Upvotes: 0

Related Questions