Equivalent of web.config in class library - vb.net

I have three web applications that all access the same database. There is a lot of duplicated code there, so I'd like to move all the common stuff into a new project that each three application can just reference.
Firstly, I assume that a Class Library is the best thing to use to achieve this?
Secondly, where in a Class Library would I store the connection string settings that previously would have been inside web.config?
In ASP.NET I am currently doing the following:
Web.config
<connectionStrings>
<clear/>
<add name="A" connectionString="Persist Security Info=False; User ID=user; Password=pass; Initial Catalog=myDb; Data Source=localhost; Connection Timeout=60" providerName="System.Data.SqlClient"/>
<add name="B" connectionString="Persist Security Info=False; User ID=user2; Password=pass2; Initial Catalog=myDb; Data Source=localhost; Connection Timeout=60" providerName="System.Data.SqlClient"/>
</connectionStrings>
ASP.NET Pages
Dim conn as New SqlConnection(ConfigurationManager.ConnectionStrings("A").ConnectionString)
I've searched for an answer, but can only find answers for accessing a web.config file from within a separate project, which is different.

You have to add an app.config file for configuration. But when reading from a class library, to have to use the app.config situated on the entry point executable project.
Imagine the following projects:
-ClassLibrary.dll (with)
- Class1.vb
- Class2.vb
- SettingsReader.vb
-ConsoleProgram.exe (with)
- Program.vb
- App.Config
Is ConsoleProgram the project where you must put your appSettings (in app.config), and then you can read them using the SettingsReader from the class library.
If you really want to read settings from your dll assembly, you could insert an app.config and set that file as "Embedded resource", so you can read the file using the Assembly.
Something like this:
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyApp.App.Config";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}

Related

How to read web.config file in .Net Core app

I have created a .Net Core API and I referenced a .Net framework application to it. The referenced Application connects to a data base and its connection string is stored in web.config file:
string CONNSTR =ConfigurationManager.ConnectionStrings["SHOPPINGCNN"].ConnectionString;
The .Net Core application uses appsettings.json instead of web.config file. When I run the API and try to use the referenced application resources, the above code will be triggered and will return null because there is no web.config file exists in .net Core app. What is the best solution to resolve this issue
Because .Net Core applications are self hosted and can almost run on any platform, they are no longer hosted on IIS. The .Net Core application settings are stored in a Json format (appsettings.json) by default while .Net Framework application configurations are stored in a web.config file in XML format. For more info about .Net Core applications, you may read Configuration in ASP.NET Core. In my case, I was trying to access the data layer of a .Net Framework assembly from a .Net Core 2.0 assembly. To achieve this, there is no need to install System.Configuration.ConfigurationManager package in the .Net Core application but you only need to add app.config to the .Net Core assembly then add the connection string to it:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SHOPPINGCNN" connectionString="server=your server name;integrated security=true;database=your database name" />
</connectionStrings>
</configuration>
After that, everything will work fine. Make sure that you use the same connection string name (SHOPPINGCNN in my case) that you used in your .Net Framework application otherwise you will not get the desired result. I did this in my project and it works 100%.
in .net core you can use ConfigurationBuilder to read appsettings.json file.
You can implement like following.
appsettings.json sample
{
"option1": "value1_from_json",
"option2": 2,
"ConnectionStrings": {
"YourConnectionString": "............."
}
}
C# code sample
static class YourClass
{
public static IConfigurationRoot Configuration;
public static string GetConnectionString()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
var connectionString = Configuration["ConnectionStrings:YourConnectionString"];
}
}
In case you missed it - and because #Zuhair doesn't seem to want to post an answer - here is a copy paste of their solution (I missed this at first as it was only in a comment):
I found the solution. I changed the name of the Web.config to
app.config and I was able to get the connection string using:
System.Configuration.ConfigurationManager.ConnectionStrings["SHOPPINGCNN"].ConnectionString
The app.config file looks like this:
<?xml version="1.0"> encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SHOPPINGCNN" connectionString="server=.\SQLEXPRESS;integrated security=true;database=xxxxx" />
</connectionStrings>
</configuration>
You also need to install this NuGet package:
System.Configuration.ConfigurationManager
In my case I simply renamed the web.config containing the connectionString 'app.config' and it worked.
I realise this probably isn't a good long term solution but for mixed legacy projects - or to get a foot in the door to begin to learn .net Core it's very useful.
For .NET Core apps, the best way is to use the Configuration API. This is a very flexible way and, thanks to providers pattern, it allows to use a different sources, not only the most common appsettings.json file (that by the way just a JSON file and could be named in a random way):
File formats (INI, JSON, and XML)
Command-line arguments
Environment variables
In-memory .NET objects
An encrypted user store Azure Key Vault
Custom providers, which you install or create
Now about ConfigurationManager . At first, the .NET Core forced to forget about this class - it was not implemented and supported and the idea was to fully replace it by providing new Configuration API.
Moreover, the reality is that ASP.NET Core apps aren't hosted via IIS anymore (IIS works mainly as a reverse proxy now), and so the web.config is not so useful anymore (unless rare cases when you still need to define parameters specific to IIS configuration).
Though, after the .NET Standard 2.0 was provided, this System.Configuration.ConfigurationManager nuget package is available and brings back the ConfigurationManager class. It became possible due to new compatibility shim implemented in new .NET Core 2.0.
Regarding your case, it is difficult to say why you have 'null' as it not enough information:
it may be a wrong section in web.config
web.config may not be copied into your output/publishing folder
The problem here is that the OP has a data access layer project library that targets the full .Net Framework that contains code which depends on the ConfigurationManager. ConfigurationManager knows nothing about configuration in .Net Core and thus cannot access values from the default configuration implementation, i.e. appsettings.json. Some answers note that you can add an app.config file to the .Net Core client, but this smells if we don't hold our nose.
The better solution would be to inject the connection string into the data access library. Especially if that library will be used by multiple clients.
For everyone having problem reading web.config in asp.net core. After spending hours trying and failing.. then i came to a blog which stated
The .Net Core application uses appsettings.json instead of web.config
file.
so i suggest everyone to place files they want to read to appsettings.json and from there you can read. There are plenty of methods available. Just wanted to save time of those trying.
The only way i could use a web.config was reading the file as a XML, and inputing the values into a ConfigurationManager object using the .AddInMemoryCollection(list) method.
//ConfigurationManager.cs
using System;
using System.Collections;
using System.Xml;
using Microsoft.Extensions.Configuration;
namespace sql_util {
public static class ConfigurationManager {
public static IConfiguration AppSettings { get; }
static ConfigurationManager() {
var list = new List<KeyValuePair<string, string>>();
XmlDocument xd = new XmlDocument();
xd.Load(Directory.GetCurrentDirectory() + "\\" + "web.config");
if (xd.GetElementsByTagName("appSettings").Count > 0) {
XmlNodeList confs = xd.GetElementsByTagName("appSettings")[0].ChildNodes;
for (int y = 0; y < confs.Count; y++) {
if (confs[y].Attributes != null) {
if (confs[y].Attributes[0] != null) {
list.Add(new KeyValuePair<string, string>(confs[y].Attributes[0].Value, confs[y].Attributes[1].Value));
}
}
}
}
AppSettings = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddXmlFile(Directory.GetCurrentDirectory() + "\\Web.config", false, true)
.AddInMemoryCollection(list)
.Build();
}
}
}
//Web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="smtp_server" value="127.0.0.1" />
//envia_mail.cs
using ConfigurationManager = sql_util.ConfigurationManager;
namespace anotacoes {
public class envia_email {
private string SmtpServer = ConfigurationManager.AppSettings["smtp_server"];
added Web.config file to root as i used in .net framework project.
ConfigurationManager.AppSettings["MySQL"]; works fine.
I changed my Web.config to App.config and my issues resolved immediately.
You can still use an app.config for connection strings if needed. Include the nuget package System.Configuration.ConfigurationManager.
Then to get a connection string you'd do this:
System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString
As others have pointed out, connection strings are easily used from the appsettings.json file and this is the preferred method.

Use SQL Server database for code first migrations in ASP.NET MVC 4

I have been going around and around trying to use a SQL Server Database with code first migrations. For my latest attempt I:
created a new ASP.NET MVC 4 Web Application project (yes, I want to use this template)
created the model entities
created the data context class that inherits from dbcontext
Placed the following connection string in the Web.config residing in the application root:
add name="DefaultConnection" connectionString="Data Source=.\sqlexpress;Integrated Security=true;Initial Catalog=DatingAnAsianWoman;MultipleActiveResultSets=true;providerName=System.Data.SqlClient"
In the Package Manager I input:
Enable-Migrations -ContextTypeName UsersContext
Followed by:
Update-Database
At this point I am receiving the following error:
The connection string 'DefaultConnection' in the application's configuration file does not contain the required providerName attribute."
I would appreciate any help at all.
Ack! I didn't even realize providerName was to be specified as a separate key value!

SubSonic 2.2 encrypted connection string

I want to use SubSonic on a website. I put SubSonic.DLL in the asp.net Bin folder and I've generated some classes from SubSonic Commander and put them in a DLL that is also in Bin folder. The problem is the site I'm working on encrypts their connection strings. Can SubSonic 2.2 work with an encrypted connection string? How?
You can use local integrated security and avoid encryption because you have nothing to encrypt.
<add name="ConName" connectionString="Data Source=localhost;Initial Catalog=YourDataBase;Integrated Security=True;" providerName="System.Data.SqlClient" />
By default Subsonic did not have decryption, you can find the DataProvider.cs and add it by your self, or you can use the public SharedDbConnectionScope(DataProvider dataProvider, string connectionString) to make connections with the connection string after you decode it, but I do not think that this can help you in large scale.

Sharing Entity framework objects across projects?

I am having some refactor troubles, maybe someone knows why...
In one solution I have a WCF service. In another solution I have a RIA application. Since the SQL database between these two is identical, I wanted to create a separate project, in which to host the edmx file as well as a domain service. If I create the edmx file in the WCF project directly, and do the same on the RIA side, everything works fine.
But when I try to pull this edmx file into a separate project and add references to it I get all kinds of bizarre errors that my entity objects cannot be found. The WCF service itself seems fine, in that it references the edmx project and compiles just fine.
But the WCF client project, that has a service reference to the WCF service pukes on the entity references. Even adding the edmx assembly doesnt really help- some entities are found others are not. Very odd.
Anyone know what Im missing?
Copy the connection string across all your projects which attempt to use the model.
However, in the connection string, remove the OR'd Resource pointers.
e.g. Full Entity Connection
<connectionStrings>
<add name="AwesomeEntityModel"
connectionString="metadata=res://*/AwesomeEntityModel.csdl|res://*/AwesomeEntityModel.ssdl|res://*/AwesomeEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=NEILHIGHLEY.COM;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Trim it to the following;
<connectionStrings>
<add name="AwesomeEntityModel"
connectionString="metadata=res://*;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=NEILHIGHLEY.COM;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Based on what you've posted so far and taking it that you've ensured that the "edmx" project has a new namespace that is also used by the other projects.
If I'm reading what you've said correctly:
your WCF server references the "edmx"
project.
your WCF client references the WCF
server AND the "edmx" project.
It could be something as simple as circular referencing conflicts. Make sure that any "edmx" reference data within both of the other projects aren't public, just in case the client is picking up "edmx" data from the server project.
Also check whether missing items are left at the default accessibility of internal (when not defined).

Silverlight: How to setup ServiceReferences.ClientConfig when placed inside xap

I'm using a wcf service with my silverlight application. The location of the wcf service is stated in the ServiceReferences.ClientConfig file, and have to be changed to location where the application is installed.
However this file is included in the xap file, and is not something that can be easily changed when the application is deployed. Is there another way of doing the reference to the wcf service from the silverlight application? Or how do you change the ServiceReferences.ClientConfig in the xap file?
There may be better ways, which I'm open to using but this works for me and it's flexible.
In your Web Application's Web.config, add a variable in AppSettings and store the base URL, notice that I'm not storing the location of the SVC file, I will append that later. This is because I have multiple SVC that I usually point to. You may choose to do it differently.
<appSettings>
<add key="ServiceURI" value="http://localhost:64457/"/>
</appSettings>
In my Web Application's Web Page, add a param called InitParms, this allows you to add a list of key, pair values (seperated by comma that will be read by the XAP file)
<div id="silverlightControlHost">
<object data="data:application/x-silverlight," type="application/x-silverlight-2"
width="100%" height="100%" ID="Xaml1" >
<param name="InitParams" value="ServiceURI=<%= ConfigurationManager.AppSettings("ServiceURI") %>" />
In the Silverlight App.xaml.vb, load all the InitParms into a Resource or where ever you want
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
If e.InitParams IsNot Nothing Then
For Each k As Generic.KeyValuePair(Of String, String) In e.InitParams
Me.Resources.Add(k.Key, k.Value)
Next
End If
Then in any of my XAML files I can initialize the service with the configured URI, I have a method like this
Private Sub InitializeService()
Dim uri As String = App.Current.Resources("ServiceURI")
If uri Is Nothing OrElse uri = String.Empty Then
'if there is no value added in the web.config, I can fallback to default values
_client = New ServiceClient
Else
'Notice I hardcoded the location of the SVC files in the client and append there here, you may choose not to do this
Dim uri_withservice As String = uri & "svc/secure/Service.svc"
_client = New ServiceClient("CustomBinding_Service", New EndpointAddress(uri_withservice))
End If
End Sub
Excellent, with these suggestions, I managed to get my WCF ServiceReferences.ClientConfig data to be dynamically altered at application startup, with the Service URI read from web.config. This is possible by using the "web.config transformations" in VS2010.
Here a sample web.config.debug, showing how the ServiceURI is replaced when I choose "publish" for my web site.
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<...>
</connectionStrings>
<appSettings>
<add key="ServiceURI" value="http://my.location.com/myService.svc"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In my base web.config I then have the same key/value, pointing to the local service. No need to remember to change the ServiceURI every time i deploy to test/production. Great, I've been looking for that for a while now.
Found a solution in this blog.
http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=132
Here the wcf service endpoint is calculated from the location of the silverlight application
The solutions presented here all impractical in the sense that you modify your application to adapt to your config settings. This blog entry nailed it.