6

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.

  • Is the assembly containing your SPDiagnosticsServiceBase deployed to the GAC? – Steve P Jun 2 '11 at 18:20
2

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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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