My WPF Application instance implements a custom interface, but second DLL doesn't see the interface
I have an application written in WPF where one of the assemblies needs an
System.Net.Http.HttpClient
instance. The MSDN documentation says to have only one instance for your entire application. But I was using the client an assembly, here called MyDLL
that wasn't the application's assembly. So I put in my application class a reference to a custom interface, IHttpClientOwner
that's declared in an assembly that both projects can reference. (Otherwise, I'd get a circular reference.) I thought I'd find when the class inside MyDLL
checks System.Windows.Application.Current
, it would find IHttpClientOwner
was implemented. But no. It isn't.
What's going on?87 Replies
The MSDN documentation says to have only one instance for your entire applicationThat's a very naive understanding of the underlying problem. Give this a read. https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
Use IHttpClientFactory to implement resilient HTTP requests - .NET
Learn how to use IHttpClientFactory, available since .NET Core 2.1, for creating
HttpClient
instances, making it easy for you to use it in your applications.I thought I'd find when the class inside MyDLL checks System.Windows.Application.Currentdoes your you
System.Windows.Application
subclass implement this interface?
is this code getting called when your WPF app is the entry point?Yes it does.
Well, the App class implements the interface. But no constructor is called.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Don't have a repository online
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Well, I don't see how the
IHttpClientFactory
helps. First, I need a link to an interface. But where do I get that. Your page doesn't describe that. Then if I'm asking the factory to make new clients all the time, how is that different from just creating a HttpClient
yourself?is this code getting called when your WPF app is the entry point?
No. I said that. It's the only app.
what app?
In my project.
which one?
What do you mean which one?
you said you have two apps, yes?
plus a third shared library
No. I have two proejcts.
One is a DLL.
which are what?
they're all DLLs
The DLL isn't an application.
so, what is it?
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
???
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Now I'm lost.
me too
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
what two projects do you have
Will Pittenger
The DLL isn't an application.
Quoted by
React with ❌ to remove this embed.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I don't have a repository.
personally, I have 0 interest in pouring over an entire repository of code, and I do not want to encourage that as a legit way to get help
define the problem and answer some basic straightforward relevant questions
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I think it probably is
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I think he doesn't understand what an entry point is
okay
so, when does that run?
what loads that?
I know what the entry point project means. I've been doing Windows since 1995.
great
so, what are the two projects you have
one is a WPF application
what is the other one?
how do they relate?
Class is loaded indirectly by events in the main window.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
here's what I know....
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Nope.
debugger shows x is
true
what's probably happening is that the static constructor of Class
is getting called before the application has actually been instantiated and bootstrapped
because static constructors are invoked when the type is loaded into memory, not when it's actually used
the reason that linking to IHttpClientFactory
is helpful is because it plays directly with IoC systems
which is what you probably want here
instead of using some strange static context to pass around the shared HttpClient instanceUnknown User•14mo ago
Message Not Public
Sign In & Join Server To View
I'm explicitly starting the app. I added a
OnStartup
override. That's not called.if your
Class
instance needs an HttpClient
object to use, you should pass it in when you construct itIt constructs itself. Only one instance allowed.
which is what an IoC system is for
so
example
It's loading JSON from the Internet so I can define its contents. How is that relevant?
Why isn't my App's
OnStartup
being called? Or any of its constructors?_serviceProvider
is an IoC container: Inversion of Control
we have a lot of different services and classes and whatnot responsible for different things that should have different lifetimes
we hand over management of all of that to one container
that container ALSO handles dependency management
so, when I say GetRequiredService<Window>()
As I said,
OnStartup
isn't being called. What good does all this do if it isn't being called? Yet that's the project I'm trying to start. The main window even appears.because
Window
is registered as Transient
it creates a brand new one for me, every time
and Window
requires MyService
so, the container supplies one
and since MyService
is registered as Singleton
it only ever creates one, and shares it to everyone that asksSo explain how all that helps if
OnStartup
isn't called?then the
HttpClientFactory
system supplies HttpClient
s to everyone in the most appropriate way
and it does not, in fact, only supply one instance, across the entire app
it does a much more efficient job
it allows you to, say, configure a different HttpClient
for each different server you want to call to
since they might have different HTTP settings you want to use
the underlying resource that is ACTUALLY important to maintain as singleton, or close to it, is maintained internally
why do you think OnStartup()
isn't being called?I put a breakpoint in it. I also told you the constructors weren't called.
a breakpoint only hits if you give it the opportunity to hit
is your app crashing inside the
static Class
constructor?
where you throw an exception?There's nothing in that constructor that runs until after I open a window and click a menu item. So no.
so,
OnStartup()
isn't being called, but your window is opening?It would crash there, but that doesn't prevent
OnStartup
from running.
Correct.where does your window get created?
By app.xaml.
StartupUri
?Yes.
show your App codebehind
I did ages ago.
there's no
OnStartup()
override in it, so....
no, you didn'tThat was added just now.
which is why I asked to see it again
use "cs" BTW for the language identifier, not "C#"
Ah.
It worked while editing. 9_9
¯\_(ツ)_/¯
You probably want this too:
is it
Best_Chat
or BestChat
?Oh.
That was it.
Something simple and I missed it.
Thanks guys. Sorry for the confusion.
You're instantiating a new client twice here
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
That was testing code.
It's gone.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
So what's the other project? Is it a class library?
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
Yeah I'm just curious
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.