ZZZ
ZZZ

Reputation: 2812

Web.config gone in Asp.net 5 mvc6, how about non web config?

Context

In asp.net 5 vNext MVC6, Web.config is gone, in favor of json file as config. However, in my web.config, I have settings of WCF clients, trace listeners of System.Diagnostics and Essential.Diagnostics, and these components (WCF clients and trace listeners apparently could not read json at startup). So in vNext Microsoft has abandoned System.Configuration apparently.

Question

Are there some migration path so I don't have to totally rewrite in order to make these components read config info at startup?

Upvotes: 3

Views: 245

Answers (1)

Sly Gryphon
Sly Gryphon

Reputation: 3785

Short answer: It works fine in ASP.NET Core MVC on .NET Framework (4.5.1+), put your settings in app.config, but doesn't work in ASP.NET Core MVC on .NET Core.

Longer answer:

ASP.NET Core can run on both .NET Framework and .NET Core, so I guess the migration path is to continue using .NET Framework for now.

For System.Diagnostics, and Essential.Diagnostics (which I contribute to), I have been able to at least get a demo of this working in ASP.NET Core running on .NET Framework (not .NET Core).

In the .NET Framework, TraceSource, etc still load from the config. In an ASP.NET Framework project there will be an app.config file (not web.config) where you need to put the configuration sections.

Note: Just Building (Debug > Start new instance) does not update the .exe.config file in the bin directory; you need to do a Clean or Rebuild, and then Debug (or delete the output bin folder).

You can write to TraceSource either directly (for legacy code), or via the new Microsoft.Extensions.Logging, with the Microsoft.Extensions.Logging.TraceSource provider configured. (The new interface includes some nice hierarchical support features.)

Either way, it will load and write to the configured TraceSource, which can output to system TraceListeners.

You can also configure additional listeners from Essential.Diagnostics (although the RollingFileTraceListener wasn't correctly handling the {AppData} token in the filename, so I had to hard code the path to C:\Temp\Logs).

The above also works in a console app (simpler), with the .NET Framework 4.5.1 and above.

There is some example code in Essential.Diagnostics: https://essentialdiagnostics.codeplex.com/SourceControl/latest#Examples/TestAspNetCoreOnNetFx/Controllers/HomeController.cs

I suspect it may be similar with WCF: the .NET Framework version should continue to work, using app.config, but the .NET Core implementation may be a shell only.

On .NET Core:

Note that Essential.Diagnostics will not work on .NET Core (it doesn't support it yet, as at Feb 2017), and most system TraceListener classes are also not supported.

The only one that is is the TextWriterTraceListener, which I have got working with a manually created TraceSource, however from looking at the .NET Core source code, I don't think there is a way for TraceSource to automatically load configuration yet.

Upvotes: 2

Related Questions