相关疑难解决方法(0)

使用ConfigurationManager从任意位置加载配置

我正在开发一个数据访问组件,该组件将用于包含经典ASP和ASP.NET页面混合的网站,并且需要一种管理其配置设置的好方法.

我想使用自定义ConfigurationSection,对于ASP.NET页面,这非常有用.但是当从传统的ASP页面通过COM interop调用组件时,组件不会在ASP.NET请求的上下文中运行,因此不了解web.config.

有没有办法告诉ConfigurationManager只需从任意路径加载配置(例如,..\web.config如果我的程序集在/bin文件夹中)?如果有,那么我认为我的组件可以回退到如果默认ConfigurationManager.GetSection返回null我的自定义部分.

任何其他方法都欢迎!

asp.net configuration asp-classic

121
推荐指数
5
解决办法
10万
查看次数

将app.config文件重定位到自定义路径

是否可以将整个App.Config文件重定位到自定义路径?

配置文件与exe文件位于同一文件夹中似乎有点奇怪,Windows的新approcah将所有程序设置保存在c:\ ProgramData和all中.

我们的另一个要求是以编程方式指定在哪里找到app.config文件.这样做的原因是我们从同一个exes生成不同的服务实例,并希望将每个服务的app.config存储在c:\ ProgramData \\下该服务的settings文件夹中.

.net c# app-config path

27
推荐指数
4
解决办法
5万
查看次数

PowerShell App.Config

有没有人研究过如何让PowerShell使用app.config文件?我有一些.NET DLL我想在我的一个脚本中使用,但是他们希望自己的配置部分存在于app.config/中web.config.

powershell configuration-files

26
推荐指数
2
解决办法
2万
查看次数

App.config和F#Interactive无法正常工作

当我正在抛弃我的小宠物项目时,我正在尝试将所有常量字符串存储在我的app.config文件中(Keys,XpathExpressions等).当我运行编译的exe时,这很有效.在Interactive Shell中并非如此.

我试图将.config文件从我的bin/Release目录复制到obj/Debug&obj/Release目录,但Call to ConfigurationManager.AppSettings.Item("key")always总是返回null.

有任何建议如何解决这个问题?

最诚挚的问候

.net f#

16
推荐指数
3
解决办法
5093
查看次数

如何从HostType("Moles")测试中读取UnitTest项目的App.Config

我有以下测试:

[TestClass]
public class GeneralTest
{
    [TestMethod]
    public void VerifyAppDomainHasConfigurationSettings()
    {
        string value = ConfigurationManager.AppSettings["TestValue"];
        Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
    }

    [TestMethod]
    [HostType("Moles")]
    public void VerifyAppDomainHasConfigurationSettingsMoles()
    {
        string value = ConfigurationManager.AppSettings["TestValue"];
        Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
    }
}
Run Code Online (Sandbox Code Playgroud)

它们之间的唯一区别是[HostType("Moles")].但第一次传球,第二次传球失败.如何从第二次测试中读取App.config?

或者我可以在其他地方添加另一个配置文件?

c# unit-testing mstest moles

12
推荐指数
4
解决办法
1万
查看次数

使用nunit重新加载app.config

我有多个NUnit测试,我希望每个测试都使用特定的app.config文件.有没有办法在每次测试之前将配置重置为新的配置文件?

.net c# nunit app-config

11
推荐指数
1
解决办法
5735
查看次数

使用CurrentDomain.SetData("APP_CONFIG_FILE")在PowerShell ISE中不起作用

我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig);    
Run Code Online (Sandbox Code Playgroud)

[Configuration.ConfigurationManager] :: ConnectionStrings.Count总是返回"1",
"[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name"总是返回"LocalSqlServer",而ConnectionString名称不在我的".config"中文件.

请注意,从PowerShell命令提示符执行PowerShell脚本将按预期运行.就在我从PowerShell ISE中执行它时,它无法按预期工作.

.net-4.0 powershell-ise windows-8 powershell-3.0

9
推荐指数
1
解决办法
5911
查看次数

如何告诉应用程序从我的自定义 app.config 文件而不是默认文件中读取 <runtime>

假设我正在创建一个名为ConsoleApp2的应用程序。

由于我使用了一些第三方库,我的默认 app.config 文件正在生成类似的代码

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)

那是因为我的解决方案引用了一个库的不同版本,所以它需要告诉大家:“嘿,如果你要找这个库的任何旧版本,就用新版本”。没关系。

问题是我想定义一个单独的配置文件“test.exe.config”,在那里我有一些设置并摆脱自动生成的设置。

为了告诉我的应用程序有关新配置文件的信息,我使用了如下代码

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "test.exe.config");
Run Code Online (Sandbox Code Playgroud)

这(几乎)完美地工作。我在那里写了“几乎”,因为虽然该<appSettings>部分被正确读取,但该<runtime>部分并未在我的自定义配置文件中查看,但应用程序在默认配置文件中查找它,这是一个问题,因为我想以后可以删除那个。

那么,我如何告诉我的应用程序也<runtime>从我的自定义配置文件中读取信息?


如何重现问题

重现我的问题的简单示例如下:

创建一个名为ClassLibrary2 ( .Net Framework v4.6 )的库,包含一个类,如下所示

using Newtonsoft.Json.Linq;
using System;

namespace ClassLibrary2
{
    public class Class1
    {
        public Class1()
        {
            var json = new JObject();
            json.Add("Succeed?", true);

            Reash = json.ToString();
        }

        public String …
Run Code Online (Sandbox Code Playgroud)

.net c# app-config

9
推荐指数
2
解决办法
2047
查看次数

如何在带有配置文件的Powershell脚本中使用自定义WCF代理?

我在它自己的程序集中有一个手写的WCF代理,它非常简单:

public class MyServiceClient : ClientBase<IMyService>, IMyService
{
    public MyServiceClient()
    {
    }

    public MyServiceClient(string endpointConfigurationName) :
        base(endpointConfigurationName)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我将其加载到Powershell脚本中:

Add-Type -Path "$LocalPath\MyService.Client.dll"
Add-Type -Path "$LocalPath\MyService.Contracts.dll"
Run Code Online (Sandbox Code Playgroud)

然后我尝试设置App.config(根据SO上的其他帖子),以便客户端可以使用在config中定义的Endpoint进行实例化,而不是在脚本本身中:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$LocalPath\MyService.Client.dll.config")
Run Code Online (Sandbox Code Playgroud)

我检查了AppDomain,并将配置文件设置为其ConfigurationFile属性.

当我创建客户端的实例时:

$endpointName = "MyServiceHttpEndpoint" # defined in the app.config file
$myclient = New-Object MyService.Client.MyServiceClient($endpointName)
Run Code Online (Sandbox Code Playgroud)

它贬低说:

Exception calling ".ctor" with "1" argument(s): "Could not find endpoint element with name 'MyServiceHttpEndpoint' and contract 'MyService.Contracts.IMyService' in the ServiceModel client configuration section. This might be because no configuration file …

.net c# powershell wcf configuration-files

8
推荐指数
1
解决办法
2696
查看次数

使用fsx文件中的app.config

是否可以使用F#脚本(fsx)文件中的app.config文件?如果是这样,怎么样?我在哪里放置文件以及它将被称为什么?

configuration-files f#-scripting

6
推荐指数
1
解决办法
1052
查看次数