Click here to Skip to main content
15,913,709 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Richard Andrew x6427-Jul-23 10:55
professionalRichard Andrew x6427-Jul-23 10:55 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx27-Jul-23 11:14
professionaljkirkerx27-Jul-23 11:14 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
Dave Kreskowiak27-Jul-23 18:02
mveDave Kreskowiak27-Jul-23 18:02 
GeneralRe: Creating a print event handler in a class, and calling it from my print dialog, I need human help on this Pin
jkirkerx28-Jul-23 6:26
professionaljkirkerx28-Jul-23 6:26 
QuestionNuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 6:25
professionalRichard Andrew x642-Jul-23 6:25 
AnswerRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 7:38
mveGerry Schmitz2-Jul-23 7:38 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 8:36
professionalRichard Andrew x642-Jul-23 8:36 
GeneralRe: NuGet Packages and Library Files Pin
Gerry Schmitz2-Jul-23 11:03
mveGerry Schmitz2-Jul-23 11:03 
AnswerRe: NuGet Packages and Library Files Pin
Dave Kreskowiak2-Jul-23 9:27
mveDave Kreskowiak2-Jul-23 9:27 
GeneralRe: NuGet Packages and Library Files Pin
Richard Andrew x642-Jul-23 10:38
professionalRichard Andrew x642-Jul-23 10:38 
AnswerRe: NuGet Packages and Library Files Pin
Mirza Ahmed Ali Baig3-Sep-23 23:30
Mirza Ahmed Ali Baig3-Sep-23 23:30 
QuestionIsolated/protected execution environment? Pin
GregJ728-May-23 14:16
GregJ728-May-23 14:16 
AnswerRe: Isolated/protected execution environment? Pin
Dave Kreskowiak29-May-23 3:58
mveDave Kreskowiak29-May-23 3:58 
GeneralRe: Isolated/protected execution environment? Pin
GregJ729-May-23 6:47
GregJ729-May-23 6:47 
AnswerRe: Isolated/protected execution environment? Pin
jschell29-May-23 6:10
jschell29-May-23 6:10 
QuestionHardly any .onnx modules. But plenty of .pt Modules included. Pin
Member 159402389-May-23 15:53
Member 159402389-May-23 15:53 
AnswerRe: Hardly any .onnx modules. But plenty of .pt Modules included. Pin
Pete O'Hanlon9-May-23 20:51
mvePete O'Hanlon9-May-23 20:51 
GeneralRe: Hardly any .onnx modules. But plenty of .pt Modules included. Pin
Member 1594023810-May-23 2:40
Member 1594023810-May-23 2:40 
AnswerRe: Hardly any .onnx modules. But plenty of .pt Modules included. Pin
Richard MacCutchan9-May-23 22:25
mveRichard MacCutchan9-May-23 22:25 
GeneralRe: Hardly any .onnx modules. But plenty of .pt Modules included. Pin
Member 1594023810-May-23 2:43
Member 1594023810-May-23 2:43 
QuestionQuestion about Dependency Injection Pin
Fred283428-Apr-23 2:28
Fred283428-Apr-23 2:28 
Hello guys,

I have an issue related to dependency injection in the Program.cs of a .Net 7 application.
In this case it's an API, but that does not really matter.

Before calling the line:
var app = builder.Build();

I am adding a few singletons normally with :
C#
builder.Services.AddSingleton<IMyConfig, MyConfig>();
builder.Services.AddSingleton<IMyHttpClient, MyHttpClient>();

My issue is that the HTTP client singleton requires info from the config singleton, and it's not a simple case of injecting the former into the latter.

We can't use appsettings.json to manage our configuration, that would be too easy, and because the second one is an HTTP client, I have this code :
C#
builder.Services.AddSingleton<IMyHttpClient, MyHttpClient>();
builder.Services.AddHttpClient<IMyHttpClient, MyHttpClient>(client =>
{
    client.BaseAddress = new Uri(myConfig.myURI);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

}).ConfigurePrimaryHttpMessageHandler(() =>
{
    return new HttpClientHandler()
    {
        UseDefaultCredentials = true
    };
});

.. all this still before I call builder.Build();

I have already googled and chatGPT'd the problem, but the solutions I get are for .Net 6 at best, and do not apply.

My current solution is to use builder.Services.BuildServiceProvider().GetService<imyconfig>() to get an instance of myConfig, but then the builder is called twice, so this instance is lost, and after the builder.Build(), I have to read my config again.

Technically, this works, but it's ugly and triggers my OCD.

If there's any DI god out there, what am I missing ? Smile | :)

Thanks in advance
Fred

modified 28-Apr-23 9:43am.

AnswerRe: Question about Dependency Injection Pin
Gerry Schmitz28-Apr-23 4:39
mveGerry Schmitz28-Apr-23 4:39 
AnswerRe: Question about Dependency Injection Pin
jschell28-Apr-23 11:21
jschell28-Apr-23 11:21 
AnswerRe: Question about Dependency Injection Pin
Bohdan Stupak29-Apr-23 3:43
professionalBohdan Stupak29-Apr-23 3:43 
AnswerRe: Question about Dependency Injection Pin
Rob Philpott18-Jul-23 4:18
Rob Philpott18-Jul-23 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.