Example #1
0
        /// <summary>
        /// Initializes the VSPackage with a back pointer to the environment. This is the entry point
        /// for the Visual Studio package.
        /// </summary>
        /// <param name="sp">
        /// Pointer to the <see cref="IOleServiceProvider"/> interface through which the
        /// VSPackage can query for services.
        /// </param>
        /// <returns>An HRESULT indicating the result of the call.</returns>
        int IVsPackage.SetSite(IOleServiceProvider sp)
        {
            if (this.Closed)
            {
                Tracer.Fail("We shouldn't have been called if we're being unloaded.");
                return(NativeMethods.E_UNEXPECTED);
            }

            try
            {
                if (sp != null)
                {
                    // If SetSite has been called more than once, it's an error.
                    if (this.Context != null)
                    {
                        string message = this.Context.NativeResources.GetString(ResId.IDS_E_SITEALREADYSET, this.GetType().FullName);
                        Tracer.Fail(message);
                        throw new InvalidOperationException(message);
                    }

                    // Initialize the ServiceProvider and ourself.
                    ServiceProvider contextServiceProvider = new ServiceProvider(sp);
                    this.context = this.CreatePackageContext(contextServiceProvider);
                    Tracer.Initialize(this.context);
                    this.Initialize();
                }
                else if (this.Context != null && this.Context.ServiceProvider != null)
                {
                    this.Dispose(true);
                }
            }
            catch (Exception e)
            {
                Tracer.Fail("Unexpected exception: {0}\n{1}", e.Message, e);
                throw;
            }

            return(NativeMethods.S_OK);
        }