0

I am in the middle of trying to write my first windows service using C# and .NET framework and am having trouble referencing a project within the same solution within the service. My MVC solution structure within VS2012:

  1. 1 MVC project (web app project)
  2. 1 additional processing project (used for extended processing within controllers)
  3. 1 service project
  4. 1 install shield le project (successfully installs the service project)

Within my service (which I want to fire every x mins) I am referencing methods in the extended processing project that intellisense is able to detect when I am writing code within the service project (I have a using extendedProcessingProject; using webAppProject; and a project references to the extendedProcessingProject and web app project).

The service installs to my local pc without errors. When I start the service I have an error being logged that I cannot seem to figure out:

Exception body: ReminderEmailService Object reference not set to an instance of an     object.  System.Collections.ListDictionaryInternal  
 System.NullReferenceException: Object reference not set to an instance of an object.
   at ReminderEmailService.ReminderEmailService.ReminderEmails(Object source, ElapsedEventArgs e)

   at ReminderEmailService.ReminderEmailService.SendReminderEmails(Object source, ElapsedEventArgs e)

Void SendReminderEmails(System.Object, System.Timers.ElapsedEventArgs)

I know can see though using the System.Diagnostics.Debugger.Launch();

List<SessionReminder> sessions = Queries.GetSessionsToRemind();                    
//sessions does not have a value, method returns a list of 1
//I suspect this is a project reference issue
debugString += "sessions: \n" + sessions.ToString(); 
//exception thrown on this line--I suspect because sessions is null

I am using the .NET4.0 (not client profile) on all projects within my solution. Any insight on how to enable my windows service to correctly access these assemblies (webappproject.dll and extendedProcessing.dll) would be much appreciated, as these dll's are being installed by install shield.

miniscem
  • 327
  • 1
  • 5
  • 14

2 Answers2

1

It might not be an actual assembly reference issue. The error you are seeing is that the runtime is complaining about accessing methods/fields/properties of a null object. This indicates that your code is accessing an uninitialized object.

RamblinRose
  • 172
  • 6
1

The exception you've displayed doesn't look like an issue with referencing assemblies. It looks like a plain ordinary bug in the code, not a missing assembly.

I always create a test harness for a windows service to enable me to run the service as a simple Windows Forms application to make debugging easy.

Separate your service implementation into a separate assembly from the assembly with the actual windows service. Create another project WPF/Winforms with a simple start/stop button. From both the windows service and winforms application reference the assembly containing the functionality that runs within your service.

Mick
  • 6,527
  • 4
  • 52
  • 67
  • I have placed the method the service is executing in a separate assembly; however, I am receiving the same result. Again intellisense can access all methods needed for processing within the service in the separate assembly I created. However; the `Queries.GetSessionsToRemind();` is not returning a result within the service (I have tested this method separately within a controller in my web app project, which should be equivalent to the separate WPF/Winform project you suggested and it worked as expected). Thank you again for your help on this issue. – miniscem Feb 26 '14 at 02:17
  • So u can't replicate the issue in your Web App that you're getting in your Windows Service? – Mick Feb 26 '14 at 02:29
  • I'd suggest there's something different regarding the configuration of the application or the context within it is running. I would suggest you use a windows application rather than a web application to test your code as the differences between the context of a web application and your service are going to be far greater than that between a windows service and windows form application. e.g. apps have HttpContext. You might also want to think about the security context. – Mick Feb 26 '14 at 02:35
  • Normally I create an app.config on the Windows Service. Then go to the winforms project and add that same app.config to the Windows Application using the "Add as Link" functionality in Visual Studio so that both applications use the same physical file for configuration – Mick Feb 26 '14 at 02:36
  • I have found the solution. I ended up separating my service implementation in another project ("helper" project). In this "helper" I was accessing my database via EF. In order to do this I had to add an app.config file (with the connection string to my model, found in the same project as the .edmx file). From there I was able to access my database, and avoid the original error that I was receiving as @Nate Burkett suggested. – miniscem Feb 27 '14 at 00:11
  • This question helped a lot: http://stackoverflow.com/questions/3491165/the-specified-named-connection-is-either-not-found-in-the-configuration-not-int – miniscem Feb 27 '14 at 00:11