unable to retrieve value from app.config - vb.net

Issue
I just want to be able to retrieve a value inside my App.Config file but it just does not seem to work.
The code to retrieve the value is:
Dim email As String = ConfigurationManager.AppSettings("stmpFromEmail")
My App.Config:
<configuration>
<applicationSettings>
<DataImport.DataImport.My.MySettings>
<setting name="stmpFromEmail" serializeAs="String">
<value>AgilityDataImport#agilitylogistics.com</value>
</setting>
</DataImport.DataImport.My.MySettings>
</applicationSettings>
</configuration>
I cannot seem to get the value back, help anyone?

Okay, you would have to change your app.config file to be like the below.
<configuration>
<appSettings>
<add key="stmpFromEmail" value="AgilityDataImport#agilitylogistics.com"/>
<appSettings>
</configuration>
Then you can call it like the below.
Dim email As String = ConfigurationManager.AppSettings("stmpFromEmail")

Related

Configuration Manager app settings return nothing from the app.config

I have a windows app in which there are few keys configured as follows.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="Mode" value ="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key ="IntervalMinutes" value ="1"/>
<add key ="ScheduledTime" value ="18:41"/>
<add key ="mainConnection" value ="hidden"/>
<add key ="tbl_ref_date" value ="2017-01-11 11:46:38.833"/>
<add key ="trg_ref_date" value ="2017-01-09 19:03:08.840"/>
<add key ="ScheduledTime" value ="18:41"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
</configuration>
But in my code behind I tried to access a value, but it returns nothing.
Also I tried this which returns 0
System.Configuration.ConfigurationManager.AppSettings.AllKeys.Length
Why this happening..
This is my project structure

Set default webpage in vb.Net web-project

I have a class api, and a function test in my web-project. To call this function, I type the link http://localhost/api/test in my browser. Now my question is it possible to call a default page, for example index.html, if i just call the class without the function like: http://localhost/api ?
"I doesn't use ASP.Net"
I solved the problem with a redirect with the following code in my web.config file.
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*api" destination="/index.htm" />
<add wildcard="*api/" destination="/index.htm" />
</httpRedirect>
</system.webServer>
</configuration>

Save into app.config appSettings value

I have following app.config. I would like save into ConnectionString key value - my text from serialized xml.
Private Shared strcon As String = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value=""/>
</appSettings>
<system.diagnostics>
<sources>
...
So far i got this code but i am receiving error: "Object reference not set to an instance of an object."
on the line: config.AppSettings.Settings("ConnectionString").Value = "myconn"
' Get the configuration file.
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
' Update the setting.
config.AppSettings.Settings("ConnectionString").Value = "myconn"
' Save the configuration file.
config.Save(ConfigurationSaveMode.Modified)
' Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings")

Web.config jsonSerialization maxJsonLength ignored

I have an MVC3 application running under .NET 4.0 and when I use JavascriptSerializer.Deserialize I'm getting an error.
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Reading Can I set an unlimited length for maxJsonLength in web.config I put the jsonSerialization maxJsonLength key in my web.config but it gets ignored. Though I can set the JavaScriptSerializer.MaxJsonLength property in code and it works fine.
I would like to have the value in the Web.config rather than code. Here is how I am using JavaScriptSerializer.
Dim client As New WebClient
...
Dim result = _client.DownloadString("Test")
Dim serializer = New JavaScriptSerializer
Return serializer.Deserialize(Of Foo)(result)
Here is my Web.config
<configuration>
<configSections>
...
</configSections>
<appSettings>
...
</appSettings>
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="/Http404"/>
<error statusCode="403" redirect="/Http403"/>
<error statusCode="550" redirect="/Http550"/>
</customErrors>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="4.0">
<namespaces>
<add namespace="System.Web.Helpers"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
<system.webServer>
....
</system.webServer>
</configuration>
According to the link you provided (the 2nd most voted answer), your web.config setting will be ignored because you're using an internal instance of the JavaScriptSerializer.
If you need the value to be stored in the web.config, you could add a key in the <appSettings> section called maxJsonLength with a value of 50000000 and then in your code, you could use it like:
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = ConfigurationManager.AppSettings['maxJsonLength'];

Add Interceptors through the web.config? NHibernate

I can't seem to find an example where someone added an interceptor via web.config - is this possible?
And yes I know about event listeners and will be using them on another project - but I wanted to see if I could get around having to inject the interceptor in code - thank you
I don't think it's supported but you can easily fetch and instantiate interceptors from a custom config section:
NHibernate.Cfg.Configuration cfg = ...
var interceptors = (NameValueCollection) ConfigurationManager.GetSection("nhibernate.interceptors");
foreach (string k in interceptors)
cfg.SetInterceptor((IInterceptor) Activator.CreateInstance(Type.GetType(k)));
web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate.interceptors" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>
<nhibernate.interceptors>
<add key="MyApp.Interceptors.SomeInterceptor, MyApp" value=""/>
<add key="MyApp.Interceptors.AnotherInterceptor, MyApp" value=""/>
</nhibernate.interceptors>
</configuration>