GetService gives null for my DTE request
I'm using following code within my Initialize function on my package.
DTE dte = GetService(typeof(DTE))
But dte is allways null. What's wrong with my code?
Also i should note that my package is loaded at startup that is I've used [ProvideAutoLoad(UIContextGuids.NoSolution)] attribute on my package
[330 byte] By [
FatihBoy] at [2007-12-20]
The reason for not being able to retrieve the DTE at this point is that the shell is not fully loaded - it has zombie state. The good thing is that you can get notified when the shell has finished loading. I have added sample code that demonstrates how to set an event listener for shell property changes. In short, you should implement IVsShellPropertyChanges and look for changes on __VSSPROPID.VSSPROPID_Zombie . Once you have retrieved the DTE you can stop listening to the property changes as shown below.
Thanks,
Ole
Sample c# code
public
class MyNestedPackage : ProjectPackage, IVsShellPropertyEvents {
DTE dte; uint cookie; protected override void Initialize() {
base.Initialize(); //Set an eventlistener for shell property changes since we ant to know when the zombie state changes from true to false IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell; if (shellService != null) ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this,out cookie)); //Continue initialize ... this.RegisterProjectFactory(new MyNestedProjectFactory(this)); }
#region
IVsShellPropertyEvents Members public int OnShellPropertyChange(int propid, object var) {
// If zombie state changes from true to false we can go ahead and // ask for the DTE service and then stop listening for property changes if ((int)__VSSPROPID.VSSPROPID_Zombie == propid) {
if ((bool)var == false) {
this.dte = GetService(typeof(DTE)) as DTE; IVsShell shellService = GetService(typeof(SVsShell)) as IVsShell; if (shellService != null) ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(this.cookie)); this.cookie = 0; }
}
return VSConstants.S_OK; }
#endregion
}