configSource 绝对路径解决方法

发布于 2024-11-02 11:24:24 字数 362 浏览 0 评论 0原文

我有一个 WCF 数据服务,其中包含一些在 page_load 事件期间异步调用的方法。

如果需要,我需要一个解决方案来从脚本或控制台应用程序调用这些方法。

我创建了一个引用 WCF .dll 库的控制台应用程序。我必须将 WCF 服务下的 web.config 中的一些配置变量复制到控制台应用程序下的 app.config 中。

我希望 app.config 自动镜像 web.config 或以某种方式将控制台应用程序指向 WCF 服务 web.config。

我的控制台应用程序和 wcf 项目在同一解决方案中彼此相邻,因此“configSource”属性不起作用。不允许有父目录或绝对路径。

有谁知道这个问题的解决方法?

I have a WCF data service with some methods I call asynchronously during page_load events.

I need a solution to call these methods from either a script or console app if necessary.

I created a console app that references the WCF .dll libraries. I have to duplicate some of the configuration variables that are in web.config under the WCF service into an app.config under the console app.

I want the app.config to mirror the web.config automatically or somehow point the console app at the WCF services web.config.

My console app and wcf projects are adjacent to eachother in the same solution, so the 'configSource' attribute wont work. No parent directories or absolute paths are allowed.

Does anyone know of a work-around for this?

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

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

发布评论

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

评论(1

作业与我同在 2024-11-09 11:24:24

好的,我不知道您的自定义配置部分是什么,但此类将向您展示如何以编程方式检索配置部分(本例中为 system.serviceModel/client)、应用程序设置和连接字符串。您应该能够修改它以满足您的需要。

public class ConfigManager
{
    private static readonly ClientSection _clientSection = null;
    private static readonly AppSettingsSection _appSettingSection = null;
    private static readonly ConnectionStringsSection _connectionStringSection = null;
    private const string CONFIG_PATH = @"..\..\..\The rest of your path\web.config";

    static ConfigManager()
    {
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()
        {
            ExeConfigFilename = CONFIG_PATH
        };

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

        _clientSection = config.GetSection("system.serviceModel/client") as ClientSection;
        _appSettingSection = config.AppSettings;
        _connectionStringSection = config.ConnectionStrings;
    }

    public string GetClientEndpointConfigurationName(Type t)
    {
        string contractName = t.FullName;
        string name = null;

        foreach (ChannelEndpointElement c in _clientSection.Endpoints)
        {
            if (string.Compare(c.Contract, contractName, true) == 0)
            {
                name = c.Name;
                break;
            }
        }

        return name;
    }

    public string GetAppSetting(string key)
    {
        return _appSettingSection.Settings[key].Value;
    }

    public string GetConnectionString(string name)
    {
        return _connectionStringSection.ConnectionStrings[name].ConnectionString;
    }
}

用法:

ConfigManager mgr = new ConfigManager();

string setting = mgr.GetAppSetting("AppSettingKey");
string connectionString = mgr.GetConnectionString("ConnectionStringName");
string endpointConfigName = mgr.GetClientEndpointConfigurationName(typeof(IServiceContract));

Ok, I don't know what your custom config section is, but this class will show you how to retrieve config sections (system.serviceModel/client in this example), app settings and connection strings programmatically. You should be able to modify it to fit your needs.

public class ConfigManager
{
    private static readonly ClientSection _clientSection = null;
    private static readonly AppSettingsSection _appSettingSection = null;
    private static readonly ConnectionStringsSection _connectionStringSection = null;
    private const string CONFIG_PATH = @"..\..\..\The rest of your path\web.config";

    static ConfigManager()
    {
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()
        {
            ExeConfigFilename = CONFIG_PATH
        };

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

        _clientSection = config.GetSection("system.serviceModel/client") as ClientSection;
        _appSettingSection = config.AppSettings;
        _connectionStringSection = config.ConnectionStrings;
    }

    public string GetClientEndpointConfigurationName(Type t)
    {
        string contractName = t.FullName;
        string name = null;

        foreach (ChannelEndpointElement c in _clientSection.Endpoints)
        {
            if (string.Compare(c.Contract, contractName, true) == 0)
            {
                name = c.Name;
                break;
            }
        }

        return name;
    }

    public string GetAppSetting(string key)
    {
        return _appSettingSection.Settings[key].Value;
    }

    public string GetConnectionString(string name)
    {
        return _connectionStringSection.ConnectionStrings[name].ConnectionString;
    }
}

Usage:

ConfigManager mgr = new ConfigManager();

string setting = mgr.GetAppSetting("AppSettingKey");
string connectionString = mgr.GetConnectionString("ConnectionStringName");
string endpointConfigName = mgr.GetClientEndpointConfigurationName(typeof(IServiceContract));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文