I tried getting value from my appsettings.json which is modeled like this:
"ConfigurationModel": {
"RfidAddress": "172.23.19.73",
"BaudRate": "152000",
"DataManagerTimeOut": "32000"
}
Then I created a POCO like so:
public class ConfigurationModel
{
public string RfidAddress { get; set; }
public int BaudRate { get; set; }
public int DataManagerTimeOut { get; set; }
}
In my Startup.cs I added the ConfigurationModel in the services like so:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.Configure<ConfigurationModel>(Configuration.GetSection("configurationModel"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSignalR();
}
I created a class that utilized this settings like this:
public class RfidClass
{
private readonly ConfigurationModel _configurationModel;
public RfidClass(IOptions<ConfigurationModel> configurationModel)
{
_configurationModel = configurationModel.Value;
}
public void sas()
{
Console.WriteLine(_configurationModel.RfidAddress);
}
}
Then in my Program.cs I need to call that class that I have just created like this:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
SetRfid();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static void SetRfid()
{
var rfidClass = new RfidClass(); <-- MISSING ARGUMENT
}
}
How can I pass the needed parameter to instantiate my RfidClass?
You should be able to extract the value by setting the result from the .Build() as follows:
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
host.Run();
var config = host.Services.GetService<IConfiguration>();
var configSection = config.GetSection("ConfigurationModel");
var configModel = configSection.Get<ConfigurationModel>();
SetRfid(configModel);
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static void SetRfid(ConfigurationModel configModel)
{
var rfidClass = new RfidClass(configModel); <-- ADDED
}
}
I'm bit scared to say that Program and Startup classes are not meant to do such things. Usually such operations are called from some other classes, i.e. Controller classes. Post this, you can use dependency injection to pass the objects. Here is the code to do that:
public void ConfigureServices(IServiceCollection services) {
... services.Configure(Configuration.GetSection("configurationModel")); services.AddSingleton(m => { return new RfidClass(m.GetService>()); }); ... }
And here is the controller code:
public HomeController(ILogger<HomeController> logger, RfidClass rfidClass)
{
...
}
You appear to have everything set up correctly to use the Options Pattern, but are then trying to new up RfidClass rather than inject the options into a class like a Controller, View, Handler, or Middleware. These classes are unique in the sense that the framework can inject services into them.
public class HomeController : Controller
{
private readonly RfidClass _rfidClass;
public HomeController(IOptionsMonitor<ConfigurationModel> options)
{
_rFidClass= options.CurrentValue;
}
public IActionResult Index()
{
var rfidAddress = _rfidClass.rfidAddress;
var baudRate = rfidClass.BaudRate;
// do stuff.
return View();
}
}
There is some great information int he microsoft documentation on utilizing the options pattern here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1.
Related
I am trying to convert some code from net core api to class library.
I am stuck how to use HttpClientfactory.
Normally the httpclientfactory can be configured in program.cs or Startup like
services.AddHttpClient("abc", xxx config).
How to do configurations in class library for Httpclientfactory.
In your library add an extension method for IServiceCollection to "enable" it in the main project.
In the library:
public static class ServiceCollectionExt
{
public static void AddYourStaff(this IServiceCollection services)
{
services.AddHttpClient("xxx", client =>
{
//your staff here
});
services.AddSingleton<ISomethingElse, SomethingElse>();
}
}
Then in your Startup just call it:
services.AddYourStaff();
UPDATE: As the author described, he's working on the plugin based application. In that case you need some kind of convention, for instance:
each plugin library must have a static class called Registration with the method Invoke(IServiceCollection sc, IConfiguration config)
Then in your Startup you can iterate through all plugin libraries and call their Registration.Invoke(sc, config) using reflection:
foreach(var pluginAssembly in plugins)
{
pluginAssembly
.GetType("Registration")
.GetMethod("Invoke")
.Invoke(null, new object[] {services, Configuration});
}
You could try as below:
public class HttpClientUtil
{
private static IServiceProvider serviceProvider { get; set; }
public static void Initial(IServiceProvider Provider)
{
if (Provider == null)
{
IHostBuilder builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(services =>
{
services.AddHttpClient("client_1", config =>
{
config.BaseAddress = new Uri("http://client_1.com");
config.DefaultRequestHeaders.Add("header_1", "header_1");
});
services.AddHttpClient("client_2", config =>
{
config.BaseAddress = new Uri("http://client_2.com");
config.DefaultRequestHeaders.Add("header_2", "header_2");
});
});
serviceProvider = builder.Build().Services;
}
else
{
serviceProvider = Provider;
}
}
public static IHttpClientFactory GetHttpClientFactory()
{
if(serviceProvider==null)
{
Initial(serviceProvider);
}
return (IHttpClientFactory)serviceProvider.GetServices<IHttpClientFactory>();
}
}
you could get the instance of httpclinetfactory through the interface
I set to indent JSON in Startup class, but how do I retrieve the formatting value from a controller?
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddWebApiConventions()
.AddJsonOptions(options=> options.SerializerSettings.Formatting=Newtonsoft.Json.Formatting.Indented);
}
}
public class HomeController : Controller
{
public bool GetIsIndented()
{
bool isIndented = ????
return isIndented;
}
}
You can just inject an instance of IOptions<MvcJsonOptions> into your Controller, like so:
private readonly MvcJsonOptions _jsonOptions;
public HomeController(IOptions<MvcJsonOptions> jsonOptions, /* ... */)
{
_jsonOptions = jsonOptions.Value;
}
// ...
public bool GetIsIdented() =>
_jsonOptions.SerializerSettings.Formatting == Formatting.Indented;
See the docs for more information about IOptions (the Options pattern).
If all you care about is the Formatting, you can simplify slightly and just use a bool field, like so:
private readonly bool _isIndented;
public HomeController(IOptions<MvcJsonOptions> jsonOptions, /* ... */)
{
_isIndented = jsonOptions.Value.SerializerSettings.Formatting == Formatting.Indented;
}
In this example, there's no need for the GetIsIndented function.
One option is to create a class where you declare the current configuration values
public class MvcConfig
{
public Newtonsoft.Json.Formatting Formatting { get; set; }
}
Then instantiate it in the configure method where you also register the class as a singleton
public void ConfigureServices(IServiceCollection services)
{
var mvcConfig = new MvcConfig
{
Formatting = Newtonsoft.Json.Formatting.Indented
};
services.AddMvc()
.AddWebApiConventions()
.AddJsonOptions(options=> options.SerializerSettings.Formatting=mvcConfig.Formatting);
services.AddSingleton(mvcConfig);
}
Then inject it in the controller and use it
public class HomeController : Controller
{
private readonly MvcConfig _mvcConfig;
public HomeController(MvcConfig mvcConfig)
{
_mvcConfig = mvcConfig;
}
public bool GetIsIndented()
{
return _mvcConfig.Formatting == Newtonsoft.Json.Formatting.Indented;
}
}
Is it possible to access an non-static object in the C# REST API controller?
For description:
I want to program a REST-API for an project. The data comes from a database. But I dont want do ask the database every time a request is triggered. So I have an Object "gen" that talks to the database and buffers the result.
I create the object direct from start and it holds itself alive and refreshes his internal data.
But how can I give the object to the REST-Controller (ValuesController) ?
I dont want to create the object every time someone wants new data.
Examplecode:
namespace GenAPI
{
public class Program
{
public static void Main(string[] args)
{
GenAPI gen = new GenAPI(); // gen.table is the object of intrest
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var wh = WebHost.CreateDefaultBuilder(args);
wh.UseUrls("http://*:4999");
wh.UseSetting("windowsAuthentication", "false");
wh.UseSetting("anonymousAuthentication", "true");
var iwh = wh.UseStartup<Startup>().Build();
return iwh;
}
}
public class Startup
{
public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}
}
namespace GenAPI.Controllers
{
[Route("")]
public class ValuesController : Controller
{
//[HttpGet]
//public IEnumerable<string> Get()
//{
// return new string[] { "value1", "value2" };
//}
[HttpGet("{url}")]
public IEnumerable<string> Get(string url)
{
List<string> result = new List<string>();
//foreach (var r in gen.table.Rows) // <-- Here I want do get the data
// result.Add(r[0]);
return result;
}
[HttpPost]
public void Post([FromBody]string value)
{
}
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
We had the same requirement and what we did was create a Global class with a static property that was the cached data:
public class Global
{
private static CacheObject _cache;
public static CacheObject cacheObject
{
get
{
if(_cache == null)
{
InitializeCacheObject();
}
return _cache;
}
set
{
_cache=Value;
}
}
public void InitializeCacheObject()
{
//get your data from db
CacheObject = db.getData();
}
}
in your controller, you can do this:
var model = Global.CacheObject.Rows.ToList();
I have a new Asp.Net core application that has the following entry in the appsettings.json file:
{
"DatabaseConnections": {
"DatabaseUri": "https://localhost:8081",
"ApplicationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"DatabaseName": "MyDatabase"
}
}
I'm attempting to pull the data out to use during the ConfigureServices method, using the .Bind method:
public class DatabaseConnections
{
public string DatabaseUri { get; set; }
public string ApplicationKey { get; set; }
public string DatabaseName { get; set; }
}
private DatabaseConnections databaseSettings;
private DatabaseConnections DatabaseSettings
{
get
{
if (databaseSettings == null)
{
databaseSettings = new DatabaseConnections();
Configuration.Bind(databaseSettings);
}
return databaseSettings;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IDocumentClient>(
new DocumentClient(
new Uri(DatabaseSettings.DatabaseUri),
DatabaseSettings.ApplicationKey));
}
However, when I perform the binding, the settings are all set to null. But if I try to do it without the model binding, it seems to work fine:
public void ConfigureServices(IServiceCollection services)
{
var databaseSettings = Configuration.GetSection("DatabaseConnections");
services.AddSingleton<IDocumentClient>(
new DocumentClient(
new Uri(databaseSettings.GetValue<string>("DatabaseUri")),
databaseSettings.GetValue<string>("ApplicationKey")));
}
What am I doing wrong?
You can either build a service provider or use string.
Configuration.GetSection("DatabaseConnections:DatabaseUri").Value
For example,
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<DatabaseConnections>(
Configuration.GetSection("DatabaseConnections"));
var sp = services.BuildServiceProvider();
var databaseConnections = sp.GetService<IOptions<DatabaseConnections>>();
services.AddSingleton<IDocumentClient>(
new DocumentClient(new Uri(databaseConnections.Value.DatabaseUri)),
databaseConnections.Value.ApplicationKey));
}
Controller
public class HomeController : Controller
{
private readonly DatabaseConnections _databaseConnections;
public HomeController(IOptions<DatabaseConnections> databaseConnections)
{
_databaseConnections = databaseConnections.Value;
}
}
The other answer is good if you want to use IOptions, for whatever reason I really don't like doing it and prefer binding to a class.
You can do this with a single line in your Configure Services method :
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration.GetSection("DatabaseConnections").Get<DatabaseConnections>());
}
It looks like you are missing the "Get" on the end which takes the configuration section and binds it to your class.
Further info : http://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/
In one of my concrete class. I have the method.
public class Call : ICall
{
......
public Task<HttpResponseMessage> GetHttpResponseMessageFromDeviceAndDataService()
{
var client = new HttpClient();
var uri = new Uri("http://localhost:30151");
var response = GetAsyncHttpResponseMessage(client, uri);
return response;
}
Now I put the url into appsettings.json.
{
"AppSettings": {
"uri": "http://localhost:30151"
}
}
And I created a Startup.cs
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
}
and now I get stuck.
EDIT
By the way, I don't have a controller, it is a console application.
The preferred way to read configuration from appSettings.json is using dependency injection and the built or (or 3rd party) IoC container. All you need is to pass the configuration section to the Configure method.
public class AppSettings
{
public int NoRooms { get; set; }
public string Uri { get; set; }
}
services.Configure<AppSettings>(Configuration.GetSection("appsettings"));
This way you don't have to manually set the values or initialize the AppSettings class.
And use it in your service:
public class Call : ICall
{
private readonly AppSettings appSettings;
public Call(IOptions<AppSettings> appSettings)
{
this.appSettings = appSetings.Value;
}
public Task<HttpResponseMessage>GetHttpResponseMessageFromDeviceAndDataService()
{
var client = new HttpClient();
var uri = new Uri(appSettings.Uri);
var response = GetAsyncHttpResponseMessage(client, uri);
return response;
}
}
The IoC Container can also be used in a console application, you just got to bootstrap it yourself. The ServiceCollection class has no dependencies and can be instantiated normally and when you are done configuring, convert it to an IServiceProvider and resolve your main class with it and it would resolve all other dependencies.
public class Program
{
public static void Main(string[] args)
{
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
var configuration = configurationBuilder.Build()
.ReloadOnChanged("appsettings.json");
var services = new ServiceCollection();
services.Configure<AppSettings>(configuration.GetSection("appsettings"));
services.AddTransient<ICall, Call>();
// add other services
// after configuring, build the IoC container
IServiceProvider provider = services.BuildServiceProvider();
Program program = provider.GetService<Program>();
// run the application, in a console application we got to wait synchronously
program.Wait();
}
private readonly ICall callService;
// your programs main entry point
public Program(ICall callService)
{
this.callService = callService;
}
public async Task Run()
{
HttpResponseMessage result = await call.GetHttpResponseMessageFromDeviceAndDataService();
// do something with the result
}
}
Create a static class
public static class AppSettings
{
public static IConfiguration Configuration { get; set; }
public static T Get<T>(string key)
{
if (Configuration == null)
{
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
var configuration = builder.Build();
Configuration = configuration.GetSection("AppSettings");
}
return (T)Convert.ChangeType(Configuration[key], typeof(T));
}
}
then access the settings anywhere you want like
var uri = AppSettings.Get<string>("uri");
var rooms = AppSettings.Get<int>("noRooms");
appsettings.json example
{
"AppSettings": {
"uri": "http://localhost:30151",
"noRooms": 100
}
}
You can access data from the IConfigurationRoot as following:
Configuration["AppSettings:uri"]
Like suggested in the comment I would put the information in a seperate class for that info and pass it into the DI container.
the class
public class AppSettings {
public string Uri { get; set; }
}
DI
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(new AppSettings() { Uri = Configuration["AppSettings:uri"] });
// ...
}
Controller
public class DemoController
{
public HomeController(IOptions<AppSettings> settings)
{
//do something with it
}
}