Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Igor 3 posts 73 karma points
    1 week ago
    Igor
    0

    Nullreference exception IComponent IMacroRenderer

    Hello i have a problem..

    my code

    Initialize TaskExecuting

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class SubscribeComposer : IUserComposer {
        public void Compose(Composition composition) {
          composition.Components().Append<TaskExecutingComponent>();
        }
    }
    

    TaskExecuting class

    public class TaskExecutingComponent : IComponent
    {
      ...
        public TaskExecutingComponent(IProfilingLogger logger, IMacroRenderer macroRenderer, IUmbracoDatabaseService dataBaseService, IScopeProvider scopeProvider, UmbracoMapper umbracoMapper, SubscribeConfiguration config, IMacroService macroService, IPublishedSnapshotAccessor publishedSnapshotAccessor) {
           ...
        }
    
        public void Initialize() {
            int delayBeforeWeStart = 60000;
            int howOftenWeRepeat = 60000; 
    
            var task = new Task(_sharePriceRunnerRunner,
                delayBeforeWeStart,
                howOftenWeRepeat,
                _logger,
                _dataBaseService,
                _scopeProvider,
                _macroRenderer,
                _umbracoMapper,
                _config,
                _macroService,
                _publishedSnapshotAccessor);
    
            _sharePriceRunnerRunner.TryAdd(task);
        }
    
        public void Terminate()
        {
        }
    }
    

    And with problem moment

    public class Task : RecurringTaskBase {
        ...
    
        public Task(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayBeforeWeStart, int howOftenWeRepeat, IProfilingLogger logger, IUmbracoDatabaseService dataBaseService, IScopeProvider scopeProvider, IMacroRenderer macroRenderer, UmbracoMapper umbracoMapper, SubscribeConfiguration config, IMacroService macroService, IPublishedSnapshotAccessor publishedSnapshotAccessor)
            : base(runner, delayBeforeWeStart, howOftenWeRepeat) {
            _...
        }
    
        public override bool PerformRun() {
                var PageId = 999; // This page is present
            var macro = _macroService.GetById(task.MacrosId);
            var macro_params = _umbracoMapper.Map<IEnumerable<MacroParameter>>(macro);
            var maxro_param_val = new Dictionary<string, object>();
    
        maxro_param_val.Add("getFrom", DateTime.Now.Date());
    
            var content_val = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(PageId);
            var macro_props = maxro_param_val.ToDictionary(
                x => x.Key,
                i => i.Value is string ? HttpUtility.HtmlDecode(i.Value.ToString()) : i.Value);
    
            var result = _macroRenderer.Render(macro.Alias, content_val, macro_props).GetAsText();
    
            return true;
        }
    

    And Exception

    System.NullReferenceException: 
    in _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(_config.SubscribeMessage.SubscribePageId.Value);
    

    if i use other methods for recive Content i have problem with content in _macroRenderer.Render

  • Igor 3 posts 73 karma points
    5 days ago
    Igor
    0

    I solved my problem, now I have a new problem)

    Solved how render macro in background task

    first im use

    (...,IUmbracoContextFactory umbracoContextFactory)
    

    when redid standard methods

    using (var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext()) {
    var content = umbracoContextReference.UmbracoContext.Content.GetById(999);
    using (var controller = new MyPartialViewMacroController(macro_model, content)) {
        var route_vals = new RouteData();
        route_vals.Values.Add("controller", "PartialViewMacro");
        route_vals.Values.Add("action", "Index");
        //routeVals.DataTokens.Add(Core.UmbracoContextDataToken, umbCtx); //required for UmbracoViewPage
        if (umbracoContextReference.UmbracoContext.HttpContext == null)
            throw new NullReferenceException("HttpContext is null");
        var request = new RequestContext(umbracoContextReference.UmbracoContext.HttpContext, route_vals);
        //controller.ViewData = viewContext.ViewData;
        controller.ControllerContext = new ControllerContext(request, controller);
    
        //call the action to render
        result = controller.RenderViewResultAsString(controller.Index());
    }
    

    }

    Ok and incapsulate umbraco class PartialViewMacroController and rename to MyPartialViewMacroController

    ))

    Ok now i have problem in cshtml....

    var startnode = Umbraco.Content(startnode_id);

    System.NullReferenceException: 
    LightInject.Web.PerWebRequestScopeManager.GetOrAddScope()
    DynamicMethod(Object[] )
    Umbraco.Core.FactoryExtensions.GetInstance[T](IFactory factory)
    Umbraco.Web.Runtime.WebInitialComposer.<>c.<C ompose>b__0_6(IFactory factory)
    Umbraco.Core.FactoryExtensions.GetInstance[T](IFactory factory)
    Umbraco.Web.Mvc.UmbracoViewPage`1.get_Umbraco()
    
  • Igor 3 posts 73 karma points
    5 days ago
    Igor
    0

    Ok i solved all my probmels ....

    in schtml need use it for

    UmbracoContext.Content.GetById(...);
    

    not

    Umbraco.Content(...);
    
Please Sign in or register to post replies

Write your reply to:

Draft