Пример #1
0
        /// <summary>
        /// Updates the web.config as necessary to permit the generated domain service to be found.
        /// </summary>
        private void UpdateConfiguration()
        {
            // There is no work to do unless the user has selected some context
            // and chosen to expose the DomainService via [EnableClientAccess] or
            // to expose an OData endpoint.
            if (!this._isClientAccessEnabled && !this._isODataEndpointEnabled)
            {
                return;
            }

            // Get active project.  Throws if not available.
            Project project = this.ActiveProject;

            // Get hierarchy.  Throws if not available.
            IVsHierarchy vsHierarchy = this.GetVsHierarchy(project);

            IVsApplicationConfigurationManager cfgMgr = this.GetService(typeof(IVsApplicationConfigurationManager)) as IVsApplicationConfigurationManager;

            if (cfgMgr == null)
            {
                this.TerminateWizard(Resources.BusinessLogicClass_Error_No_ConfigurationManager);
            }

            // Return the current application's configuration file by using
            // the IVsApplicationConfiguration APIs. Make sure that the
            // instance that is returned is disposed of correctly in order
            // to clean up any event hooks or docdatas.
            // Note that this interface is aware of source control and text buffers, so it
            // works even if the file is currently open and modified.
            using (IVsApplicationConfiguration appCfg = cfgMgr.GetApplicationConfiguration(vsHierarchy, Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT))
            {
                // Do not do anything unless the file already exists, else we will create an empty one
                if (appCfg != null && appCfg.FileExists())
                {
                    System.Configuration.Configuration cfg = appCfg.LoadConfiguration();
                    if (cfg != null)
                    {
                        WebConfigUtil webConfigUtil = new WebConfigUtil(cfg);

                        // First check whether any work needs to done
                        bool addHttpModule                  = webConfigUtil.DoWeNeedToAddHttpModule();
                        bool addModuleToWebServer           = webConfigUtil.DoWeNeedToAddModuleToWebServer();
                        bool setAspNetCompatiblity          = !webConfigUtil.IsAspNetCompatibilityEnabled();
                        bool setMultipleSiteBindingsEnabled = !webConfigUtil.IsMultipleSiteBindingsEnabled();
                        bool addValidationSection           = webConfigUtil.DoWeNeedToValidateIntegratedModeToWebServer();
                        bool addODataEndpoint               = this._isODataEndpointEnabled && !webConfigUtil.IsEndpointDeclared(BusinessLogicClassConstants.ODataEndpointName);

                        // Modify the file only if we decided work is required
                        if (addHttpModule || addModuleToWebServer || setAspNetCompatiblity || setMultipleSiteBindingsEnabled || addValidationSection || addODataEndpoint)
                        {
                            string domainServiceFactoryName = WebConfigUtil.GetDomainServiceModuleTypeName();

                            // Check the file out from Source Code Control if it exists.
                            appCfg.QueryEditConfiguration();

                            if (addHttpModule)
                            {
                                webConfigUtil.AddHttpModule(domainServiceFactoryName);
                            }

                            if (addModuleToWebServer)
                            {
                                webConfigUtil.AddModuleToWebServer(domainServiceFactoryName);
                            }

                            if (setAspNetCompatiblity)
                            {
                                webConfigUtil.SetAspNetCompatibilityEnabled(true);
                            }

                            if (setMultipleSiteBindingsEnabled)
                            {
                                webConfigUtil.SetMultipleSiteBindingsEnabled(true);
                            }

                            if (addValidationSection)
                            {
                                webConfigUtil.AddValidateIntegratedModeToWebServer();
                            }

                            if (addODataEndpoint)
                            {
                                string odataEndpointFactoryName = WebConfigUtil.GetODataEndpointFactoryTypeName();
                                webConfigUtil.AddEndpointDeclaration(BusinessLogicClassConstants.ODataEndpointName, odataEndpointFactoryName);
                            }
                            cfg.Save();
                        }
                    }
                }
            }
        }
        public void RunFinished()
        {
            IOleServiceProvider oleServiceProvider    = this.Dte2 as IOleServiceProvider;
            IVsApplicationConfigurationManager cfgMgr = null;
            IVsHierarchy vsHierarchy = null;

            using (ServiceProvider sp = new ServiceProvider(oleServiceProvider))
            {
                // Get the solution
                IVsSolution sln = sp.GetService(typeof(IVsSolution)) as IVsSolution;
                if (sln == null)
                {
                    return;
                }

                cfgMgr = sp.GetService(typeof(IVsApplicationConfigurationManager)) as IVsApplicationConfigurationManager;
                if (cfgMgr == null)
                {
                    return;
                }

                int result = sln.GetProjectOfUniqueName(this._project.UniqueName, out vsHierarchy);
                if (result != 0 || vsHierarchy == null)
                {
                    return;
                }
            }

            // Return the current application's configuration file by using
            // the IVsApplicationConfiguration APIs. Make sure that the
            // instance that is returned is disposed of correctly in order
            // to clean up any event hooks or docdatas.
            // Note that this interface is aware of source control and text buffers, so it
            // works even if the file is currently open and modified.
            using (IVsApplicationConfiguration appCfg = cfgMgr.GetApplicationConfiguration(vsHierarchy, Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT))
            {
                // Do not do anything unless the file already exists, else we will create an empty one
                if (appCfg != null && appCfg.FileExists())
                {
                    System.Configuration.Configuration cfg = appCfg.LoadConfiguration();
                    if (cfg != null)
                    {
                        WebConfigUtil webConfigUtil = new WebConfigUtil(cfg);

                        // First check whether any work needs to done
                        bool addHttpModule                  = webConfigUtil.DoWeNeedToAddHttpModule();
                        bool addModuleToWebServer           = webConfigUtil.DoWeNeedToAddModuleToWebServer();
                        bool setAspNetCompatiblity          = !webConfigUtil.IsAspNetCompatibilityEnabled();
                        bool setMultipleSiteBindingsEnabled = !webConfigUtil.IsMultipleSiteBindingsEnabled();
                        bool addValidationSection           = webConfigUtil.DoWeNeedToValidateIntegratedModeToWebServer();

                        // Modify the file only if we decided work is required
                        if (addHttpModule || addModuleToWebServer || setAspNetCompatiblity || setMultipleSiteBindingsEnabled || addValidationSection)
                        {
                            string domainServiceModuleName = WebConfigUtil.GetDomainServiceModuleTypeName();

                            // Check the file out from Source Code Control if it exists.
                            appCfg.QueryEditConfiguration();

                            if (addHttpModule)
                            {
                                webConfigUtil.AddHttpModule(domainServiceModuleName);
                            }

                            if (addModuleToWebServer)
                            {
                                webConfigUtil.AddModuleToWebServer(domainServiceModuleName);
                            }

                            if (setAspNetCompatiblity)
                            {
                                webConfigUtil.SetAspNetCompatibilityEnabled(true);
                            }

                            if (setMultipleSiteBindingsEnabled)
                            {
                                webConfigUtil.SetMultipleSiteBindingsEnabled(true);
                            }

                            if (addValidationSection)
                            {
                                webConfigUtil.AddValidateIntegratedModeToWebServer();
                            }

                            cfg.Save();
                        }
                    }
                }
            }
        }