appsetting not working in wcfservice web.config file - wcf

Detailed Error Information:
Module IIS Web Core
Config Error The configuration section appSetting cannot be read because it is missing a section declaration
my web.config file below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add key="myserver" value="localhost:12345,localhost:12346"/>
</appSetting>
</configuration>

Try using appSettings instead.

Related

allowDoubleEscaping on .net core 2.0

I am using asp.net identity and GenerateEmailConfirmationTokenAsync gives me a token for email confirmation. I cannot use this code in confirmation url, because it gets an error :
The request filtering module is configured to deny a request that contains a double escape sequence.
The solution for that issue was to allowDoubleEscaping in web.config, but how can I do it in appsettings.json? I should write this code somehow in appsettings, or in Startup.cs:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
</system.webServer>
It's still an IIS setting if you're running in IIS.
Create a web.Release.config file (you don't need a web.config file in your actual project) with the following:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<security xdt:Transform="InsertIfMissing">
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</location>
</configuration>
When you publish a release build this will get added. Very important to include the part InsertIfMissing or it will be ignored.
You DON'T need a third party package such as this. 7
See also https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-3.1

can you use a variable in the message logging policy for host and port?

can you use a variable in the message logging policy for host and port? for example
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MessageLogging async="false" continueOnError="false" enabled="true" name="splunk">
<Syslog>
<Message>Message. id = {request.header.id}</Message>
<Host>{variable}</Host>
<Port>{variable}</Port>
</Syslog>
</MessageLogging>
At this time, the Host and Port entries cannot be variables.
An alternative to using variables at runtime is to set those attributes at build time using a build tool like maven.

Setting Connection String throws InvalidOperationException

I've effectively copied the first step I would use to connect to our DB from a C# project into VB.NET but VB 2010 Express is getting upset.
I have a winForms with an empty DataGridView. Behind this form I have added the following code:
In the App.config file I have the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name ="WH" connectionString="USER ID=xxx;PASSWORD=xxx;PERSIST SECURITY INFO=True;Data Source=xxx;Initial Catalog=WH" />
</connectionStrings>
</configuration>
At this point I thought I'd debug and I'm already getting an error:
What is the likely cause of this bug?
As an alternative, you could try creating a new settings file rather than using the ConfigurationManager class.
Say you create a file called ConnectionStrings.settings with a setting named WH, you can access the value via ConnectionStrings.Default.WH.

NLog in WCF Service

Can I use NLog in a WCF Service? I am trying to but cannot get it to work.
First I set up a simple configuration in a Windows Forms application to check that I was setting up correctly and this wrote the log file fine (I am writing to a network location using name and not IP address).
I then did exactly the same thing in the WCF Service. It did not work.
To check permissions I then added some code to use a TextWriter.
TextWriter tw = new StreamWriter(fileName);
tw.WriteLine(DateTime.Now);
tw.Close();
This worked OK so I know I can write to the location.
Check that your NLog.config file is in the same directory as your .svc file and NOT the Bin directory.
If you've just added the config file to the WCF project, then published it you will probably find your config file has been copied to the bin directory which is why NLog can't find it. Move it to up a level then restart the website hosting the service (to make sure the change is picked up).
This had me stumped for a while this morning!
Put your NLog config in the web.config file. Like so:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
. . . (lots of web stuff)
<nlog>
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/logs/nlog.log"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
</configuration>
See my comment to your original question for how to turn on NLog's internal logging.
To turn on NLog's internal logging, modify the top of you NLog config to look like this:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.mono2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="nlog_log.log"
>
The key parts are internalLogLevel and internalLogFile.
You can also set internalLogToConsole to true or false to direct the internal logging to the console.
There is another setting, throwExceptions, that tells NLog whether or not to throw exceptions. Ordinarily, this is set to false once logging is successfully configured and working. You can set it to true to help determine if your problem is due to an NLog error.
So, if you had all of those options enabled, the top of your NLog configuration might look like this:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.mono2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Trace"
internalLogFile="nlog_log.log"
internalLogToConsole="true"
throwExceptions="true"
>
My first guess is that NLog is not finding the config information. Are you using an external config file (NLog.config) or "inline" configuration (in your app.config or web.config)? In your project, is(are) your config file(s) marked (in Properties) as Copy Always?

Separate config file for Providers

In a small test project, I currently have the provider sections in the web.config. I want to move that to a separate config file, like providers.config. My current provider instantiation code is like:
//Get the feature's configuration info
ProviderConfiguration pc = (ProviderConfiguration)ConfigurationManager.GetSection(DATA_PROVIDER_NAME);
This code works if the provider info is in web.config, but how to I read this info from another file (like providers.condfig) because it seems that the ConfigurationManager "reads" only web.config file. I may be missing something very simple here :)
Would love to get more inputs on this.
Thanks
V
If you want to reference an external file for a collection of settings in the web.config you can do this:
<?xml version="1.0"?>
<configuration>
<appSettings file="externalSettings.config"/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
</system.web>
Hope this helps.
So in your case you can do something like this:
<configSections>
<section name="ProviderName" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<ProviderName file="provider.config" />