Can a non-SF project make use of the applicationmanifest configuration values? - c#

I've got a Service Fabric Application set up in the following way:
Solution
--SF Project
--ApplicationManifest.xml
--Stateless Project (uses app manifest values)
--Stateless Project (uses app manifest values)
--Class Library (used as a repository by the above two projects)
How can I enable the class library to make use of the ApplicationManifest.xml configuration file from the SF Project?
To allow the projects to be able to use the AppManifest for build/deployment, they simply need to be created like so:
How does a project that does not get added as a Service Fabric project make use of the applicationmanifest?
The Service Fabric projects are able to use the appmanifest by including parameters in settings.xml and servicemanifest (but non-SF projects cannot):

Option #1
If you need to access the parameters defined in your service's setting.xml, the next should work -
In your non-SF project, install
Microsoft.Extensions.Configuration and ServiceFabric.Extensions.Configuration NuGet packages
Wherever you decide to access the parameters, use the next code snippet -
var builder = new ConfigurationBuilder().AddFabricConfiguration("Config");
var configuration = builder.Build();
var section = configuration.GetSection("MyConfigSection");
var parameterValue = section["MyParameter"];
One note though - you will get access to only one SF service at a time. That's because AddFabricConfiguration() works by calling FabricRuntime.GetActivationContext(), which ties settings being loaded with the SF service you're calling a non-SF code from.
Option #2
Next option will work from any place where you could establish connection with the SF. Using code below, you could read any parameter passed into the app manifest -
var fClient = new FabricClient();
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/2011/01/fabric");
var manifest = XDocument.Parse(fClient.ApplicationManager.GetApplicationManifestAsync("YOUR_APP_TYPE_NAME", "YOUR_APP_TYPE_VERSION").Result);
var parameterValue = manifest.XPathSelectElement("/ns:ApplicationManifest/ns:Parameters/ns:Parameter[#Name='PARAMETER_NAME']", namespaceManager).Attribute("DefaultValue").Value;

Related

How can I execute a plugin from a test method?

I've added a test project to my solution where I want to test the integrations, and by that testing the plugins from my local machine. I've added Microsoft.Crm.Tooling.Connector and have a connection to my test instance. But I'm unsure on how and what the configuration and service is set up.
var crm = new CrmServiceClient(crmConnectionString);
crm.OrganizationServiceProxy.EnableProxyTypes();
var service = crm.OrganizationServiceProxy;
var unsecureConfig = "?";
var secureConfig = "?";
var plugin = new ExternalWorkorder_OnCreate(unsecureConfig, secureConfig);
plugin.ExecutePluginLogic(service?);
For executing the plugin, does the configuration matter? As long as i have the IServiceProvider, and how do I get that? Can I get it from CrmServiceClient? Or the OrganizationServiceProxy?
No you do not need t worry about secure and unsecure config.
Look at this article which will connect to dynamics and perform operations as expected.
You don't need the configuration if youre not counting on it in Plugin. But depending on how the plugin code is structured you will need to provide some even empty configuration.
I would recommend using FakeXrmEasy. https://dynamicsvalue.com/home
There are many examples on how to use the library.

how to remove app.config in application code

I am writing a Dll project in which I am using EnterpriseLibrary.Caching.NetCore for in memory caching. This package require a app.config. Now when I call this DLL in my console application, I am bound to add app.config in my console application also. So I need to remove app.config from my console application.
I think you can programmatically initialize the Enterprise Library Caching block by manually setting the Enterprise library container (IServiceLoccator) from a customized configuration. Thus, the application config file should not be read. For example :
var configSource = new DictionaryConfigurationSource();
var cacheSettings = new CacheManagerSettings();
configSource.Add(CacheManagerSettings.SectionName, cacheSettings);
var storageConfig = new CacheStorageData("NullBackingStore", typeof(NullBackingStore));
cacheSettings.BackingStores.Add(storageConfig);
var cacheManagerData = new CacheManagerData(
"Cache Manager",
60,
1000,
10,
storageConfig.Name);
cacheSettings.CacheManagers.Add(cacheManagerData);
cacheSettings.DefaultCacheManager = "Cache Manager";
cacheSettings.DefaultCacheManager = cacheManagerData.Name;
EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
EDIT: Note I have already used this kind of code for Enterprise Library on .NET Framework but I guess it should be the same on .Net core.

Publishing a shared appsettings file with .net core

I have a .net core solution that contains a database project (class library) and a console application. The database project contains EF Migrations and to do Add-Migration, most methods use a hard-coded connection string in one place or the other.
To avoid hard-coding (and/or duplication) I have created a shared appsettings.json file in the solution root and I use it in my Main method and the class library
In the console application
static void Main(string[] args)
{
var settingPath = Path.GetFullPath(Path.Combine(#"../appsettings.json"));
var builder = new ConfigurationBuilder()
.AddJsonFile(settingPath, false);
var configuration = builder.Build();
var services = new ServiceCollection()
.AddDbContext<MyContext>(options => options.UseSqlServer(configuration["ConnectionStrings:MyDatabase"]))
.BuildServiceProvider();
}
And in the class library to use migrations
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
var settingPath = Path.GetFullPath(Path.Combine(#"../appsettings.json"));
var builder = new ConfigurationBuilder()
.AddJsonFile(settingPath, false);
var configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder<MyContext>()
.UseSqlServer(configuration["ConnectionStrings:MyDatabase"]);
return new MyContext(optionsBuilder.Options);
}
}
This is working well for development purposes when I use dotnet run but when I publish the console application, it doesn't include the appsettings file. Other than running a powershell script as part of dotnet publish, is there any other cleaner way of including this file when the project is published?
IDesignTimeDbContextFactory is exactly for the purpose its name describes. You shouldn't be running migrations against your production database in the first place, and if you do, you should be generating specific migrations for production into your app (instead of the class library) and using your app for the migrations. See the docs on using a separate project for migrations. That, then, negates the need to share your appsettings.json. Just leave the connection string hard-coded in your factory, since it's only for development anyways.
Now, you might have an issue I suppose in a team environment. However, even if you're using something like SQLite, you can use project-relative paths that won't be developer-specific, and with LocalDB, you can use a normal SQL Server connection string to the MSSQLLocalDB instance, which will be same for every developer using Visual Studio. Regardless, even if you do need to specify the connection specifically by developer, at that point it would make more sense to use user secrets, anyways, since you wouldn't want that info be committed to source control. Otherwise, each developer would end up clobbering the other's copy of appsettings.json, and you'd have a mess on your hands.
Long and short, just hard-code the connection string in your factory, or if you can't or won't, use user secrets for the connection string. In either case, you do not need to share appsettings.json.
The way I've done this before is to specify the startup project when you run dotnet ef (with the -s switch - the options are at https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#common-options)
It gets messy quickly, and it's probably easiest to write some wrapper scripts for the project that deal with this kind of thing.

Retrieve a custom configuration section

I want to make a library that will be used by either an exe (app.config) or a website (web.config). If I create a custom configuration section, and load it using
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
CustomSection section = config.Sections.OfType<CustomSection>().FirstOrDefault();
it won't run on web site. It throws a exePath must be specified when not running inside a stand alone exe. exception, since it's not an exe. I can use something like:
CustomSection db = (CustomSection )ConfigurationSettings.GetConfig("CustomSection");
But that relies on the section being properly named. Is there a way to get all the sections and iterate through them?

ConfigurationManager.RefreshSection not working in c# windows service

I am having two projects one is basically a Windows service and second is class project in which I am doing my business processing. My App.config file is in Windows service project and in my Class project I am using below
ConfigurationManager.RefreshSection("appsettings");
string scheduledTime = ConfigurationManager.AppSettings["ScheduleTime"];
this setting is in my appsettings section of config file
I am using RefreshSection and also updating my app.config value in windows service project but its not getting updated at run time in my class project.
What is the catch in this ?
I'm having a little trouble myself with something similar, however I did come across something for AppSettings. Give this a shot:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
I think appsettings should be appSettings
If you want to get latest value use this code:
var appSettings = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location).AppSettings;
var mySetting = appSettings.Settings["keyOfSetting"].Value;
remember every time you need latest value, must use two line of code in same place!

Categories