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

.NET (Core and Framework)

 
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 
QuestionExe File encoding - http via vb.net Pin
Bart van Tuijl31-Mar-23 21:20
Bart van Tuijl31-Mar-23 21:20 
AnswerRe: Exe File encoding - http via vb.net Pin
Dave Kreskowiak1-Apr-23 5:18
mveDave Kreskowiak1-Apr-23 5:18 
AnswerRe: Exe File encoding - http via vb.net Pin
Richard MacCutchan1-Apr-23 5:42
mveRichard MacCutchan1-Apr-23 5:42 
AnswerRe: Exe File encoding - http via vb.net Pin
Eddy Vluggen1-Apr-23 6:28
professionalEddy Vluggen1-Apr-23 6:28 
GeneralRe: Exe File encoding - http via vb.net Pin
Bart van Tuijl2-Apr-23 5:13
Bart van Tuijl2-Apr-23 5:13 
GeneralRe: Exe File encoding - http via vb.net Pin
Richard Deeming2-Apr-23 22:05
mveRichard Deeming2-Apr-23 22:05 
GeneralRe: Exe File encoding - http via vb.net Pin
Bart van Tuijl3-Apr-23 5:49
Bart van Tuijl3-Apr-23 5:49 
GeneralRe: Exe File encoding - http via vb.net Pin
Richard MacCutchan3-Apr-23 6:13
mveRichard MacCutchan3-Apr-23 6:13 
GeneralRe: Exe File encoding - http via vb.net Pin
Richard Deeming3-Apr-23 6:44
mveRichard Deeming3-Apr-23 6:44 
GeneralRe: Exe File encoding - http via vb.net Pin
Bart van Tuijl3-Apr-23 7:33
Bart van Tuijl3-Apr-23 7:33 
GeneralRe: Exe File encoding - http via vb.net Pin
jschell4-Apr-23 6:19
jschell4-Apr-23 6:19 
AnswerRe: Exe File encoding - http via vb.net Pin
jschell3-Apr-23 6:09
jschell3-Apr-23 6:09 
QuestionBad Programming??... Pin
FreakNeck71631-Mar-23 4:09
FreakNeck71631-Mar-23 4:09 

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.