如何在 WF4 工作流服务的自定义活动设计器中读取 Web.Config 文件

发布于 2024-09-01 00:50:34 字数 509 浏览 4 评论 0原文

我有一个带有自定义活动和自定义设计器(WPF)的 WF 服务。我想添加一个验证来检查 web.config 文件中是否存在某些值。

在运行时我可以重载 void CacheMetadata(ActivityMetadata 元数据)< /a> 因此我可以使用 System 愉快地进行验证.Configuration.ConfigurationManager 读取配置文件。

由于我也想在设计时执行此操作,因此我一直在寻找一种在设计器中执行此操作的方法。

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

At runtime I can overload void CacheMetadata(ActivityMetadata metadata) and thus I can do the validation happily there 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-09-08 00:50:34

好的,我有一个解决方案:

    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;
}

注意:别忘了这里可能需要一船 try catch

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: Don't forget you'll probably need a boat load of try catches in here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文