Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to create a feature that Extends the SPDiagnosticServicesBase class following the code described in MSDN That article has example code for extending the SPDiagnosticServicesBase class which I am using almost verbatim.

Luckily, my solution deploys properly and I am able to see my custom categories in Central Administration Diagnostic Logging page.

However, in a second WebPart solution I am building I want to use this dll to write log statements using my extended SPDiagnosticServices class: MyDiagnosticServices

When my WebPart solution references the Local method to write a trace log like:

MyDiagnosticService.Local.LogMessage(...)

The Local() method of MyDiagnosticService class fails with the following Error

System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'MyDiagnosticService.MyDiagnosticService' to type 'MyDiagnosticService.MyDiagnosticService'.
  Source=Microsoft.SharePoint
  StackTrace:
       at Microsoft.SharePoint.Administration.SPPersistedObjectCollection`1.GetValue[U](String name)
       at Microsoft.SharePoint.Administration.SPPersistedObjectCollection`1.GetValue[U]()
       at Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocalToFarm[T](SPFarm farm)
       at Microsoft.SharePoint.Administration.SPDiagnosticsServiceBase.GetLocal[T]()
       at SJHSDiagnosticService.SJHSDiagnosticService.get_Local()
       at SJHSMinistryRedirect.SJHSMinistryRedirect.SJHSMinistryRedirectUserControl.<>c__DisplayClass4.<_eventLogWrite>b__0()
       at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
       at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
  InnerException:

Thank you for your advise on how to fix this error.

share|improve this question
    
Is the assembly containing your SPDiagnosticsServiceBase deployed to the GAC? –  Steve P Jun 2 '11 at 18:20
    
Yes indeed. Thanks for reading! –  ChiliYago Jun 2 '11 at 18:57

1 Answer 1

up vote 2 down vote accepted

These type of casting errors are usually caused by 2 different versions of the same assembly being loaded at the same time.

Run your project with the Visual Studio debugger attached and at the time this exception occurs open the "Debug/Windows/Modules" window

enter image description here

This will enable you to see exactly what assemblies are loaded at the time the error occurs.

Check to see if there is more than 1 copy of your assembly loaded.

If there are 2 versions loaded "uninstall" the incorrect one then try again.

share|improve this answer
    
Yes this was the case which I discovered the other day. Thanks for reading my post and helping. –  ChiliYago Jun 5 '11 at 14:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.