IIS WCF Service not using app.config - wcf

I have a .NET 3.5 WCF service hosted in IIS. The project service library has an app.config file with some configuration settings (Database connection strings, etc.). I deploy the project via a website project in Visual Studio, which generates a web.config to manage the endpoints. Is there a way I can put the app.config settings from my service library in to the web.config? The IIS hosted service seems to be using default values from the settings designer, and ignoring even an expliclty copied in app.config. I'm guessing this has something to do with the fact that a DLL can not utliize an app.config.
My service application is set up to pull the settings settings from the [MyAssembly].Properties.Settings.Default namespace.

Can you use external configuration files?
Your web.config:
<config>
...
<connectionStrings configSource="myConnections.config"></connectionStrings>
</config>
And then your external myConnections.config file:
<connectionStrings>
<add ... />
</connectionStrings>
You can have multiple external configuration files referenced from your main web.config file. See see this blog post for a nice explanation of how/why to do this.
I hope this helps!

Related

What are the config for NewRelic ASP.NET Core appsettings.json

The settings of referenced here was introduced as
Here are instructions for enabling the agent for .NET Framework and.NET Core.
https://docs.newrelic.com/docs/apm/agents/net-agent/installation/install-net-agent-windows/#app-config
It shows the following configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="NewRelic.AgentEnabled" value="true" />
<add key="NewRelic.AppName" value="DataServices" />
</appSettings>
But for ASP.NET Core, you don't use XML configuration, you use appsettings.json:
What are the configuration for ASP.NET Core?
Can the configuration be added to the Configuration of Azure Web App Service rather than in the local appsettings.json ? And if so, what are the keys?
In the doc page you provide, it is explained how to set it up in dotnet core.
You don't need to add anything to the appsettings file; just set an environment variable. Beware that it must be set only in the project's scope or all dotnet core applications running on the host will be monitored.
CORECLR_ENABLE_PROFILING=1
They provide a helpful link to a page with even more details: https://discuss.newrelic.com/t/setting-net-core-agent-environment-variables-per-process/157750
In addition to the methods they mention, you could also put the variable in a .env file and load it, for example as explained here https://dusted.codes/dotenv-in-dotnet

ASP.NET Core deploy only applicable appsettings.{EnvironmentName}.json

I've made my first proof of concept ASP.NET Core application, I have 5 appsettings files:
appsettings.json
appsettings.Development.json
appsettings.Test.json
appsettings.Staging.json
appsettings.Production.json
I'm running the app in IIS so I actually have web.config files so that I can set the ASPNETCORE_ENVIRONMENT environment variable on a per application basis:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<aspNetCore processPath="dotnet" >
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development"></environmentVariable>
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
I have 4 web.config transforms so I can publish from Visual studio, and it will set the correct environment variable value for each environment.
When I publish to each environment though it publishes all the appsettings files. Is it possible to get it to publish on the root appsettings.json and the applicable environment specific one, but omit the others?
No, it's not possible. ASP.NET Core is not like ASP.NET. In ASP.NET, you literally published for the environment; if you wanted to switch to a different environment, you'd need to republish. ASP.NET Core is published for all environments. The same published app can be picked up and moved to any environment, without change. The actual environment is generally externalized, such as via and environment variable, and can be changed on a whim, without requiring new code to be deployed. This is actually a feature of ASP.NET Core.
Now, the way you're handling the environment variable does somewhat make it dependent on the publish, but that's just modifying the web.config, which itself only has meaning when deploying to IIS. ASP.NET Core itself doesn't care about or use web.config, and honestly doesn't even care about or use release configurations.

Why is this class library dll not getting information from app.config

I am developing a custom HttpHandler, to do so I write a C# Class Library and compile to DLL.
As part of this I have some directory locations which I want not hard coded in the app, so i'm trying to put it in the app.config which I've used before.
Before this has worked by just going building the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Share" value="C:\...\"/>
</appSettings>
</configuration>
And then obtaining this in code like:
var shareDirectory = ConfigurationManager.AppSettings["Share"];
But when I compile it and put it in the bin folder for the webservice, it keeps getting null for shareDirectory, probably because it can't find app.config. So how do I make sure this is included so I don't have to hard code my direcotry locations? I notice it will essentially after compiled we get the assembly.dll and the assembly.dll.config which is the app.config file, so it's definetly there in the bin folder!
That is because your web service uses web.config
You're probably confusing the scope of your class library.
Remember, your config, be it web.config, or app.config, is the config present in the HOSTING application. In this case your hosting application is the WebService, hosted of course by IIS, so your config file is the web.config.
If you had a console app which somehow used that class library (though probably not in an http handler context), then the config would be the app.config in the console app, not the app.config in your class library.
You need to put the configuration in your web.config file, not in assembly.dll.config: .NET does not (by design) read assembly.dll.config files.

how can i setup my nhibernate library to work in both a web and console application?

on the web application, I am using a NHibernate helper that looks up the session that was opened in a httpmodule (and committed there also).
<property name="current_session_context_class">web</property>
In the console application, what do I do?
Your options are: "call" & "thread_static". Have a look at this for more detailed explanation on all available contexts:
http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session
In order to have your library work for both a web and a console application you have two options:
Based on a application setting in the App.config and in the web.config have the Session Factory built accordingly by setting the current_session_context_class property manually in the code and remove it from the hibernate.cfg.xml file.
Include a in the web application's web.config and in the app.config of the console application. This way you can have the current_session_context_class property set to different values. If I am not mistaken the in the web.config and in the app.config overrides the values of the hibernate.cfg.xml. If I am wrong then you will just have to include in the web.config and the app.config the complete and remove the hibernate.cfg.xml file from your library.

IIS publish of WCF service -- fails with no error message

I havea WCF service which I publish from Visual Studio 2008 to an IIS 6. According to the output window of VS, the publish succeeded, no error messages or warnings. When I look at IIS, the virtual directory was created, but there is no .svc listed in the directory. The directory just has my web.config and a bin. Any attempts to call my WCF service fail cause they don't exist.
How can I see an error message of what's going wrong?
By trial-and-error, I discovered changing my app.config before publishing will make the service show up. Namely my app.config file has these lines:
<binding ...>
<security mode="Transport">
<transport clientCreditionalType="None"/>
</security>
</binding>
If I switch "Transport" to "None", then my service shows up on IIS. But I do have a certificate installed on IIS on the server, and as far as I can tell, everything is configured correctly on the server.
There is no error message in the event log.
How can I get a find more error messages about why the service is failing to show up?
Try to copy your WCF project to your machine with IIS 6.0 as is, without using Publish of VS. If it helps, then the problem is within VS, otherwise the problem is with the IIS. Make sure .NET Framework 3.5 and ASP.NET are installed on the IIS machine. Then make sure that there are Mapping Handlers for .svc extension.
Hope it helped
To find out more turn on the wcf logging. The easiest way to do this is to use the wcf configuration tool.
Edit based on your comment: The way we publish a service is to create an msi package using the setup project. There are a couple of outputs that need to be included:
Primary output (that is dll's) must go to the bin directory.
Content output. You must create the svc files and mark them as content.
To publish WCF service in IIS 7 or 8 you must register handler for svc extension in web.config:
<system.webServer>
<handlers>
<add name="svc" path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>
(System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 for .Net 3.5)