Close

Troubleshooting Tip - Service cannot be started. System.TypeLoadException: Could not load type 'type name' from assembly

Recently I ran into this bizarre issue while developing a windows service, and thought it would be great to share the remedy on interwebs to save others some time and pain.

The problem usually starts when you have a windows service project, with possibly other associated class library projects as part of the solution. Everything worked fine, your unit tests still run, and your console tester (recommended to have with a windows service for stepping thru / debugging) also still works. However, your service stops working. Install util works ok, however upon net start, when the service is started it immediately stops.  The Event Log says following.

 

Service cannot be started. System.TypeLoadException: Could not load type '' from assembly '', Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at nova.edu.MyService.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

You check for diffs, run it thru tests, console app etc, and wonder what could possibly be wrong. Its because of namespaces.

Somewhere in your solution, you have a class, outside of your service project, which refers to the same namespace as the service namespace.

So how do you resolve this issue?

Step 1. Locate the assembly where there is a "duplicate" reference; so for instance your type library; see the matching namespace and be careful NOT to do replace all via search and replace.

Service cannot be started. System.TypeLoadException Could not load type 'type name' from assembly -1

Step 2. Rename the namespace appropriately.

Service cannot be started. System.TypeLoadException Could not load type 'type name' from assembly -2

 

Tada! Service cannot be started. System.TypeLoadException: Could not load type '' from assembly should be gone.

Share