Open
Description
Currently, when a component/service needs to run on the server and on webassembly, developers are forced to create at least two versions of the service, put each version in a different location depending on whether it runs on the server or on webassembly, and register things in the DI container twice.
This in many cases is hard to get right, as it requires several steps and it's easy to miss something.
As an alternative, we can consider bringing back multi-targeting support. With this approach, it should be possible for the app to define both implementations on the same location and use the same call to register the services in one go. We can update the template to support such pattern when webassembly is involved.
One such example is:
#if !NET10_WASM
public class WeatherForecastService(WeatherContext context)
#else
public class WeatherForecastService(HttpClient ctx)
#endif
public async Task<WeatherForecast []> GetForecastAsync(Date ...)
{
#if !NET10_WASM
return _httpClient.Getasync<...>...
#else
return context.Forecasts.ToListAsync();
}
public static class ServiceRegistrationExtensions
{
public static IServiceCollection AddCommonServices(this IServiceCollection services)
{
services.AddScoped<WeatherForecastService>();
}
}