How to read Web.Config file in Custom Custom Designer in WF4 Workflow Service

I have a WF service with custom action and custom designer (WPF). I want to add a check that will check for some value in the web.config file.

At runtime, I can overload the void CacheMetadata (ActivityMetadata metadata) and this way I can successfully validate using System.Configuration.ConfigurationManager to read the config file.

Since I also want to do this at design time, I was looking for a way to do this in the designer.

+2


a source to share


1 answer


Ok I have one solution:

    string GetWebConfigXml() {

        string configXml = null;

        Window window = null;
        ProjectItem project = null;
        ProjectItem configFile = null;

        try {
            EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE)) as DTE;
            if(dte == null) return null;

            project = dte.Solution.FindProjectItem(dte.ActiveDocument.FullName);
            configFile = (from ProjectItem childItem in project.ProjectItems
                            where childItem.Name.Equals("web.config", StringComparison.OrdinalIgnoreCase)
                            select childItem).FirstOrDefault();

            if (configFile == null) return null;

            if (!configFile.IsOpen) window = configFile.Open();
            var selection = (TextSelection)configFile.Document.Selection;
            selection.SelectAll();
            configXml = selection.Text;
        } finally {

            //Clean up the COM stuff

            if (window != null) {
                window.Close(vsSaveChanges.vsSaveChangesNo);
                window = null;
            }

            if (configFile != null) { 
                configFile = null; 
            }

            if (project != null) {
                project = null;
            }
        }
    }
    return configXml;
}

      



Note: Do not forget that you will probably need a boat to load test catches here

+3


a source







All Articles