How can I prepare the configuration settings (App.config, maybe?) I need to use Enterprise Library Data Access Application Block in a C# console project?
Following is what I currently trying with an App.config in the console project. When I call DatabaseFactory.CreateDatabase(), it throws an exception which says:
"Configuration system failed to initialize"
<configuration>
<dataConfiguration>
<xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.
Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<enterpriseLibrary.databaseSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultInstance="Northwind"
xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
<databaseTypes>
<databaseType name="Sql Server" type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase,
Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" />
</databaseTypes>
<instances>
<instance name="Northwind" type="Sql Server" connectionString="Northwind" />
</instances>
<connectionStrings>
<connectionString name="Northwind">
<parameters>
<parameter name="Database" value="Northwind" isSensitive="false" />
<parameter name="Integrated Security" value="True" isSensitive="false" />
<parameter name="Server" value="local" isSensitive="false" />
<parameter name="User ID" value="sa" isSensitive="false" />
<parameter name="Password" value="sa1234" isSensitive="true" />
</parameters>
</connectionString>
</connectionStrings>
</enterpriseLibrary.databaseSettings>
</xmlSerializerSection>
</dataConfiguration>
</configuration>
Use the Enterprise Library Configuration Tool which is integrated into Visual Studio once you install the Enterprise Library - it will ensure your configuration XML is setup correctly.
Related
In my .Net 4.8 MVC application I'm trying to write logs with NLog. One of the field is traceId - ${aspnet-TraceIdentifier}, which suppose to be supported by NLog.Web.
So I followed the documentation and installed:
NLog v4.7.6
NLog.Extensions.Logging v1.6.5
NLog.Web v4.9.3
I placed my NLog.config in project's directory:
<?xml version="1.0" ?>
<nlog autoReload="true"
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true"
internalLogLevel="trace"
internalLogFile="c:\test\internal-nlog.txt">
<targets>
<target name="ws" xsi:type="WebService" protocol="JsonPost"
url="https://log-api.com/log/v1">
<parameter name="time" layout="${date}" />
<parameter name="level" layout="${level:upperCase=true}"/>
<parameter name="message" layout="${message}" />
<parameter name="host" layout="${machinename}" />
<parameter name="threadid" layout="${threadid}" />
<parameter name="ActivityId" layout="${activityId}" />
<parameter name="processId" layout="${processId}" />
<parameter name="threadid" layout="${threadid}" />
<parameter name="event-properties" layout="${all-event-properties}" />
<parameter name="exception" layout="${exception}" />
<parameter name="traceId" layout="${aspnet-TraceIdentifier}" />
<parameter name="environment" layout="Production" />
<parameter name="serviceName" layout="company-api2222222" />
<parameter name="session" layout="${aspnet-session}"/>
<parameter name="sessionId" layout="${aspnet-sessionid}"/>
<header name="X-License-Key" layout="${environment:LICENSE_KEY}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="ws" />
</rules>
</nlog>
According to the documentation, further configuration is not needed:
"Simply install the package. NLog will detect the extension automatically."
So, inside my controller I simply getting current class logger and logging:
NLog.LogManager.GetCurrentClassLogger().Info("Hello World");
But unfortunately, ${aspnet-TraceIdentifier} value is empty, and the worst thing is that I cannot find any documentation about how to make it works.
I tried to put extensions tag above targets in NLog.config file
<extensions>
<add assembly="NLog.Web"/>
</extensions>
and even tried this as well:
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
But nothing changed. Can anyone help? There is something that I am missing? maybe a configuration problem after all?
It's unclear if you're using ASP.NET or ASP.NET Core.
So an answer for both:
ASP.NET (non core)
For ASP.NET you should use <add assembly="NLog.Web"/> and the NLog.Web package
For ASP.NET with IIS needs IIS ETW feature enabled: https://learn.microsoft.com/en-us/iis/configuration/system.webServer/httpTracing/ (IIS7 - Win2008)
internally HttpWorkerRequest.RequestTraceIdentifier will be used.
ASP.NET Core
For ASP.NET Core you should use <add assembly="NLog.Web.AspNetCore"/> and the NLog.Web.AspNetCore package
NLog uses these for the traceindentifier:
ASP.NET Core 3-5: use System.Diagnostics.Activity.Current.Id (default) or HttpContext.TraceIdentifier
ASP.NET Core 1+2: HttpContext.TraceIdentifier
For ASP.NET Core 3-5 you maybe need ignoreActivityId. e.g. ${aspnet-TraceIdentifier:ignoreActivityId=true} - if true - HttpContext.TraceIdentifier will be used instead of System.Diagnostics.Activity.Current.Id
i have an entity model contained in a dll and i am writing some unit tests for it. when i run it it says the connection string can't be found in the application config.
Data:
model:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog" />
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" />
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
<connectionStrings>
<add name="efContext"
connectionString="metadata=res://*/Models.efModel.csdl|res://*/Models.efModel.ssdl|res://*/Models.efModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Production;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
the connection string is clearly in there.
When my test calls a function within the dll which is written like this
Public Function Find(ByVal SectionId As Integer) As Data.Section Implements ISection.Find
Dim SectId As String = SectionId.ToString()
Using dataContext As efContext = New efContext()
dataContext.Database.Connection.ConnectionString = _efConnectionString
Dim c As Data.Section = Nothing
c = (From cs In dataContext.Sections
Where cs.SectionId = SectId).FirstOrDefault()
Return c
End Using
End Function
it fails on dataContext.Database.Connection.ConnectionString = _efConnectionString
although when i debug it it already has the exception.
I tried duplicating the app config in the unit test project. I tried linking the app config as a shortcut. I have been stuck on this for about a full day now. I have tried just about every solution i have seen on stack overflow. it just doesn't seem to work.
I have even tried remaking the entity model and that didn't help either.
Any ideas?
I have created a wpp.targets file which does all the deployment in azure. I want to change the appsetting property of webconfig during deployment to azure. I found a sample in http://sedodream.com/PermaLink,guid,25ddd39e-59de-4e35-becc-de19dcc5e4ea.aspx , which uses the pubxml and parameters.xml. I want to use wpp.targets instead of pubxml.
<appSettings>
<!-- TODO: set this in the azure config -->
<add key="customer" value="xyz" />
</appSettings>
Need to update the customer value to "client" during deployment and web.config file should reflect the changes in the deployed folder like below
<appSettings>
<add key="customer" value="client" />
</appSettings>
ANSWER
I have achieved using the ProjectParametersXMLFile in msDeploy
msbuild Api.csproj /p:ProjectParametersXMLFile="c:\parameter.xml"/p:PublishSettingsFile=%publishFileLocation%
You can use parameter xml file
for example
<parameters >
<parameter name="customer" description="web Job queue name" defaultValue="sitetest1">
<parameterEntry kind="XmlFile"
scope="\\web.config$"
match="/configuration/appSettings/add[#key='customer']/#value" />
</parameter>
<parameter name="customer" description="web Job queue name" defaultValue="sitetest">
<parameterEntry kind="XmlFile"
scope="\\app.config$"
match="/configuration/appSettings/add[#key='customer']/#value" />
</parameter>
</parameters>
this file will change the appsettings when you deploy in azure based on default value you provide
I have an entity 4.0 model that is using a SqlServerCE database as its provider. I am trying to add a SQL Server Connection string so that, I can use choose to use either SQL Server or SQLCompact database.
Here is my Connection String
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<--<add name="SomeDBEntities" connectionString="metadata=res://*/SomeDB.csdl|res://*/SomeDB.ssdl|res://*/SomeDB.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=ABC.sdf"" providerName="System.Data.EntityClient" />-->
<add name="SomeDBEntities" connectionString="metadata=res://*/SomeDB.csdl|res://*/SomeDB.ssdl|res://*/SomeDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=Database;Initial Catalog=MyDatabase;Integrated Security=True;multipleactiveresultsets=True;"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> <parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>-->
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=Database;Integrated Security=True;multipleactiveresultsets=True" />
</parameters>
</defaultConnectionFactory>
<-- -->
When I attempt to query the Entity Model, I get the following error:
SqlCeCommand.CommandTimeout does not support non-zero values.
How do I set the provider from SqlServerCE to SqlClient?
The SqlServerCE provider is specified in the edmx file.
You need to create a Second SSDL File as it is described here : http://www.codeproject.com/Articles/82017/Preparing-an-Entity-Framework-model-for-multi-prov.aspx
Now and again im getting the following error
"Seems that the framework isn't configured properly. (isWeb != true and SessionScopeWebModule is in use) Check the documentation for further information"
and my web app crashes
my web.config is so..
<activerecord isweb="true">
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string_name" value="main" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
</config>
</activerecord>
any ideas, on what I need to change\add?
thank you
isWeb is case sensitive.
<activerecord isWeb="true">
From the source:
XmlAttribute isWebAtt = section.Attributes["isWeb"];
They aren't being nice about case :)