Tag: 配置

为什么StringValidator总是因自定义配置部分而失败?

我通过inheritanceConfigurationSection在ac#类库中创建了一个自定义配置节。 我在我的Web应用程序(也是c#,ASP.NET)中引用了类库,填写了适当的属性,一切都很好。 当我开始添加validation器时,问题就开始了。 例如,这个属性: [ConfigurationProperty(“appCode”, IsRequired = true)] public string ApplicationCode { get { return (string)base[“appCode”]; } set { base[“appCode”] = value; } } 因为它工作正常,但只要我添加这个: [StringValidator(MinLength=1)] 它轰炸了以下错误: 属性“appCode”的值无效。 错误是:字符串长度必须至少为1个字符。 即使我的web.config文件中有一个有效的appCode值,我也会收到此错误。 如果我删除validation器,它可以完美地工作。 有谁知道怎么解决这个问题?

从Mac上的命令行运行ASP.NET 5应用程序时是使用launchSettings.json吗?

我正在使用Mac上的Visual Studio代码开发ASP.NET 5 Web API应用程序。 我手动修改了我的Properties/launchSettings.json文件,使用ASPNET_ENV环境变量将环境设置为Staging for all profiles: 但是,当我在Mac终端上运行dnx web来启动应用程序时,我仍然得到了Production环境: 如果我在Mac上使用Visual Studio Code,是否可以使用launchSettings.json指定环境变量(以及环境类型)? 或者它是否特定于完整的Visual Studio? ASP.NET 5文档建议 launchSettings.json可用于注入环境变量。 然而, 这个SO讨论建议通过命令传递它们。

启动Windows服务时出现TypeInitializationException,因为无法创建config部分

启动Windows服务时,我在特定的Windows Server 2008 R2计算机上遇到了一个奇怪的错误(它可以在其他2008 R2计算机上运行)。 该服务使用Common.Logging和log4net 。 但是,在此特定计算机上,无法创建Common.Logging的配置节处理程序。 它使用以下堆栈跟踪失败(格式化以获得更好的可读性)。 最令我惊讶的是SecurityException 。 是什么导致这种情况? 有人有线索吗? System.TypeInitializationException: The type initializer for ‘MyWindowsService.Program’ threw an exception. —> Common.Logging.ConfigurationException: Failed obtaining configuration for Common.Logging from configuration section ‘common/logging’. —> System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for common/logging: Request failed. (C:\Path\MyWindowsService.exe.Config line 7) —> System.Security.SecurityException: Request failed. at […]

循环通过configrationsection使用C#读取它的元素

我有一个配置文件,如: DEV http://login.dev.server.com/Logon.asmx http://login.ide.server.com/Logon.asmx abc 123 如何读取配置以获取传递的keyname的值。 这是我写的方法: private static string GetKeyValue(string keyname) { string rtnvalue = String.Empty; try { ConfigurationSectionGroup sectionGroup = config.GetSectionGroup(“logonurls”); foreach (ConfigurationSection section in sectionGroup.Sections) { //I want to loop through all the settings element of the section } } catch (Exception e) { } return rtnvalue; } config是配置变量,其中包含配置文件中的数据。

自定义web.config节处理程序

我之前设计了一个自定义部分处理程序,但我遇到了一个我似乎无法想到的问题。 我有一个像这样的配置部分: 我创建了以下处理程序类: using System.Configuration; namespace MyProject.Configuration { public class ProvidersSection : ConfigurationSection { public new Element this[string key] { get { } } } [ConfigurationCollection(typeof(ProviderElement))] public class ProvidersCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ProviderElement(); } protected override object GetElementKey(ConfigurationElement element) { return element.ElementInformation.Properties[“name”].Value; } public ProviderElement this[string key] { […]

以编程方式从IIS获取站点状态,返回COM错误

我试图以编程方式从IIS获取我的站点状态,看看它是否已停止,但我一直收到以下错误, 对象标识符不代表有效对象。 (HRESULTexception:0x800710D8) 该应用程序使用ServerManager Site类来访问站点状态。 这是代码, //This is fine, gets back the site var serverManager = new Microsoft.Web.Administration.ServerManager(ConfigPath); var site = serverManager.Sites.FirstOrDefault(x => x.Id == 5); if (site == null) return; var appPoolName = site.Applications[“/”].ApplicationPoolName; //error! var state = site.State; 我已经使用静态站点进行测试以隔离问题,确保站点已启动并运行,所有配置都有效,指向有效的应用程序池…等。 如果您需要更多详细信息,请告诉我们。 这是COM的事吗?

WCF服务基地址与端点地址

以下两种情况有什么区别: 配置1: 配置2: 我的理解是在任何一种情况下, 基地址+端点地址都解析为相同的绝对地址 但是为什么我在配置2上得到错误: “没有终点正在监听net.tcp://127.0.0.1:808 / 但配置1 运行服务没有任何错误 ! 编辑1: 工作配置: 非工作配置: <!– –> 在这种情况下,我删除了基地址并提供了完整的服务地址(带有.svc路径),但是出现套接字超时错误。 在这种情况下有什么问题? 当未定义基址时,端点地址是否始终需要.svc的完整地址? 如果是这样,背后的原因可能是什么?

AppSettings的变化需要重新启动我的应用程序如何避免?

我正在使用C#.NET 2.0 Windows应用程序。 我正在使用app.config进行应用程序设置。 但AppSettings中的更改并未反映运行时,需要重新启动应用程序。 我怎么能避免它。 这是我用来读取和编写应用程序设置的代码片段。 我正在读这样的设置 string temp = ConfigurationManager.AppSettings.Get(key); 我正在更新这样的值,其中node是当前配置/ appSettings节点 node.Attributes[“value”].Value = value; xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ASP.NET MVC 6上每个控制器的特定JSON设置

我需要在ASP.NET MVC 6 webApi中为每个控制器设置特定的JSON设置。 我发现这个样本(我希望!)适用于MVC 5: 在每个控制器的ASP.NET WebAPI上强制使用CamelCase using System; using System.Linq; using System.Web.Http.Controllers; using System.Net.Http.Formatting; using Newtonsoft.Json.Serialization; public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration { public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()} }; controllerSettings.Formatters.Add(formatter); } }

ASP.net Core 2.0中的appsettings.json预览配置GetSection null

我试图从Startup.cs注入配置调用GetSection 。 Value为null ,而具体节值的indexer返回non-null值。 在我看来, GetSection方法背后的错误或我错了吗? appsettings.json: {“MyConfig”:{“ConfigA”:“valueA”,“ConfigB”:“valueB”}} Program.cs中: public static void Main(string[] args) { var host = BuildWebHost(args); host.Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); Startup.cs: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by […]