Merge wcf app.config with another config file another project? - wcf

I have a MyProject.config file that contains ConnectionStrings, AppSettings, and custom sections. I don't want to have to duplicate these settings in the wcf app.config file. Is it possible to merge MyProject.config with app.config or web.config if it is hosted in IIS. If so, where in the code would I do this and how would it be done?
As an example, in MyProject.config, I have a section group called Common and in Common I have AppSettings and ConnectionStrings. I know I can duplicate this in app.config, but then I would have to maintain it in two places.
MyProject.config:
<?xml version="1.0"?>
<configuration>
<sectionGroup name="common">
<section name="appSettings" />
<section name="connectionStrings" />
</sectionGroup>
</configSections>
<common>
<appSettings>
<add key="UserName" value="test" />
<add key="Password" value="test" />
</appSettings>
<connectionstrings></connectionstrings>
</common>
app.config for wcf contains ServiceModel configuration, I dont' want to have to put the above in app.config. I want to read it, does this make sense?

WCF will only load the config based settings for a service from the .NET configuration file for an application (web.config for IIS hosted services). Essentially you are fighting the .NET configuration file model.
If you want to keep the files separate then decide what will act as your primary config file and then use the configSource model for combining multiple config files

Related

.Net core request filtering and file downloads

We have an .net core Web application which simply hosts files for some of our client applications updates.
We decided to add Application insights in one of these client applications, and the file ApplicationInsights.config is a part of update.
The request to https://server/path/to/update/ApplicationInsights.config throws 404 error.
So far I’ve tried :
Add “.config” extension in static files definition on the startup : no effect (This worked for .exe and .dll)
Enable folder browsing for this folder, still no effect
It seems to be related to some out-of-box requests filtering.
The question is :
How do I disable all download restriction on a specific folder (Best)
OR
How do I disable ALL filtering for *.config files
Thank you in advance
That's because the default FileExtensionContentTypeProvider doesn't provide a mapping for *.config files.
To make it serve *.config files, simply create your own ContentTypeProvider, or add mapping for *.config :
var myContentTypeProvider= new FileExtensionContentTypeProvider();
myContentTypeProvider.Mappings.Add(".config","text/plain");
app.UseStaticFiles(new StaticFileOptions{
RequestPath = "/path/to/update",
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(),"path/to/update"),
ExclusionFilters.None
),
ContentTypeProvider = myContentTypeProvider,
});
[Update]
After a discussion, the following Web.Config ( by OP) works:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<fileExtensions>
<remove fileExtension=".config" />
<add fileExtension=".config" allowed="true" />
</fileExtensions>
</requestFiltering>
</security>
</system.webServer>
</configuration>

Changing which queues are used for NServiceBus Pub/Sub scenario

I'm new to NServiceBus and I'm trying to use it with IIS and SignalR. I have a working scenario but I'm curious how the client chooses which queues to publish to. I've noticed (by stopping IIS and running the publisher) that the message gets published to a system.web queue. I assume this is because my endpoint is being started by ASP.Net or something similar. However, this seems like a really generic queue to use and I would like to use application specific queue names. How do I specify which queues the publisher uses? I've changed the endpoint on the SignalR application but it doesn't seem to make much of a difference.
Here is my client config:
[EndpointName("signalbus.web")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
}
Here is my app.config from the publisher:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
<section name="TransportConfig" type="NServiceBus.Config.TransportConfig, NServiceBus.Core"/>
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
</configSections>
<connectionStrings>
<add name="NServiceBus/Persistence" connectionString="Url = http://localhost:9090" />
</connectionStrings>
<MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
<TransportConfig MaximumConcurrencyLevel="5" MaxRetries="2" MaximumMessageThroughputPerSecond="0"/>
<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="SignalBus.Messages" Endpoint="signalbus.web" />
</MessageEndpointMappings>
</UnicastBusConfig>
</configuration>
When you configure the bus:
Configure.With().DefineEndpointName("MyAppName")
Edit
I'm a little confused based on your posted code and your comments. Are you hosting the subscriber in IIS/ASP.Net? If so, I think EndpointConfig will get ignored, as (AFAIK) it is only used via the NSB Host.
See this link for configuring the Bus in your own process (or ASP.Net):
http://docs.particular.net/nservicebus/hosting-nservicebus-in-your-own-process-v4.x
Also, you shouldn't need to reference the subscriber endpoint in your publisher's config--it needs no knowledge of its subscribers. It gets that via RavenDB or whatever subscription storage you are using.

What is the expected contents of Web.config if appSettings entry is parameterized?

I'm trying to parameterize an appSettings entry in my Web.config. Since this is a part of a quite long build process, I'd like to verify that my parameterization actually works before trying it out on our CI server (i.e. trial and error is not a good idea).
So, if I run MSBuild with /T:Package to create my package, I expect that the .zip file created would contain a Web.config with my appSetting entry tokenized, just like a connection string is tokenized.
But, so far I do not get my expected result. Is my assumption wrong?
Is it maybe that the tokenization/replacing happens first in the actually deploy-step?
Here's the tokenized web.config. Notice how my appSetting isn't tokenized:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<lots of stuff here...>
<connectionStrings>
<add name="DefaultConnection" connectionString="$(ReplacableToken_DefaultConnection-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="mySetting" value="monkey"/> <!-- Shouldn't monkey this be tokenized? -->
</appSettings>
<rest of web config here ...>
The con string tokenization is taken care of in the web publishing MSBuild targets. It's not a part of Web Deploy itself. In your scenario I'd expect that the package was created and app settings are not modified.
When the package is created there are two ways you can see the parameters:
Use msdeploy.exe and pass GetParamters - http://technet.microsoft.com/en-us/library/dd569044(v=ws.10).aspx
You can crack open the .zip file and look at the parameters file inside of it

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" />