We use the following services, which are declared in the Web.config.
AspNetSqlMembershipProvider
AspNetSqlProfileProvider
AspNetSqlRoleProvider
We are using EntityFramework, and now have a requirement to programmatically set the connection string (it is fetched from the deployment environment, so Web.config cannot be used).
The question is, how can we continue to use these Providers, but have a programatic connection string?
We do not want to maintain multiple configs, and we do not want to leave credentials in the Web.config.
Thanks!
Related
I'm trying to configure an ASP.NET Core (2.1) application in the same IIS server with two different url/port, each url/port should use different DataBase connection e.g.:
http://localhost:55001 (use QA DB connection)
http://localhost:55002 (use Production DB connection)
This scenario is for using an intranet web application. A group of users will use the Production app. Another group of users will use the QA or Staging app. This two urls/db-connections must be enable in the same application at same time in order to have one main app (for policies, maintaining and machine resources consumption purposes) and two different databases.
What I've tried is to configure two appsettings.json files: the first (appsettings.Release.json) sets the production database connection string and a applicationUrl url/port. The second one (appsettings.QA.json) defines a different database connection string and different applicationUrl.
appsettings.Release.json:
{
"ConnectionStrings": {
"default": {
"ConnectionString": "<production-connection-string>",
"ProviderName": "Oracle.ManagedDataAccess.Client"
}
},
"applicationUrl": "https://localhost:55001",
...
}
appsettings.QA.json:
{
"ConnectionStrings": {
"default": {
"ConnectionString": "<qa-connection-string>",
"ProviderName": "Oracle.ManagedDataAccess.Client"
}
},
"applicationUrl": "https://localhost:55002",
...
}
Now here I'm a little bit lost, the next configuration settings I need to achieve are confusing and not clear for me. I don't even know if what I'm trying to achieve is possible or acceptable.
If I define an ASPNETCORE_ENVIRONMENT variable value for each appsettings[environment/scenario].json then I cannot use two values for this variable at the same time right?.
So I'm thinking that set and use the ASPNETCORE_ENVIRONMENT may not be the answear to my problem. Also I have to properly configure the startup and (maybe) program classes to achieve what I'm looking for.
So well at this point, any advice, guidance, code snippet or solution you guys may have will be very appreciated.
I think your question basically boils down to how to set different values for ASPNETCORE_ENVIRONMENT per app instance. Pretty much your best bet there for an IIS deployment is adding an <environmentVariables> section in <aspNetCore> in your web.config:
<aspNetCore ...>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
</environmentVariables>
</aspNetCore>
To have that handled automatically for you when publishing, you'll need to modify the .pubxml for your publishing profile and add something like:
<PropertyGroup>
<EnvironmentName>Staging</EnvironmentName>
</PropertyGroup>
That will then add the relevant <environmentVariables> setup to the generated web.config automatically when publishing.
For more information, see: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-2.2#configuration-with-webconfig
I am new to ADO.NET, I am using MYSQL with WCF.
I have a WCF project with ADO.NET, the SalesSolLIB project has reference of SalesSolWCF, and SalesSolView has reference of SalesSolLib project.
The problem is ADO.Net entities are created in WCF and when run, the error comes as although the name is present in web.config file.
"No connection string named 'SalesSolEntities' could be found in the application config file."
WebConfig File
<connectionStrings>
<add name="SalesSolEntities" connectionString="metadata=res://*/SalesSolDBModel.csdl|res://*/SalesSolDBModel.ssdl|res://*/SalesSolDBModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=surpavan;persist security info=True;database=salessol;old guids=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Code in WCF file:
Dim DB As New SalesSolEntities()
DB.Database.Connection.Open()
DB.Database.Connection.Close()
Return "test"
Code in SalesSolLib:
Dim service As New SalesSolWCF.ContactService
Dim errormsg As String = service.SaveContact(wcfcontact, IsNewContact)
However, through browsing, I found that connection string must be all the projects, however, why would I need to use WCF if connection string is needed in all projects, please advice how this can be solved, I don't want to put connection string in all projects or did I do wrong?
I am using Entity Framework 5.0 and the WCF Service Application as wcf project type.
You are calling your WCF services the wrong way. You do not add the reference by simply adding reference to DLL, you need to expose WCF endpoints via MEX, and then add reference by using "Add Service Reference".
What you've done now is that you're using your WCF not as a a service but just as a simple class that you make an instance off and then call it's methods. That is not how WCF services should be used, and that is why you get "connection string" missing error. Method in your class talks to database, so when you create instance of that class in other project, and call that method, that project must also have matching connection string. Basically it is all wrong, should not be done this way.
WCF services must be hosted either as self-hosted in your application or within IIS. I'd suggest that you go and take some basic WCF tutorial, otherwise what I just wrote will not make much of a sense to you.
I’ve been testing the new web deploy tool with VS 2012 but I have this issue:
I get this extra connection string added to my published web config that I don't currently have in my projects web config.
<add name="name" connectionString="name_ConnectionString" providerName="System.Data.SqlClient" />
Where could this be coming from? It seems like is a relic from past conn strings I've used..
Hope this explains my issue :0)
Thanks for any assistance
Quantum
It has to be coming from one of the web.config files.
Have you checked if a transform to your web.config is applied or not. Check web.release.config, web.debug.config file to check if the connection string is still there.
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).
on the web application, I am using a NHibernate helper that looks up the session that was opened in a httpmodule (and committed there also).
<property name="current_session_context_class">web</property>
In the console application, what do I do?
Your options are: "call" & "thread_static". Have a look at this for more detailed explanation on all available contexts:
http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session
In order to have your library work for both a web and a console application you have two options:
Based on a application setting in the App.config and in the web.config have the Session Factory built accordingly by setting the current_session_context_class property manually in the code and remove it from the hibernate.cfg.xml file.
Include a in the web application's web.config and in the app.config of the console application. This way you can have the current_session_context_class property set to different values. If I am not mistaken the in the web.config and in the app.config overrides the values of the hibernate.cfg.xml. If I am wrong then you will just have to include in the web.config and the app.config the complete and remove the hibernate.cfg.xml file from your library.