Updating connection string to LDAP with NHibernate and Oracle - c#

I've inherited an app that uses NHibernate and FluentNibernate to connect to an Oracle database. Unfortunately, I have no experience with NHibernate. The current connection string is like so:
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=0000)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));User Id={username};Password={password};
but now needs to switch over to LDAP (see the last code snippet).
My question, is it possible to make a change only to the web.config with an LDAP connection or will I need to add the Oracle.ManagedDataAccess and make some code changes?
It is my understanding that OracleClientConfiguration has been deprecated based on this page hence the part of the question about a new nuget package. Another note, there isn't a hibernate.cfg.xml file within the project (which I think is ok?) but wondering if this project isn't using the best practices for NHibernate. Any other hints or tips would be appreciated.
C# code:
private ISessionFactory GetSessionFactory()
{
var connectionString = LoginHelper.GetConnectionString(ConnectionStringSetting, LoginInfoSetting);
var iSessionFactory
= Fluently
.Configure()
.Database(OracleClientConfiguration.Oracle10.ConnectionString(connectionString))
.Mappings(e => e.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
.BuildSessionFactory();
return iSessionFactory;
}
Web.config (there no other references to NHibernate in the Web.config. There are some references to dependentAssembly in other dll.config files):
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<configSections>
<configuration>
What I (think I) need in the Web.config is something like so:
<oracle.dataaccess.client> <!-- or <oracle.manageddataaccess.client>? -->
<version number="*">
<LDAPsettings>
<LDAPsetting name="DIRECTORY_SERVERS" value="" />
<LDAPsetting name="DIRECTORY_SERVER_TYPE" value="" />
<LDAPsetting name="DEFAULT_ADMIN_CONTEXT" value="" />
</LDAPsettings>
<settings>
<setting name="NAMES.DIRECTORY_PATH" value="" />
<setting name="LDAP_ADMIN" value="" />
</settings>
</version>
</oracle.dataaccess.client>
EDIT 1:
Contents of ldap.ora file:
DIRECTORY_SERVERS=(ldap:XXX)
DEFAULT_ADMIN_CONTEXT="dc=Oracle,dc=com"
DIRECTORY_SERVER_TYPE=ad

Here is what I did in order to get this to work...
In the Oracle folder structure, navigate to the Network folder.
Add the following files (should be able to copy and paste another folder down called Sample):
sqlnet.ora
ldap.ora
tnsnames.ora
sqlnet.ora (the key here was adding LDAP to the beginning of the list):
SQLNET.AUTHENTICATION_SERVICES= (NTS)
NAMES.DIRECTORY_PATH= (LDAP, TNSNAMES, EZCONNECT)
ldap.ora:
DIRECTORY_SERVERS=(ldap:XXX:XXX)
DEFAULT_ADMIN_CONTEXT="dc=oracle,dc=com"
DIRECTORY_SERVER_TYPE=XX
tnsnames.ora:
No change was required; left as default
Connection string:
<add name="connection" connectionString="Data Source=YourDataSource"/>
<!-- might need to add a username/password -->

According documentation it should be this:
<LDAPsettings>
<LDAPsetting name="DIRECTORY_TYPE" value="AD" />
<LDAPsetting name="DEFAULT_ADMIN_CONTEXT" value="dc=Oracle,dc=com"/>
</LDAPsettings>
Or use
<settings>
<setting name="TNS_ADMIN" value="C:\oracle\work"/>
</settings>
and specify LDAP settings in C:\oracle\work\ldap.ora file (in conjunction with C:\oracle\work\sqlnet.ora file).
I don't think it is still valid but have a look at ODP.NET Managed library does resolve alias, but 32-bit library does
You can modify the config file with a simple text editor or use the config tool OraProvCfg.exe. Would be like this:
OraProvCfg.exe /action:config /product:odpm /frameworkversion:v4.0.30319 /providerpath:C:\oracle\product\12.1\Client_x64\odp.net\managed\common\Oracle.ManagedDataAccess.dll /set:settings\TNS_ADMIN:C:\oracle\network\admin
OraProvCfg.exe /action:config /product:odpm /frameworkversion:v4.0.30319 /providerpath:C:\oracle\product\12.1\Client_x64\odp.net\managed\common\Oracle.ManagedDataAccess.dll /set:LDAPsettings\DIRECTORY_TYPE:ad
Note, you have a OraProvCfg.exe for 32-bit (x86) and for 64-bit. Run the one which matches to your application, (or simply run both).

Related

Custom config elements collection in Web.config - Using a key to choose

Intro
I'm developing a WebApp built on C# ASP.NET.
I've been researching creating a "Custom Configuration" section with child elements in the Web.config file, and I've hit a bit of a snag when it comes to consuming the keys/values in the data.
I seem to be going round in circles and I don't know how to tackle the issue I'm having.
Situation
I have a few different Connection Strings defined in the Web.Config file, in the <connectionStrings> section. They are for dev, test, and live databases.
<connectionStrings>
<add name="connectionOne" connectionString="..." providerName="..." />
<add name="connectionTwo" connectionString="..." providerName="..." />
<add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>
The WebApp is currently hard-coded to use one of these connection strings - if I need to change which one to use, I need to re-compile.
Desired Functionality
I'd like to define a section in the Web.config, let's say DbSettings.
In that, I'd then like to be able to define some child elements for, let's say DbSettings, in which I could define dbConnectionName, foo, bar etc. as attributes.
For example:
<dbSettings>
<dbSetting key="DbSetting1"
dbConnectionName="connectionOne"
foo="fooOne"
bar="barOne" />
... and so on
</dbSettings>
Then, perhaps in the <appSettings> section, define which of these DbSettings elements I want to use to get the settings from:
<appSettings>
<add name="dbSettingsKey" value="DbSetting1" />
</appSettings>
Desired Web.config section
Here is a fuller example of what I'd imagine my Web.config file to look like:
Connection Strings
<connectionStrings>
<add name="connectionOne" connectionString="..." providerName="..." />
<add name="connectionTwo" connectionString="..." providerName="..." />
<add name="connectionThree" connectionString="..." providerName="..." />
</connectionStrings>
App Settings
<add key="dbSettingsKey" value="DbSetting1" /> // or 2, or 3 etc.
DbSettings (custom section)
<dbSettings>
<dbSetting key="DbSetting1"
dbConnectionName="connectionOne"
foo="fooOne"
bar="barOne" />
<dbSetting key="DbSetting2"
dbConnectionName="connectionTwo"
foo="fooTwo"
bar="barTwo" />
<dbSetting key="DbSetting3"
dbConnectionName="connectionThree"
foo="fooThree"
bar="barThree" />
</dbSettings>
My question...
How the devil am I going to get this desired functionality in the C# code?
I've read loads on "creating your own custom section", and similarly "creating a custom config collection". But, I just can't seem to glue it all together to apply for my situation.
I'd like to be able to have a class (like the one I'm using at the moment with the hard-coded strings), which I can reference necessary properties (as I am doing, at the moment) - and then the code can dynamically load the correct settings at run-time from the sections I've described above.
As always, thank you in advance for your suggestions and help.
I agree with the comments. The way this is usually done is you deploy a different web.config to each environment. When your deployment group (or you) deploys, you deploy everything EXCEPT the web.config unless you have changes to push.
In answer to your other question, adding a custom section is not trivial. It's quite a bit of work. Custom section handler which requires a whole bunch of configuration element classes and a bunch of configuration element collection classes... and then, if you want it to "work" correctly, you also need to create a schema and register that with the IDE, etc.
For your particular case, I'd just do it the "normal" way :).

Castle Windsor / ActiveRecord / NHibernate: How to intercept/modify connection string

I have consolidated the connection string information for a number of C# .NET solutions that are in my possession. Previously, each project was storing its connection string in its own format, requiring me to modify several files for each installation of the software.
Only one remaining solution is giving me trouble. This particular solution uses Castle Windsor 2.0, ActiveRecord 2.0 and NHibernate 2.1. The code reads its configuration from an XML file. I wish to remove the connection string from the config file and set it programmatically in the code.
Here is the relevant section of code that initiates Windsor:
windsorContainer = new WindsorContainer(new XmlInterpreter(xmlFileName));
windsorContainer.Resolve<IWindsorConfigurator>().Configure(windsorContainer);
logger = windsorContainer.Resolve<ILogger>();
Here are the contents of the XML file:
<?xml version="1.0"?>
<configuration>
<properties>
<connectionString>Server=*****;Database=*****;User Id=*****;Password=*****</connectionString>
</properties>
<facilities>
<facility id="logging" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" loggingApi="log4net" configFile="Configs/log4net.config" />
<facility id="atm" type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement" />
<facility id="arfacility" type="Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility, Castle.Facilities.ActiveRecordIntegration" isDebug="false" isWeb="false">
<!-- Configure the namespaces for the models using Active Record Integration -->
<assemblies>
<item>ChronoSteril.Application</item>
</assemblies>
<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" value="#{connectionString}" />
<add key="hibernate.cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="hibernate.expiration" value="60" />
</config>
</facility>
</facilities>
<components>
<component id="windsorConfigurator" service="ChronoSteril.Application.IWindsorConfigurator, ChronoSteril.Application" type="ChronoSteril.WinApp.ClarionIntegrationWindsorConfigurator, ChronoSteril.WinApp" />
</components>
I am not familiar with Windsor. During my Google tour, I did see some code that adds facilities programmatically, but those examples were not valid for my version of Windsor (I assume).
Question: Can anyone guide me in removing the connection string information from the XML file and allow me to set it in the code?
Thank you!
I managed to accomplish my intention. It is not ideal, but will work until the code base is rewritten. (I cannot wait to drop the existing code like a bad dream.)
Patrick's comment, under my initial question, let me to refine my search criteria, which yielded the thread located here.
My XML file remains the same, except that I use bogus values for the connection string information. I will never need to modify these, and they do not reveal any valid connection information. This was my intention. I still have not discovered how to successfully remove the ActiveRecord configuration from the XML file and configure using code.
I now call a method that contains the following code:
ISessionFactoryHolder sessionFactoryHolder = ActiveRecordMediator.GetSessionFactoryHolder();
NHibernate.Cfg.Configuration configuration = sessionFactoryHolder.GetConfiguration(typeof(ActiveRecordBase));
connectionString = ReadConnectionString();
configuration.SetProperty("connection.connection_string", connectionString);
This works for me. I hope that it can also help someone else who is in the same position as I was.

Visual Studio Express 2013 WPF warning: "The 'configuration' element is not defined" [duplicate]

I'm doing some work in Visual Studio 2012 Express Edition. I have added an App.config XML file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
The first thing that happens is a warning comes up that says "The 'configuration' element is not declared". Does anyone know why this is happening? It looks like elements can not be declared inside of until this is resolved.
Thanks!
This is the entire XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Version" value="779" />
<add key="TimeOut" value="60000" />
<add key="LogFileName" value="Log.txt" />
<!-- your Developer Id with eBay -->
<add key="Environment.DevId" value="" />
<!-- your Application Id with eBay -->
<add key="Environment.AppId" value="" />
<!-- your Application Certificate with eBay -->
<add key="Environment.CertId" value="" />
<!-- API Server URL -->
<!-- For production site use: https://api.ebay.com/wsapi -->
<!-- For Sandbox use: https://api.sandbox.ebay.com/wsapi -->
<add key="Environment.ApiServerUrl" value="https://api.sandbox.ebay.com/wsapi" />
<!-- EPS Server URL -->
<!-- For production site use: https://api.ebay.com/ws/api.dll"/-->
<add key="Environment.EpsServerUrl" value="https://api.sandbox.ebay.com/ws/api.dll" />
<!-- eBay Signin URL -->
<!-- For production site use: https://signin.ebay.com/ws/eBayISAPI.dll?SignIn -->
<!-- https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn -->
<add key="Environment.SignInUrl" value="https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn" />
<!-- ViewItem URL -->
<!-- For production site use: http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item={0} -->
<add key="Environment.ViewItemUrl" value="http://cgi.sandbox.ebay.com/ws/eBayISAPI.dll?ViewItem&item={0}" />
<!-- token is for both API server and EPS server -->
<add key="UserAccount.ApiToken" value="" />
<!-- eBay site ID -->
<add key="UserAccount.eBayUserSiteId" value="0" />
<add key="logexception" value="true"/>
<add key="logmessages" value="true"/>
<add key="logsdkmessages" value="true"/>
<add key="logsdk" value="true"/>
<add key="logfile" value="Log.txt"/>
<!-- Rule Name-->
<add key="RuName" value=""/>
<!-- Set this if you access eBay API server behind a proxy server-->
<add key="Proxy.Host" value =""/>
<add key="Proxy.Port" value =""/>
<!-- set proxy server username/password if necessary-->
<add key="Proxy.Username" value=""/>
<add key="Proxy.Password" value=""/>
Go to XML menu (visual studio top menu item) choose schemas and find for DotNetConfig.xsd and choose Use this schema.
Your problem will resolve for sure
<configuration xmlns="schema URL">
<!-- configuration settings -->
</configuration>
do changes,like above & try
I had the same issue. It is not an error, it is simply a warning; so your application should still compile. I used the following simple config file and the warning is still produced.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime
version="v4.0"sku=".NETFramework,
Version=v4.5"/>
</startup>
</configuration>
It is an issue that has been raised on the MSDN website, but it does not seem to have been satisfactorily resolved. See link below:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/18a1074f-668f-4fe3-a8d9-4440db797439
I had to
-> Go to XML menu (visual studio top menu item) choose schemas and select DotNetConfig.xsd AND RazorCustomSchema.xsd AND EntityFrameworkConfig_6_1_0.xsd
I just had this warning popup inside an autogenerated xml file while working on a xaml project.
Using Debug->Clean Solution and Debug->Rebuild Solution fixed it. Might want to try that before getting fancy with the schemas.
Visual Studio 2013 Express Edition is missing the DotNetConfig.xsd (https://connect.microsoft.com/VisualStudio/feedback/details/817322/dotnetconfig-xsd-files-not-present-in-vs-2013-express-for-desktop).
So to get rid of the warning in VS 2013 Express:
get a copy of DotNetConfig.xsd from another system or from the web (I used https://gist.github.com/eed3si9n/5dd7dd98ad2b3f668928b23477de35a3)
download to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas
add the schema following Ramakrishna's answer
The warning should be gone.
Choose use this schema. DotNetConfig.xsd
XLM Menu..... Visual Studio
Works perfectly.
I was having less space on my drive which might have resulted in incomplete loading of my application solution. This "the-configuration-element-is-not-declared" problem got solved after i created some space on my drive.
I also got the same warning. After thinking about for some time I realized my error working with SQL (MS SQL).
Warning: the 'configuration' element is not declared
Using C#
App.Config code:
<connectionStrings>
<add name="dbx" connectionString="Data Source=ServerNameHere;Initial Catalog=DatabaseNameHere;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
*this calls out the database name in the connectionStrings, when I plugged in my SQL code as a practice I always use the database name, schema, then table. This practice didn't carry over well in Visual Studio as I am a beginner. I removed the db name from my SQL syntax and only called from the schema, data table. This resolved the issue for me.
Form.CS:
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [DatabaseName].[Schema].[TableName] WHERE [MEPeriod] = '2020-06-01'", con))
Updated to:
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [Schema].[TableName] WHERE [MEPeriod] = '2020-06-01'", con))
This worked for me, I hope this is found as useful.

Edit App.Config name-value pairs from a separate tool

I am trying to create a simple tool for a service person to update a few entries in the App.Config of a different program. The App.Config file contains custom parameters used upon initialization of our program.
Since the App.Config contains many sensitive items a tool is needed to ensure only certain parameters are changed. Thus, the reason not to allow them to edit the App.Config directly.
My questions:
How can I access the name-value pairs from the config sections of an App.config from a separate program?
Which is better suited for the UI: Winforms or WPF? Are their controls that make it easy to add more entries in the future?
The tool should allow the user to set either a String, int, double or Boolean.
Here is the structure of the App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Settings">
<section name="Section1" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section2" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section3" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section4" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<Settings>
<Section1>
<add key="NAME_STRING" value="Some String"/>
</Section1>
<Section2>
<add key="NAME_INTEGER" value="10"/>
</Section2>
<Section3>
<add key="NAME_DOUBLE" value="10.5"/>
</Section3>
<Section4>
<add key="NAME_BOOLEAN" value="true"/>
</Section4>
</Settings>
... Omitted ...
</configuration>
In the program which uses the App.Config itself, I can easily change the values like so:
NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("Settings/Section1");
Is there a similar way to do this from a separate program after loading the App.Config?
An answer to Question 1: An app.config file is an XML file. It might be easiest to load it as an XML document and modify that programmatically, followed by a save, than to use System.Configuration classes.
ETA: I believe it can be done with ConfigurationManager. Look at the OpenMappedExeConfiguration method. There's a good example there.
You could treat the app.config file as a normal XML file. Use either XDocument or XmlDocument to load the file.
Then use XPath or Linq to XML to find the name-value pairs.
As for Windows.Forms vs. WPF, its a design decision. Both have good and bad points.
[Update]
If you still want to use System.Configuration, you can use the ConfigurationManager.OpenExeConfiguration to get access to the other program's app.config file. This returns a Configuration object, which has a GetSection method.

C# Optional fields in application settings

Is there a way to create some optional fields in application settings. For example for one client we need some client based settings in the settings file, something like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<userSettings>
<setting name="Client_1_out_folder" serializeAs="String">
<value>c:\</value>
</setting>
<setting name="Some_other_setting" serializeAs="String">
<value>True</value>
</setting>
...
And for the other client we dont need the Client_1_out_folder at all so to keep the config file clean would be nice to remove it from the config file all together. So for client 2 that part of config file would look like:
<?xml version="1.0"?>
<configuration>
<configSections>
<userSettings>
<setting name="Some_other_setting" serializeAs="String">
<value>True</value>
</setting>
...
Create a custon configuration section for your settings. Then on the configurationsection class, mark the property as "IsRequired=false" to make that property optional.
[ConfigurationProperty("frontPagePostCount"
, DefaultValue = 20
, IsRequired = false)]
You can create a class which inherits from ConfigurationSection.
Then, you can do practically whatever you want. It's much more powerful than the user settings.
MSDN: How to: Create Custom Configuration Sections Using ConfigurationSection
You can extend ASP.NET configuration
settings with XML configuration
elements of your own. To do this, you
create a custom configuration section
handler. The handler must be a .NET
Framework class that inherits from the
System.Configuration.ConfigurationSection
class. The section handler interprets
and processes the settings that are
defined in XML configuration elements
in a specific section of a Web.config
file. You can read and write these
settings through the handler's
properties.
The article says "ASP.NET", but it's not just for ASP.NET. It works equally well for WinForms.
I recommend creating your own configuration sections with Configuration Section Designer.
Unfortunately this tool isn't compatible with VS2010 but it is so very helpful that I keep using VS2008 to use it. Either way you create an extra assembly for the configuration section handler so you can use VS2008 only for this assembly and build the rest of the solution with VS2010. So this isn't a huge drawback at all.
There is also a good sample about create a custom configuration sections.
I hope it can help you...
app-config-and-custom-configuration-sections
Put those common settings in a .config file, and refer it in a special config file.
<!-- in general.config -->
<appSettings>
<add key="common1" value="something"/>
<add key="common2" value="something else"/>
</appSettings>
<!-- in client1.config -->
<appSettings file="general.config" >
<add key="specialKey1" value="for client 1 only"/>
</appSettings>
<!-- in client2.config -->
<appSettings file="general.config" >
<add key="specialKey2" value="for client 2 only"/>
</appSettings>
The use of custom configuration sections is a good idea, and you can then code for the entry to be required. That is a nice and clean way to handle this problem.
However, you could also handle this by a class that picks up these details, and tests for the existence ( or otherwise ) of this, having them all still in the usersettings section. So your main code would access the setting from the class:
if(Settings.HasClient)
//use Settings.Client;
Process(Settings.OtherSetting);
Depending on how you need to use them. Within Settings Constructor, you would access the settings directly.
I suspect there is something missing from your question.
If client 2 does not require the "Client_1_out_folder" setting and does not try and retrieve it at run time, you should be able to simply remove it, without having to make any other changes.
Have you tried doing so?

Categories