In our web project, we have a custom Database Connection class that creates the SqlConnection object and get the DB connection string for us from the web.config.
While this is nice, there's one problem. All projects inside our web project depend on that web.config being there so that this class can work.
There are needs to use this db connection from our DL in other solutions or projects that should not require a web.config such as a Console project that is trying to use lets say some of the DL methods from our web project to do some data manipulation.
Consequently, when I add some of our BL project methods to my console solution, it's looking to create the DB connection but I have no web.config in my Console application of course so it bombs out.
Is there a better way to manage the creation of the SqlConnection than:
a) putting that class in a web project
b) making that connection reliant on web.config keys
so that non web based projects can use the BL without that BL referencing and being reliant on a connection that is reliant on ultimately keys in a web.config?
In your console app those DB connection settings need to go into App.Config, however you clearly don't want to have them stored in both App.Config and Web.Config, so you can use the following technique.
1) Move the DN configuration settings to a single DBSettings.config file
2) Reference that config from Web.Config and App.Config as required
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="DBSettings.config">
...
</configuration>
This technique is detailed here.
Related
I have a mvc .NET web application written in C# and I have a web.config file associated with it for web specific configuration values. I also have a windows service application that will be running on the server in the background that has a App.config associated with it. I have linked the file within the web application and can see the file with updated values. But I am unable to use those values in my controller to display them to the UI. Is there a way to make a call to the app.config values to use in the controller and views of the web application? Right now it seems like they are coming in null due to them not being in the web.config.
Any help is apprecaited.
As long as permissions are worked out, you should be able to open the shared config file thusly:
var map = new ExeConfigurationFileMap();
//TODO: resolve this path in whatever way makes sense for your situation.
map.ExeConfigFilename = #"C:\MyConfig.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
//do something with config, e.g. config.AppSettings.Settings["Blah"];
Otherwise, you can do something like put shared settings into machine.config, but it's typically wise not to mess with that file.
I'm attempting to architect a solution involving a WCF service, which calls a dll containing an EntityFramework6 model. When I attempt to unit test the service however, I receive a message:
Additional information: No connection string named 'SyslogEntities' could be found in the application config file.
My flow is arranged logically as:
SyslogDataSvcTest.dll (app.config has service bindings) ->
SyslogDataSvc.svc (web.config has provider and connection string) ->
LibSyslogData.dll (app.config has providers and connection string)
All the code that touches EF is in the libSyslogData.dll. It maps data to upper layer objects internally, and returns them, instead of exposing its own model info, so EF use should be really isolated.
So what am I doing wrong?
Edit:
I got it working well, but in perhaps a weird way. It seems the settings in my app.config were not correctly specified by NuGet when installing the EF and MySql dependencies.
Instead I created an in-code configuration class as described on MSDN here:
https://msdn.microsoft.com/en-us/data/jj680699
Now that I have this class:
public class EFConfiguration : DbConfiguration {
public EFConfiguration()
{
//For some reason this works much better than the app.config.
//With this defined, upper layer config documents need only specify the "SyslogEntities" Connection string.
SetExecutionStrategy("MySql.Data.MySqlClient", () => new MySqlExecutionStrategy());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
SetProviderFactory("MySql.Data.MySqlClient", new MySqlClientFactory());
SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
}
}
I can leave the specifics of the DB implementation to the datalayer, and only configure the connection string at the upper layers.
You most likely don't test the actual wcf service (I mean online) based on that exception. I think you are just unit testing the wcf service dlls code. So this would mean the app.config used is from the unit test assembly. Just 1 app.config for runtime for application, having an app.config for a class library does not make sense anyway either.
You need to provide the EF connectionstring that your EF data-access layer is using in your unit test assemblies, as you are doing in your wcf services web.config (since you are not testing the online wcf service). If you want to be totally free of EF references in your unit tests and wcf service configs, you need to override the default constructors for your EF entities and provide the "normal" dbprovider + connectionstring in your app/web configs and create the EF connection string(s) on the fly.
I have 2 C# .net applications such as
1. A data access applicatin which has an entity frame work data model to connect to the DB.
2. A web application which will contain the dll of 1st application and getting data by calling functions from dll
I need to take the connection string from the 2nd application web config file and pass it to the 1st application. In the 1st application data should be retrieved according to the connection string from the 2nd application.
There are quite a few ways you can achieve this. One (which I won't go into here) is through some kind of bootstrap loader - where you pass a connection string via the constructor of your repository (or data context), or you could construct a SqlConnectionStringBuilder (some further details here).
If you want to keep things simple, store the connection string in your web.config file of your web application, set the key to the name of your object. So let's say you have extended DbContext with an object called MyDbContext. In the web.config you'll have the following as your connection string
<add name="MyDbContext" connectionString="<conn string>" providerName="System.Data.SqlClient" />
Entity framework will use this connection string automatically, as the key is the same as your context.
If the dll is to act as a data access layer, it should be retrieving the data and passing it to the web application. Not the other way around.
If you choose to do it that way anyway, you'll have to make a function that retrieves the connection string from the app.config of the dll and pass it to the web app.
To access the connection string(s) in the config file of an assembly, us the ConfigurationManager class, (opposed to the WebConfigurationManager class that you would use in a web application). It accesses the elements of the config in the same maner with teh same calls, just intended for generalized config reads/writes.
EDIT: As mentioned by #PaulAldred-Bann, the connection string "should" be stored in the web apps config file if it is needed in the web app as well. The assembly would still have access to this (you would just move the settings to the web.config from the app.config. The reason it can be accessed from there is, the assembly, when referenced, acts on behalf of the caller (in this case, the web app).
// Set LastRun to now
config.AppSettings.Settings["LastRun"].Value = DateTime.Now.ToString();
// Save all settings
config.Save(ConfigurationSaveMode.Modified);
This code was working fine in my development server but not in my production server. It seems like my program is unable to communicate with my app.config file. I have checked all the "obvious" . . Any ideas ... ?
From your code example, I cannot tell how your config variable is initialized. But, from the comments, you have a web app. Unless you are attempting to load a specific app.config file, the web app will attempt to get AppSettings from web.config.
It's not a good idea to programatically change the values of web.config. Changing web.config will cause an application restart.
If you have a different app.config for storing this type of information, that would be better than trying to change web.config. But you'll have to specifically load the file, something like this:
Configuration config = WebConfigurationManager.OpenWebConfiguration("yourPath\app.config");
ConfigurationManager.OpenExeConfiguration() is intended for use within an executable application not a web app. Try using WebConfigurationManager as shown above.
You find some more information in this SO question/answers.
More information can be found in this SO question/answer.
I am creating a windows mobile 6 application which will consume a web service (.asmx) for different clients.
As I know, I will need to manually “Add Web Reference”; then I will be able to call those functions.
Is it possible to configure web reference as a variable from code behind?
That way I can keep the url of web service in a text file. For different client, I just need to edit that text file instead of recompile that application again.
You'll have to add the Web Reference at design time.
At runtime, you can modify the URL of your target web service using the Url property. Here's an example of pulling the target URL from the app.config:
var ws = new MyWebService();
ws.Url = ConfigurationManager.AppSettings["SomeUrl"].ToString();
The only catch here is that the WSDLs of the design-time and run-time services must match.
Yes, just add something like :
<configuration>
<appSettings>
<add key="WebReference" value="URLofASMX"/>
...
then call it by :
string URL = ConfigurationManager.AppSettings["WebReference"].ToString();
You'll need to possibly add a new reference to System.Configuration to the project if you can't access ConfigurationManager just by including System.Configuration.