Custom service and using it in ContentHandler causes error

Topics: Writing modules
Oct 15, 2012 at 5:52 PM
Edited Oct 15, 2012 at 5:54 PM

I have a custom Service that requires the IOrchardServices interface as shown below: 

 

public interface IAppController :IDependency
    {
  
    }
 public class AppController :IAppController
    {
	   public AppController(IOrchardServices orchardServices)
		{
		_orchardServices = orchardServices;
		InitAppController();
		}
		
		 private void InitAppController()
                {
          
                   var obj = _orchardServices.WorkContext.CurrentSite    

		}
		
	}

 

 

If i try to use this custom service by injecting it in a custom Handler (as shown below) an error is caused  inside the  InitAppController method of the AppController class saying that the WorkContext instanse of _orchardServices.WorkContext is null.

 

 public class WorkItemIDHandler : ContentHandler {
           private readonly IAppController _appController;

           public WorkItemIDHandler(IRepository<WorkItemIDPartRecord> workItemHeaderRepository, IAppController appController) 
            {

              _appController = appController;
               Filters.Add(StorageFilter.For(workItemHeaderRepository));
               OnLoaded<WorkItemIDPart>(LoadDataFromDb);

            }
		
	}

 

Is this a bug or am i doing something wrong?

Coordinator
Oct 15, 2012 at 8:17 PM

if it's the work context that you really need, why inject IOrchardServices and not IWorkContextAccessor?

Oct 15, 2012 at 8:19 PM

bertrand ,

i tried it but got the same result.

Coordinator
Oct 15, 2012 at 8:28 PM

I can't see the actual code using the dependency, you haven't copied it. The constructor code is not going to tell us much.

Oct 15, 2012 at 8:54 PM
Edited Oct 15, 2012 at 9:03 PM

here is a more detailed description : 

when i run the site in visual studio the first error occurs in the :

 

public class AppController :IAppController
    {
	   public AppController(IOrchardServices orchardServices)
		{
		_orchardServices = orchardServices;
		InitAppController();
		}
		private void InitAppController()
        {
          
            var cnnstr = _orchardServices.WorkContext.CurrentSite.As<IntelliSettingsPart>().MasterCnnString; 
			//an error occurs here (WorkContext is null) where i have defined a custom site setting to hold a connection string to an external databse

		}
		
	}
The next error occurs in the contenthandler :
public class
 public class WorkItemIDHandler : ContentHandler {
           private readonly IAppController _appController;

           public WorkItemIDHandler(IRepository workItemHeaderRepository, IAppController appController) 
            {

              _appController = appController;
               Filters.Add(StorageFilter.For(workItemHeaderRepository));
               OnLoaded(LoadDataFromDb);

            }
		
	}

  void LoadDataFromDb(LoadContentContext context, WorkItemIDPart part)
           {
               if (part.WorkItemID!=null) {
                   var cntitem = context.ContentItem;
                   //get data from database
                   //populate part properties
                   var item = GetWorkItemFromDb(part.WorkItemID);


               }

           }

  public WorkItem GetWorkItemFromDb(int? pkid)
           {
             if (pkid != null)
               {
                   using (new TransactionScope(TransactionScopeOption.Suppress))
                   {

                           var cnnstr = _appController.ConnectionString; //The second error occurs here (_appController is null)
                           using (var db = new DataEntities(startup, cnnstr))
                           {

                              //get data
                           
                       }
                   }
              }

               return null;

           }
I cant get it why it is calling the code in content handler when site starts even though the home page has none of these content items in it.
Coordinator
Oct 15, 2012 at 9:11 PM

Ah, well, never do any significant work in the constructor. That call to init must be done lazily, later, as needed.

Oct 15, 2012 at 10:22 PM
Edited Oct 15, 2012 at 10:23 PM

Ok, 

I've removed the ''InitAppController'' call from the AppController constructor and now calling it on demand on an property called "ConnectionString" so 

i can call  : AppController .ConnectionString (inside this property i am calling the  InitAppController method)

 

Its still strange because as my site is loading the WorkItemIDHandler constructor is being called , calling the   ''LoadDataFromDb'' method  even though my home page has no reference to any of these content items.

Even more stranger is the fact that its called for every content item item i have in the database when the site is loading. and by the way when it gets to the 

var cnnstr = _appController.ConnectionString; method

The WorkContext is still null inside the AppController

Coordinator
Oct 15, 2012 at 11:20 PM

You have built a content handler so of course your code is being called for every content item that ever gets loaded (such as the site content item). That's what a handler does.

Without knowing what you are trying to do exactly, I'm having a hard time understanding what you're expecting to happen.

Oct 15, 2012 at 11:53 PM

Ok, figured it out. 

There were some ''ghost'' records hanging around with my custom parts records in the database, somehow triggering the content handler events.

Wiping out these leftover records resolved the issue.

Too sleepy to figure out why they were leftover.

Thanks bertrand for your help.

Yiannis