无法从App.Config中提取设置-返回值始终为null [英] Can't Pull settings from App.Config - Return is always null
问题描述
大家好,
我想以此作为开头,我已经在互联网上搜索了数小时的解决方案. Azure Cloud Configuration Manager可以为我提取连接字符串,但是我不能提取标准应用程序设置.返回的值始终为".
I want to preface this with, I have searched the internet for hours for a solution. Azure Cloud Configuration Manager is able to pull connection strings for me, however I can not pull standard app settings. The value returned is always "".
这是应用程序配置文件:
Here is the app config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=YOURNAME;AccountKey=YOURKEY"></add>
<add key="TwitterConsumerKey" value="YOURCONSUMERKEY"></add>
<add key="TwitterConsumerSecret" value="YOURCONSUMERSECRET"></add>
<add key="TwitterAccessToken" value="YOURACCESSTOKEN"></add>
<add key="TwitterAccessSecret" value="YOURACCESSSECRET"></add>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
这是代码:
module OAuthProvider
open System.Configuration
open System.Security.Cryptography
open System
open System.Text
open System.Net.Http
//sample requestUri: "https://stream.twitter.com/1.1/statuses/filter.json"
//sample requestType: "POST"
let GetOAuthResponse (requestUri: string) (requestType: string) =
//Get OAuth Tokens
let token = ConfigurationManager.AppSettings.Item("TwitterAccessToken")
let tokenSecret = ConfigurationManager.AppSettings.Item("TwitterAccessSecret")
let consumer = ConfigurationManager.AppSettings.Item("TwitterConsumerKey")
let consumerSecret = ConfigurationManager.AppSettings.Item("TwitterConsumerSecret")
这只是函数的开始,它会继续构建流,您现在可以返回0,或演示一个设置值.
That's just the beginning of the function, it goes on to build a stream, you can return 0 for now, or a setting value to demonstrate.
我正在使用Visual Studio 2013 Community Edition和F#控制台应用程序.
I am using Visual Studio 2013 Community Edition and a F# console application.
非常感谢您的帮助.
谢谢
〜大卫
推荐答案
我只是整理了一个普通的F#控制台应用程序(在VS2010中,用于.net 4,但带有4.5.2  ;已安装);根据您的示例:-
I just put together a vanilla F# console application (in VS2010, for .net 4, but with 4.5.2 installed) based on your sample: --
open System.Configuration
let token = ConfigurationManager.AppSettings.["TwitterAccessToken"] // more idiomatic
let tokenSecret = ConfigurationManager.AppSettings.Item("TwitterAccessSecret")
let consumer = ConfigurationManager.AppSettings.Item("TwitterConsumerKey")
let consumerSecret = ConfigurationManager.AppSettings.Item("TwitterConsumerSecret")
[ token; tokenSecret; consumer; consumerSecret ] |> List.iter (printfn "%A")
ConfigurationManager.AppSettings
|> (printfn "%A")
printfn "%A" <| ConfigurationManager.AppSettings.Item("Extra")
printfn "Done"
并获得输出
"YOURACCESSTOKEN"
"YOURACCESSSECRET"
"YOURCONSUMERKEY"
"YOURCONSUMERSECRET"
seq
["StorageConnectionString"; "TwitterConsumerKey"; "TwitterConsumerSecret";
"TwitterAccessToken"; ...]
<null>
</null>
我要检查的第一件事是验证.exe.config与.exe在同一位置.
The first thing I'd check is to verify that the .exe.config is co-located with the .exe.
这篇关于无法从App.Config中提取设置-返回值始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!