-2

I'm in the process of moving the UI side of an application to the new ASP.NET Core MVC structure. Unfortunately, I still need to reference a data layer which is built in the prior-generation ASP.NET framework. This data .dll has the appropriate connection strings to various databases all being managed by ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString which required me to mimic in the UI layer in order to actually get at the data.

Now, with ASP.NET Core MVC, the web.config for configuration has been replaced with the appsettings.json file.

That paradigm shift breaks all my access to data since I can no longer replicate the connection string in the UI application.

Is there an appropriate solution that can make this data layer .dll more self-contained and rely on its own, internally defined connection string(s), while still exposing the Methods to the "containing" application - in this case, the UI layer?

Tseng
  • 61,549
  • 15
  • 193
  • 205
rcastagna
  • 185
  • 2
  • 14

2 Answers2

1

Actually you do have reference to your connection string from the new .json file. You will do something like:

var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection();
var config = builder.Build();
config["somekey"] = "somevalue";

// do some other work

var setting = config["somekey"]; // also returns "somevalue"

here is a link: docs.asp.net

Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
  • Yes, I saw that in the docs, and figured I'd give it a try - hoping that there would be an "automagic" translation between the old syntax and the new. Unfortunately, no such luck...the data layer .dll was still looking for the old ConfigurationManager stuff. – rcastagna Sep 08 '16 at 11:51
  • Do you have access to the source code for the .dll? My thinking is that the .dll project should contain an app.config file. So then when you use it in the other project it would look for the same settings in its configuration file. So, then if the new project doesn't use a configuration file for its settings it doesn't mean that you can't add a configuration file (app.config or web.config) and this may satisfy the requirements needed by the .dll. Try it... Let me know your results. – Robert Green MBA Sep 08 '16 at 17:44
0

I have resolved my issue with a work-around, and it will do for now. Ultimately, I'd like to find a better option, but I'm moving forward which makes my boss happy.

I ended up changing my method signatures to accept a string value representing the connection string that's no longer in the calling project because of the conversion to Core MVC.

In the called .dll, the code now looks to see if there is a value to the passed parameter, and, if so, uses the passed value to initialize the SqlConnection. If no parameter is provided, it will look to the config section in web.config using the ConfigurationManager capabilities.

This will allow the existing project to use the code, as well as the new Core MVC project. It's a bit kludgy, but functional.

rcastagna
  • 185
  • 2
  • 14