StructureMap get appSettings value with XML configuration - c#

Is it possible to configure StructureMap in XML that it retrieves constructor parameter value from appSettings?
I am looking for similar XML configuration to this:
For<IService>().Use<Service>()
.Ctor<string>("serviceUrl").EqualToAppSetting("serviceUrl");
P.S. The reason for XML configuration is to use stub version of service in development and test environments, but real implementation in production.

Related

How to add a custom JSON file into IConfiguration based on a setting?

I am working on ASP.NET Core 6.0 WebAPI and I need to get data from a CRM system (Sales Logix) using Sage SData API. We have different CRM environments (Production, Staging, Development) and I want to be able to connect (or test) any environment from my WebAPI project.
For that to work, I would like to add a configuration key (to indicate a particular CRM environment) either in appsettings.json (or launchsetting.json). For example when setting is "crmEnvironment": "Development", I want to include a custom json file, named crm-dev.json. Similarly for "crmEnvironment": "Staging", I want to include crm-staging.json.
Each custom json file ideally contains the CRM Url, Username and Password.
Please tell me how can I conditionally add json config files as mentioned above, or is there any better approach of achieving similar results, considering security in mind. Best if I could have custom config files encrypted without having to shift away from the standards, just like we did for Web.Config files by inheriting ProtectedConfigurationProvider.
My questions is similar to How can I add a custom JSON file into IConfiguration?. But since Program.cs no longer uses the Main() method, I am wondering what would be the correct way to add custom json config files.
If this may work, I use this approach.
.AddJsonFile($"Config\\appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
And I will have the Json file created for each environment. You can set the environment variable value either via IHost context or may be, simplifying your need by using Environment variable values from your respective environment.

Consuming web service in class library

Scenario:
I am consuming a web service in my class library project and it generates a binding name and end point in app.config. If I reference the class library in my UI project, I also have to include the same configuration in web.config. My problem is I don't want to include this configuration in web.config because of the dependency. I want to use assembly as it own with out any dependency.
My solution approach:
When I create the instance of proxy class in the class library project it shows me constructor to pass binding and endpoint.
Example
wsProxy proxyClass = new wsProxy(System.ServiceModel.Channels.Binding binding, System.ServiceModel.Endpoint endpoint)
I was wondering if I can pass the same binding and endpoint that I have in app.config so that I don't have to include either in app.config and web.config.
Yes, you can create these classes without having matching configuration in the main .config file. Where you get that configuration is up to you; it could be App.config, a YML configuration file, a database, etc. As long as your code satisfies the constructor requirements for the classes you're instantiating, you'll be fine.
With WCF, everything defined in your configuration file can be done programmatically.
You just need to create the objects needed to instantiate your client. Depending on the WCF features you want your application to be leveraging, you'll need classes like EndpointAddress, AddressHeaderCollection, Uri, EndpointIdentity (DnsEndpointIdentity or SpnEndpointIdentity), Binding (WSHttpBinding, NetTcpBinding etc.). And you might want to have these objects populated from a decoupled, centralized configuration store such as a database.

NHibernate Configuration

My NHibernate configuration is set up using hibernate.cfg.xml, and now I am trying to implement Rhino.Seucrity, and for its configuration I need to retrieve a NHibernate.Cfg.Configuration instance. Now i was wondering if it is possible to retrieve the configuration instance that hibernate.cfg.xml produces?
Well, somewhere in your code you must be creating the Configuration object and building the ISessionFactory. Get it from there...

Using NHibernate inside a DLL

I would like to build a DLL (which should be accessed from a web service and possibly from another application through automation).
Is there any possibility using NHibernate inside this dll (so accessing the dll through automation would work) ?
I am already using NHibernate in a rich client application and it is very handy , but I have to make some changes to the app.config for this. All the other tutorials I see are using NHibernate directly on the web service - and are changing the web.config accordingly.
If you configure NHibernate in code rather than using app.config or web.config you should be able to avoid the problem you describe. For example you could use Fluent NHibernate's Fluent Configuration feature to configure NHibernate and thus avoid use of both web.config and hibernate.cfg.xml, which potentially could cause some trouble as well.
I am currently using this approach in a web app, where the data access layer is in a separate assembly and the web assembly has no reference to NHibernate and needs no modification to web.config, nor is a hibernate.cfg.xml file used.
Here is an example of a Fluent configuration:
sessionFactory = Fluently.Configure()
.Mappings(x => x
.FluentMappings.AddFromAssemblyOf<FooMap>()
.ConventionDiscovery.AddFromAssemblyOf<BarConvention>()
)
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(x => x
.Database("YourDbName")
.Server(#".\SQLEXPRESS")
.TrustedConnection())
.ShowSql())
.BuildSessionFactory();
Update:
The same goal should be possible to achieve using only standard NHibernate, by using their programmatic configuration possibilities. Instead of using the web.config or such to configure your database connection etc. you could pass an IDictionary instance to Configuration.SetProperties() when you create your session factory.
Something like this:
Configuration config = new Configuration();
IDictionary properties = new Hashtable();
properties["hibernate.dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
// more properties here ...
config.SetProperties(properties);
Chapter 3 of the docs has some info about this, but it is a bit on the short side.

How can I change the default configuration for WCF?

I have WCF Client initialized like this
MyServiceClient client = new MyServiceClient();
so it uses the app.config to read the endPoints.
I would like to dynamically change the default config file to a file I define.
I know I can open a configuration file like this:
Configuration myConfig = ConfigurationManager.OpenExeConfiguration
but how can I set myConfig to replace the default configuration?
Check out this article here: Read WCF Configuration from a Custom Location.
It basically involves creating a custom ServiceHost that will read the configuration from a different file which you can specify, rather than from web.config or app.config.
Here's another excellent article on using custom config files for WCF services hosted in IIS:
http://blogs.msdn.com/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx
Marc
I've seen something similar done by just reading values out of a custom config file (that was just opened and read with a standard XML parser). Then the values were plugged into the WCF configuration entries programmatically.
This was done because multiple projects in the same solution all read their WCF configuration entries out of the same file. I'm not sure why they went with that architecture, but in the end it worked just fine.

Categories

Resources