ASP.NET MVC WebConfig with linked config file causes exception - c#

I have a large ASP.NET MVC project with a data access layer that uses Entity Framework. As well, I have a Windows service project (in a different solution) that makes use of the same DAL project. I have my Web.config/App.config setup so that the connection strings are loaded from a separate connection.config file that is in a shared location. The connection configuration is linked (Add Existing Item > Add As Link) in both projects with Build Action = Content and the Copy To Output Directory = Copy if newer.
The Windows service project runs with no issues, I can see the service connecting to my database correctly. However the ASP.NET MVC project throws an excpetion saying Unable to open configSource file 'connection.config'. (C:\Path\To\My\web.config line 9).
I checked that the connection.config file is copied to output directory. I also tried making a copy of the connection.config file in the ASP.NET project (instead of linking it) and this runs just fine. Why does linking the file suddenly cause an exception?
My files below for reference
App.config (Windows service)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connection.config" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
[...]
<connectionStrings configSource="connection.config" />
[...]
</configuration>
connection.config
<?xml version="1.0"?>
<connectionStrings>
<clear />
<add name="MyConnection" connectionString="Data Source=./localhost;Initial Catalog=MyBatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Edit
I came across this answer that works, however I'm concerned how this would affect the application in release mode or when it's deployed. As well I still don't understand why having the file linked has a different behavior than when it's copied in.

Related

.Net 6, C#, WindowsForms, Loading dlls from a sub directory using "probing" doesn't work?

I know this kind of question has been asked many times already, but for older versions of .NET and on 4.8 my project has been working fine with such in app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="languages;bin;"/>
</assemblyBinding>
</runtime>
...
</configuration>
but after I update to .NET 6 - its stops, anybody knows how to make it work on .NET6?

Regarding Session Timeout using selenium c#

My Application has a Functionality which takes time to load the search results like more then a minute because of which my scripts fails and gives a 60 seconds session timeout error message. I googled few solutions and got one from stack overflow"How to set session timeout in web.config" but i am not sure where exactly to implement it. i have a file in my framework called "app.config" and the code in the app.config is below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This Below mentioned code is given in the stack overflow to make necessary changes in web.config file to set the session timeout
<configuration>
<system.web>
<sessionState timeout="20"></sessionState>
</system.web>
</configuration>
please help me where to make the necessary changes in app.config file.
You could set an implicit wait for the driver in the following fashion:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Now the only reason to use app.config is if you want to make this timeout configurable. In which case in your app.config file you would add a section:
<appSettings>
<add key="driver.Timeout" value="20" />
</appSettings>
Then in your code you would do something like:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(Int32.Parse(ConfigurationManager.AppSettings["driver.Timeout"]));
This way if you deploy your app somewhere and wanted to make the timeout configurable you would just edit your app.config file in a text editor and change the value.

.net Could not load file or assembly MySql.Data. Strong name validation failed (0x8013141A)

Error:
An unhandled exception of type 'System.IO.FileLoadException' occurred in WindowsFormsApplication4.exe
Additional information: Could not load file or assembly 'MySql.Data, Version=8.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
Information:
Visual Studio 2015
Windows 10 - x64
C#
MySQL 6.9.9
Problem:
I've made a class library using MySQL to run various queries to my online database.
After including the dll in my separate project, the only line of code I'm using is the following: (which it throws the exception on)
// Runs a simple select statement to find matches for 'John Doe' in my online MySQL database table... and stores results to a dataset
DataSet ds = MyDllNameSpace.Database.People.Load("John", "Doe");
App.Config files:
My dll project: (I did NOT sign this project)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
My separate project: (I did NOT sign this project)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.8.0" newVersion="8.0.8.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
What I've searched/tried:
Made sure to use the same version of MySQL in both projects
Put the dll to both my library and MySQL in the bin folder of my separate project and adding them as a reference
Also tried adding both dlls of my library and MySQL to the separate project and selecting 'always copy'
Changing around the App.config file (assuming the issue is in here?)
Lastly, I tried signing both the dll project, and the project using the dll with the same password (checking sign the assembly). This also did nothing. :(
Thank you
Have you tried signing the project(s)? That may remove your error.
Maybe this link about your error will help: https://blogs.msdn.microsoft.com/keithmg/2012/03/20/strong-name-validation-failed-exception-from-hresult-0x8013141a/
or this stackoverflow question:
Strong Name Validation Failed

How do I import .dll library from subfolder in C#

When I have the library in the same folder as app I can simply:
[DllImport("kernel32")]
public extern static IntPtr LoadLibrary(string librayName);
IntPtr iq_dll = LoadLibrary("IQPokyd.dll");
I also have this in my app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="plugins;" />
</assemblyBinding>
</runtime>
</configuration>
I'm able to load this library. Problem is that it is using some config files which needs to be in app run directory. Is there some possibility how to tell the library that files it needs are in the same folder as the library?
Since the DLL searches for the config in the current directory, it makes sense to temporarily change the current directory before loading it:
string saveCurDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.Combine(Application.StartupPath, "plugins"));
IntPtr iq_dll = LoadLibrary("IQPokyd.dll");
Directory.SetCurrentDirectory(saveCurDir);
You can do this by adding a probing tag to your app's .config file.
For example, if you wanted libraries to be loaded from a /lib/ subdirectory, your config would look like this:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib"/>
</assemblyBinding>
</runtime>
</configuration>

How to move I18n folders of WPF application

When building my WPF application all internationalization/locale folders are put inside the folder of the executable.
/MyApp
/MyApp/de
/MyApp/fr
/MyApp/otherSpecialFolder
/MyApp/...
The problem is that this mixes up with some other folders. Is it possible to put the internationalization into a separate folder and let the Wpf app search there instead? For example:
/MyApp
/MyApp/i18n/de
/MyApp/i18n/fr
/MyApp/otherSpecialFolder
/MyApp/...
This problem occurs not only for own localization ressources but also when adding thirdparty controls like Xceed.Wpf.AvalonDock.
Yes you can. You just have to add probing element into your App.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="i18n"/>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
probing element specifies the path where assemblies are searched for.

Categories