Issue in use of NpgsqlDataSource method and connecting UI with APP app with httpclient #3170
-
I am trying out aspire and in my app I am using ADO.net npgsql client. For that I have added Npgsql package.
In the program.cs file where the error is pointing out I have added the NpgsqlDataSource with the aspire container as below:
I am using this NpgsqlDataSource in the DbOperations file where I got the instance of the NpgsqlDataSource with DI. And I have added DbOperations as a singleton service, after that I can get the instance of the DbOperations by DI in the API endpoints like below: AppHost Program.cs file:
I have added the postgres database and using this postgres in the AddNpgsqlDataSource method. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 3 replies
-
This error is telling you that you are calling this method after building the application. You need to do it before. |
Beta Was this translation helpful? Give feedback.
-
Thanks @davidfowl. Able to solve it now. |
Beta Was this translation helpful? Give feedback.
-
Hii @davidfowl, Additionally I want to ask one more question, I am able to use the API and can connect with the database as well. But when I use the Application name "backend" in the endpoint request from the blazor UI app. It gets failed and stats the error as "No host found with name backend" I want to connect to API from the UI app. What will be the string to pass on in httpclient. Currently passing |
Beta Was this translation helpful? Give feedback.
-
Are you service defaults wired up correctly? That's how service discovery works when you run locally: The HttpClientFactory is wired up the service discovery system, and the apphost provides the right configuration. |
Beta Was this translation helpful? Give feedback.
-
Yes it is same. Now If I want to use HttpClient, then how can I get the instance of the same in the ui app. I think the method AddServiceDefaults() will just configures the default httpclient for us, right? Now, if it has been configured then how to use it. How can I get the instance in the Razor Pages. I will have to tell UI app to discover for the instance from AppHost? |
Beta Was this translation helpful? Give feedback.
-
If you want to get an instance of an http client from DI then you can inject an HttpClient into your application and it should be configured. You can learn more about the HttpClientFactory here https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory.
The apphost you posted should be enough to make it work. Your "Password-Manager" project should be able to talk to the backend using "http://backend". |
Beta Was this translation helpful? Give feedback.
-
Thanks @davidfowl, Now the UI app is able to communicate with backend through IHttpClientFactory injection and creating the client.
|
Beta Was this translation helpful? Give feedback.
-
Update: Now I am able to prevent the page reload on submit form by adding @rendermode="InteractiveServer" to Routes and HeadOutlet tags in App.razor. It will prevent the page reloading on the component change and will then prevent the page reloading on the form submission. |
Beta Was this translation helpful? Give feedback.
-
Hii @davidfowl, I have a doubt that is the aspire compatible to complext apps? Like, let's assume a scenario where you have to manage multiple services like usually in eShop app whenever user place the order the inventory service should be activate to reduce the number of quantity. A scenario where I have multiple dbContexts. |
Beta Was this translation helpful? Give feedback.
If you want to get an instance of an http client from DI then you can inject an HttpClient into your application and it should be configured. You can learn more about the HttpClientFactory here https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory.
The apphost you posted should be enough to make it work. Your "Password-Manager" project should be able to talk to the backend using "http://backend".