Can't getting value from Web.config - c#

I am facing an issue with getting a value from Web.config.
Here is my web.config code which contains Key
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Email" value="myname#mydomain.com" />
i want email key value, i am writing,
string From = ConfigurationManager.AppSettings["Email"].ToString();
but its giving me error "Object reference is not set to an instance of an object"
My other web.config declarations are:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com" port="***" enableSsl="true" defaultCredentials="false" userName="myname#mydomain.com" password="mypassword" />
</smtp>
</mailSettings>
</system.net>
Any help appreciated!
Thanks!

Make sure that your start up project is set correctly. If you're running this from a separate project (i.e. in a test), it won't be looking at that Web.config but its own Web/App.config file.

Related

Config reads value from another config

I am trying to setup configs so it is easy to switch from dev to production with out constantly having to copy config settings from one place to another.
What I am trying to do is to setup a singe config that contains usernames, passwords and other access strings that I want to omit from source control for obvious reasons.
I could then reference those keys in other configs.
An example of what I am thinking
Content of a config that stores keys:
<keys>
<add key="username" value="1forest1" />
<add key="password" value="life-is-like-a-box-of-chocolates" />
<add key="url" value="http://www.example.com" />
</keys>
Content of a config that requires keys:
<service name="SomeService">
<settings>
<setting key="Container" value="MyContainer" />
<setting key="MaxBytes" value="12582912" />
<setting key="Timeout" value="30000" />
<setting key="Host" value=[url value from keys config] />
<setting key="username" value=[username value from keys config] />
<setting key="password" value=[password value from keys config] />
</settings>
</service>
I am not to optimistic that this is possible due to reasons, but any patterns that might point towards a solution/workaround for this would greatly appreciated.
Check out the Microsoft.Extensions.Configuration tools. Adding json files.
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Secrets/values.json")
.Build();
Inject the Configuration then get the values:
var section = configuration.GetSection("Settings");
string connectionString = section["ConnectionString"]
json example:
{
"Settings": {
"ConnectionString": "Server=tcp.....",
"SomeOtherValue": "SomeValue"
},
"MoreStuff": {
"foo": "bar"
}
}
Have an appsettings file:
Web.config:
<appSettings file="AppSettingsSecrets.config">
<add key="Container" value="MyContainer" />
<add key="MaxBytes" value="12582912" />
<add key="Timeout" value="30000" />
<add key="Host" value="" />
<add key="username" value="" />
<add key="password" value="" />
</appSettings>
AppSettingsSecrets.config :
<?xml version="1.0"?>
<appSettings>
<add key="Host" value="SecretHost" />
<add key="username" value="MyUname" />
<add key="password" value="S3cr3t" />
</appSettings>
Give the location/name of the second config file as one setting in first config file, and read it dynamically.

WebConfig Merge AppSettings

The situation is this, I want to have a key in my Web.config that gets changed in my Web.Debug.config and my Web.Release.config but I am unsure as to how to accomplish this.
I have my Web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
I'm not sure what my Web.Debug.config is meant to be to add a key key="host" value="somevalue", so I've tried this:
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
This:
<appSettings xdt:Transform="Insert">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
This:
<appSettings xdt:Transform="Replace">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
To no avail. I read my config like so:
var appSetting = WebConfigurationManager.AppSettings["RunningHost"];
So I found out the answer for this.
It's because when I debugged the web project it didn't compile a new config combining the web.debug.config with the web.config.
However, if you publish it for release then it will combine the web.release.config with the web.config.
So the simple fix was to put the RunningHost in the web.config. Then have the release override it!
The first option is correct. Your full Web.Debug.config should look something like this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
</configuration>
You can right-click on the Web.Debug.config in Solution Explorer and choose Preview Transform to see the transformed output.

Nlog, Not sending mails to Outlook

Mail Configuration is Nlog.config file
<target name="mail" xsi:type="Mail" smtpServer="smtp.emailsrvr.com" smtpPort="25" smtpUserName="samplemail#samplemail.com" smtpPassword="Password"
from="sample-test#intsof.com" to="vinay#sample.com" subject="Hello Mail from Nlog" html="false" encoding="UTF8"/>
<rules>
<logger name="*" level="Error" writeTo="mail"/>
Configuration in App.config.
<configuration>
<system.net>
<mailSettings>
<smtp from="Sample-test#sample.com" deliveryMethod="Network">
<network defaultCredentials="true" userName="vinay-test#sample.com" password="Password" host="smtp.emailsrvr.com" port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
If i use the delivery method SpecifiedPickupDirectory and give a local path then its sending mail, but not to outlook.
For this question, the accepted answer recommended changing encoding="UTF8" to encoding="UTF-8". Try that and see if it helps.
This can be a solution as well:
Config:
from="${event-context:item=Sender}"
Logging code:
eventInfo.Properties.Add("Sender", UserPrincipal.Current.EmailAddress);

How to transform this web.config section?

I have following config for my mail:
<system.net>
<mailSettings>
<smtp from="foo#bar.com" deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:/test"/>
<network host="localhost" userName="" password=""/>
</smtp>
</mailSettings>
</system.net>
This is my .Release version:
<system.net>
<mailSettings>
<smtp from="foo#bar.com" xdt:Transform="RemoveAttributes(deliveryMethod)">
<network xdt:Transform="Replace" host="192.168.1.9" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
How do I remove
<specifiedPickupDirectory pickupDirectoryLocation="C:/test"/>
so it doesn't show in my .Release at all?
Also, I would like to remove other namespaces like System.Diagnostics completely. What is the syntax for doing so?
For specifiedPickupDirectory element this should work:
<specifiedPickupDirectory xdt:Transform="RemoveAll" />.
For System.Diagnostics:
<system.diagnostics xdt:Transform="RemoveAll"></system.diagnostics>
<system.net>
<mailSettings>
<smtp from="foo#bar.com" xdt:Transform="Replace">
<network xdt:Transform="Replace" host="192.168.1.9" userName="" password="" />
</smtp>
</mailSettings>
</system.net>
This will replace that entire tag with yours.. hope this is what you are looking for..
the good thing about this is that you dnt end up polluting your transform config with unnecessary remove commands like some of the answers stated here..
consider the case where you have more than one child tags..
Rather than attempting to remove the config from your release version, can you take it from the base version and just add it to the .Debug version? That might be simpler. However if you want to remove it I think you can use <specifiedPickupDirectory xdt:Transform="Remove"/> or something similar.
#katit , you should maintain two different configs for dev and release.
your webconfig should be dynamic and take the configs as below
/qa/yoursettings.config
/release/yoursettings.config
one more sample
<connectionStrings configSource="config\qa\connectionStrings.config"/>
when you go to qa or release , switch your web.config accordingly.
in this way it will be lot cleaner.

What is ServicePointManager.FindServicePoint used for?

Can someone explain what ServicePointManager.FindServicePoint is intended to be used for? I have been writing some code to work with proxies in C#, and have seen indicators that it might be useful in this respect, but can't see why or how. How is this class (ServicePointManager) or method (ServicePointManager.FindServicePoint) supposed to be used (or when)?
Thanks.
The ServicePointManager.FindServicePoint(...) method will help you get the ServicePoint object that you've been specified in the configuration file.
Let's say, this is your configuration file:
<configuration>
<system.net>
<connectionManagement>
<add address="http://www.contoso.com" maxconnection="2" />
<add address="192.168.1.2" maxconnection="4" />
<add address="*" maxconnection="1" />
</connectionManagement>
</system.net>
</configuration>
This code would retrieve the "http://www.microsoft.com" ServicePoint:
ServicePoint sp1 = ServicePointManager.FindServicePoint(new Uri("http://www.microsoft.com"));
You can read all about it here.

Categories

Resources