-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Blazor][Wasm] Move HttpClient from default services to Program.Main #19119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Blazor][Wasm] Move HttpClient from default services to Program.Main #19119
Conversation
So turns out I used the wrong base branch to create a new one, brb. |
74f980f
to
2e0479e
Compare
Unfortunately I don't understand how to get to the artifacts from the failed tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example failed test,
OpenQA.Selenium.BrowserAssertFailedException : Xunit.Sdk.NotEmptyException: Assert.NotEmpty() Failure\r\n at Xunit.Assert.NotEmpty(IEnumerable collection) in C:\Dev\xunit\xunit\src\xunit.assert\Asserts\CollectionAsserts.cs:line 331\r\n at Microsoft.AspNetCore.E2ETesting.WaitAssert.<>c__DisplayClass14_0.<Exists>b__0() in /_/src/Shared/E2ETesting/WaitAssert.cs:line 66\r\n at Microsoft.AspNetCore.E2ETesting.WaitAssert.<>c__DisplayClass16_01.<WaitAssertCore>b__0(IWebDriver _) in /_/src/Shared/E2ETesting/WaitAssert.cs:line 97\r\nScreen shot captured at 'F:\\workspace\\_work\\1\\s\\artifacts\\TestResults\\Release\\Microsoft.AspNetCore.Components.E2ETests\\7da499deaf6e42829c2360a57f50620a.png'\r\nEncountered browser errors\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: Unhandled exception rendering component:\"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: System.InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'BasicTestApp.AuthTest.ServerAuthenticationStateProvider'.\"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites (System.Type serviceType, System.Type implementationType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain, System.Reflection.ParameterInfo[] parameters, System.Boolean throwIfCallSiteNotFound) \\u003C0x20ecda0 + 0x0009e> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ResultCache lifetime, System.Type serviceType, System.Type implementationType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) \\u003C0x1e4c258 + 0x000f0> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (Microsoft.Extensions.DependencyInjection.ServiceDescriptor descriptor, System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain, System.Int32 slot) \\u003C0x1e4b1d0 + 0x000f2> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) \\u003C0x1e4adc8 + 0x00034> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) \\u003C0x1e458b0 + 0x0005a> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory+\\u003C>c__DisplayClass7_0.\\u003CGetCallSite>b__0 (System.Type type) \\u003C0x1e453e8 + 0x00010> in \\u003Cfilename unknown>:0 \"\r\n[2020-02-18T20:01:59Z] [Severe] http://127.0.0.1:7484/subdir/_framework/blazor.webassembly.js 0:35547 \"WASM: at (wrapper delegate-invoke) System.Func2[System.Type,Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite].invoke_TResult_T(System.Type
Stacktrace:
at Microsoft.AspNetCore.E2ETesting.WaitAssert.WaitAssertCore[TResult](IWebDriver driver, Func`1 assertion, TimeSpan timeout) in /_/src/Shared/E2ETesting/WaitAssert.cs:line 117
at Microsoft.AspNetCore.E2ETesting.WaitAssert.Exists(IWebDriver driver, By finder, TimeSpan timeout) in /_/src/Shared/E2ETesting/WaitAssert.cs:line 61
at Microsoft.AspNetCore.E2ETesting.WaitAssert.Exists(IWebDriver driver, By finder) in /_/src/Shared/E2ETesting/WaitAssert.cs:line 48
at Microsoft.AspNetCore.Components.E2ETest.Tests.AuthTest.MountAndNavigateToAuthTest(String authLinkText) in /_/src/Components/test/E2ETest/Tests/AuthTest.cs:line 234
at Microsoft.AspNetCore.Components.E2ETest.Tests.AuthTest.AuthorizeViewCases_RequireRole_NotAuthorized() in /_/src/Components/test/E2ETest/Tests/AuthTest.cs:line 118
----- Inner Stack Trace -----
at Microsoft.AspNetCore.E2ETesting.WaitAssert.<>c__DisplayClass14_0.<Exists>b__0() in /_/src/Shared/E2ETesting/WaitAssert.cs:line 66
at Microsoft.AspNetCore.E2ETesting.WaitAssert.<>c__DisplayClass16_0`1.<WaitAssertCore>b__0(IWebDriver _) in /_/src/Shared/E2ETesting/WaitAssert.cs:line 97
...omponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client/Program.cs
Outdated
Show resolved
Hide resolved
@Regenhardt, thanks for the PR. It looks like some of our test apps also need this service injected. I suggest you run these tests locally to figure out which test apps needs this. |
@Regenhardt you'll have to update the Blazor WASM based apps to add an |
{ | ||
BaseAddress = new Uri(navigationManager.BaseUri) | ||
}; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this is going to be too much non-obvious code to add into Program.cs
in the default template.
So as a recommendation, how about adding a new class in src\Components\WebAssembly\WebAssembly\src\Services
like:
namespace Microsoft.Extensions.DependencyInjection
{
public static class HttpClientServiceCollectionExtensions
{
public static void AddBaseAddressHttpClient(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
var navigationManager = s.GetRequiredService<NavigationManager>();
return new HttpClient
{
BaseAddress = new Uri(navigationManager.BaseUri)
};
});
}
}
}
Then in the project template, we just add services.AddBaseAddressHttpClient();
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea. How about .AddDefaultHttpClient();
though?
The BaseAddress part would be confusing to me if I didn't know the code, since I just now learned that it means navigationManager.BaseUri
in this context. Up until now, I would have wondered "So where does it get the BaseAddress
for the HttpClient?".
Which is coincidentally literally what I've been wondering since I started working with Blazor, as I'm planning to use CORS and had to find out how to safely change the default BaseAddress
.
Or put it in the description?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's start with AddBaseAddressHttpClient
. We have a few API reviews set up at which point we can sort out the naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to extension method and applied that at the required spots.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good
src/Components/WebAssembly/WebAssembly/src/Services/HttpClientServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/Components/WebAssembly/WebAssembly/src/Services/HttpClientServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
464b516
to
b0cf257
Compare
Now rebased as a single commit. |
src/Components/WebAssembly/WebAssembly/src/Services/HttpClientServiceCollectionExtensions.cs
Show resolved
Hide resolved
...omponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client/Program.cs
Outdated
Show resolved
Hide resolved
How's your policy on clean commits? Should I sqash the commits together, or do we keep it like this? |
@Regenhardt It will be squashed into one commit by GitHub when we merge it. |
@ajaybhargavb This should be good now. If you're happy with it, can you remove your "requested changes" flag and squash-merge when ready? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
Summary of the changes (Less than 80 chars)
Addresses #16929