How does option work in .net core
I have the below class here the contentfulclient base has a class called ContentfulClientBase which has a property called as contentfuloption with some property. I want to assign the value to one of the property. I have my own contentfuloption class which has some of the properties of ContentfulClientBase class contentfuloption. I want to pass my model value to contentfulclientbase class. But I don't know how to achieve it. When I use
_options = options.Value;
I get an error states type to type conversion is not possible.
Basically I want my baseclass property should get my value which I provide.
public class ContentfulManagementService : ContentfulClientBase, IContentfulManagementService
{
private readonly ILogger<ContentfulManagementService> _logger;
private readonly Models.ContentfulOptions _contentfulOptions;
public ContentfulManagementService(
HttpClient httpClient,
ILogger<ContentfulManagementService> logger, IOptions<Models.ContentfulOptions> options)
{
_httpClient = httpClient;
_logger = logger;
_contentfulOptions = options.Value;
}
}
54 Replies
Can you post the specific error? That looks reasonable to me.
Just as a nit (more of a style thing) if you just want the options to go to your base class, maybe the base class should have a ctor that takes the options and then you can pass to base in your inheriting class.
i.e.
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
appsettings.json:
src/Foo/FooOptions.cs:
src/Foo/FooServiceCollectionExtensions.cs:
Program.cs / Startup.cs:
Bar.cs:
Oh ya doi
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Tbh i hate using IOptions unless i need the hot reload
Just bind and raw dog it
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
nuget / unit tests? How does IOptions help there compared to just the pattern?
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Lol i mean, injecting
MyOptions
vs IOptions<MyOptions>
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I mena you have to bind either way so really it's just more code to
.value
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
preference anywho
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
.Value just makes me sad when it adds no value 😛
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
But also re: the OP question didn't his original look right?
(tho that command is 👌)
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
ah i get u
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
depends if it's for the client only or not
oh as in ctor
ye ye tru
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
private if it's client specific (as OP said)
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
anyway methinks that this whole thing smells like it wants to be using the
IHttpClientFactory
stuff
oh cool discord does diffs
how doUnknown User•11mo ago
Message Not Public
Sign In & Join Server To View
mad ty
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
❤️ markdown
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
oh ye it's pain
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
yeah i noticed that
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
the pattern isn't right
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
this'd be the more correct usage
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
note the service getting the HTTP client in OP's example too
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
ye ye but like usually client is for wrapping the calls + serilization/deserilization
and then the service handles business logic and orchestration
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Ye for sure
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
Ye 🙂
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
I've seen that pattern in a few places and it makes me
Unknown User•11mo ago
Message Not Public
Sign In & Join Server To View
true and real
but c# is a wonderful language that lets you do all kinds of interesting things