Pregunta

I have two VSPackages. The first one provides a global service. Both VSPackages consume the service.

The service is defined as explained in MSDN "How To: Register a Service". I left out the ComVisibleAttribute, because the how-to says it's only required if the service needs to be available in unmanaged code, which it doesn't. The interfaces are like

[Guid("5A72348D-617B-4960-B07A-DC6CC5AA7675")]
public interface SMessageBus {}

[Guid("04A499BA-CE09-48AF-96D5-F32DEAF0754C")]
public interface IMessageBus { ... }

The service-providing package follows MSDN "How To: Provide a Service". It looks like:

[<package atttributes>]
[ProvideService(typeof(SMessageBus))]
public sealed class MessageBusProviderPackage : Package
{
  public MessageBusProviderPackage()
  {
    var serviceContainer = this as IServiceContainer;
    var serviceCreatorCallback = new ServiceCreatorCallback(CreateMessageBusService);
    serviceContainer.AddService(typeof(SMessageBus), serviceCreatorCallback, true);
  }

  private object CreateMessageBusService(IServiceContainer container, Type serviceType)
  {
    // this gets called and returns a new bus instance
    return (serviceType == typeof(SMessageBus)) ? new MyMessageBus() : null;
  }

  protected override void Initialize()
  {
    // this is called after the package was constructed
    // the call leads to the service being created by CreateMessageService()
    var messageBus = GetService(typeof(SMessageBus)) as IMessageBus;
    // the bus is retrieved correctly
    ...
  }
}

This other package is declared like

[<package attributes>]
[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]
public sealed class MessageGeneratorPackage : Package
{
  protected override void Initialize()
  {
    // the call below is reached first, in its course the provider is loaded
    var service = GetService(type(SMessageBus));
    // this point is reached last, but service is null
    ...
  }
}

I debugged through the startup phase and found that the MessageGeneratorPackage gets created and initialized first. This means that the package was sited. When the GetService() call in Initialize() is reached, VS loads my service provider, i.e., the ProvideServiceAttribute correctly marks the MessageBusProviderPackage as provider of the SMessageBus service. The provider package gets instantiated and its Initialize() method gets called, wherein the service is retrieved successfully. Afterwards the initialization of the consumer package continues, but the service request returns null. It seems to me that all requirements stated in MSDN "How To: Troubleshoot Services" are fulfilled. Can anyone tell me what I am missing?

¿Fue útil?

Solución

Found the answer myself... the overrides of Initialize() need to call base.Initialize() since that is were registered services actually get promoted to the parent service containers.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top