Blazor #inject data service depending on project - blazor-server-side

I have a shared .net 6 project for just controls. I wanted to use the same controls in .Net Blazor Web as with .Net Maui Blazor. I was trying to do this but it doesn't work. Any idea how I can switch what data service is called based on what's calling it?
#*#if(AppDomain.CurrentDomain.FriendlyName == "nu.web")
{
#inject IDataService _db
}
else
{
#inject IHttpService _db
}*#
Thanks for any help...

From Bennyboy1973’s hint… I removed the extra interface IHttpService and used IDataService on both HttpService and DataService.
Then on the .Net Blazor web app startup.cs file:
builder.Services.AddSingleton<IDataService, DataService>();
Then on the .Net Maui Blazor app MauiProgram.cs file:
builder.Services.AddSingleton<IDataService, HttpService>();
If anyone cares about why I wanted it this way… I get a slight performance improvement from calling SQL via Dapper on web and .Net Maui doesn’t allow it… So I’m calling the same Dapper SQL via Minimal API.
Thanks again for all the answers

Related

gRPC and ASP.NET Core: serverside reflection

I've been trying to implement the concepts of serverside reflection described in https://github.com/grpc/grpc/blob/master/doc/server-reflection.md and for C# https://github.com/grpc/grpc/blob/master/doc/csharp/server_reflection.md in the implementation of ASP.NET Core. You have to somehow configure Kestrel to enable Reflection first. I just don't succeed and have to give up. This is the tricky part that somehow has to be implemented in ASP.NET Core.
// the reflection service will be aware of "Greeter" and "ServerReflection" services.
var reflectionServiceImpl = new ReflectionServiceImpl(Greeter.Descriptor, ServerReflection.Descriptor);
server = new Server()
{
Services =
{
// the server will serve 2 services, the Greeter and the ServerReflection
ServerReflection.BindService(new GreeterImpl()),
ServerReflection.BindService(reflectionServiceImpl)
},
Ports = { { "localhost", 50051, ServerCredentials.Insecure } }
};
server.Start();
Does somebody know how to enable serverside reflection in ASP.NET Core with Kestrel? Or even better has a complete working example of serverside reflection in C# ASP.NET Core?
Update
It seems that the default implementation of .NET Core 3.1 doesn't support the reflection yet. The NuGet https://github.com/grpc/grpc-dotnet/ does work and seamlessly integrates in ASP.NET Core. The example covers discovering services on a server. I can't find concrete descriptions how to iterate through all methods on a service and all fields in a message. Maybe JamesNK can help?

How to consume HttpContext in old ASP.NET web forms application and new ASP.NET core application

I'm converting all of our shared class libraries from .NET Framework to .NET Standard so that they're consumable by both our older .NET Framework and our new .NET Core applications. The goal is to get all of our applications to .NET Core, but it's a large system so will take time so we need the two worlds to coexist for a year or so in the meantime.
One of the pain points is that the shared class library code leverages HttpContext extensively. I understand that the new .NET Core way to access it from within class libraries is to use AddHttpContextAccessor. However, we still have old ASP.NET web forms applications AND new ASP.NET Core web applications that both need to call this common code. If I switch to using HttpContextAccessor with DI then it won't work from the old ASP.NET Web Forms app, as it has no concept of what HttpContextAccessor is.
I'm wondering what my options are here... Is there a way to set up access of HttpContext such that it can be consumable both in ASP.NET Web Forms and ASP.NET Core?
You have to have some common interface between both the old and new applications I would think.
Create a new IMyHttpContext and inject that type into both the old and the new. Use your DI infrastructure to instantiate a new MyClassForOld: IMyHttpContext that accesses an underlying reference HttpContext for the old apps and use your DI to instantiate another new MyClassForNew: IMyHttpContent that accesses an underlying reference to HttpContextAccessor

dot net core web api project templates are different

I just compared the code generated by dotnet new webapi and dotnet new angular, and checked only the web api code.
For some reason controllers derive from different classes, in project created with dotnet new webapi controller is derived from ControllerBase, while in project created with dotnet new angular controller is derived from Controller
Also the return types for actions are different, in angular template its the actual return type, while in webapi template its ActionResult
Why is that?
Which option is the "best"? And why are they different?
Before .NET Core Web API and MVC controllers were similar.
But starting with 2.0/2.1 Web API was changed.
Now you should derive class from ControllerBase instead Controller and use [ApiController] attribute.
Old style will continue to work anyway.
Angualar use Web API, I suppose, so template should be updated.
See WEb API documentation.
ControllerBase doesn't have any of the Razor/view support. ControllerBase is used for APIs.

Use DbContext in other .NET Class Library

I am using ASP.NET Core with EF Core Code First.
I am defining the dependency injection of the DbContext in service configuration of the Startup.cs of my ASP.NET Core project like described here (https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html):
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TestContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DevelopmentDatabase")));
}
But I do not want to use the DbContext in a controller like described at the same site but I want to outsource it to another .NET Core Class Library project together with the Migration.
How do I accomplish this?
Implement the Repository Pattern
It's explained clearly here: Building Your First Web API with ASP.NET Core MVC
But register your repository as:
services.AddScoped<ICustomerRepository, CustomerRepository>();
instead of using .AddSingleton as the link suggests, to avoid this other problem.
I just put my xxxDbContext in a dll library project, but decided to keep migrations in the asp project:
protected override void OnConfiguring(DbContextOptionsBuilder b)
{
base.OnConfiguring(b);
b.UseSqlServer("Data Source=...", x => x.MigrationsAssembly("WebApp1"));
If you don't use MigrationsAssembly, your migrations should go to the dll I believe.
But I'm having problems using the dll in another project, I can't initiate a connection to the database

Using WebApi and structure map dependency injection

Although there are so many question on stack overflow which tends similar to my this question but no one is resolving my problem
I was using an MVC4 internet application wherein i had few MVC controller and for dependency injection i was using Structure map. Although dependency injection works fine with MVC controller but when i added an WebApi controller in the same MVC internet application and using the same parameter in constructor of WebApi controller as what i am using in MVC controller but dependency injection is not working with WebApi controller, although if i don't use dependency injection for WebApi controller(parameterless constructor), then it works fine, but for WebApi dependency injection(parameterized constructor) it is throwing an error No parameter less constructor is found.
Conclusion depedencies are not being injected for WebApi Controller in Internet(MVC application).
Few articles suggested to use DependencyResolver.SetResolver(). i used but did not resolve the issue.
The reason why WebApi controller were not working is as following:
Since MVC Controller uses different DependenyResolver instance that is the part of System.Web.MVC .dll and within the System.Web.MVC namespace
http://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.98).aspx
Where as Api Controllers uses DependencyResolver instance that is part of System.Web.Http.
http://msdn.microsoft.com/en-us/library/system.web.http.dependencies.idependencyresolver(v=vs.108).aspx
MVC and WebAPI controllers have a different way of setting their dependency resolver. This is how I set my dependency resolver for Unity:
public void ConfigureForMvc4()
{
DependencyResolver.SetResolver(
new UnityMvc4.UnityDependencyResolver(Container));
}
public void ConfigureForWebApi()
{
GlobalConfiguration.Configuration.DependencyResolver =
new UnityWebApi.UnityDependencyResolver(Container);
}
You need to add Dependency Injection files for WebApi
Install NuGet StructureMap.WebApi2 and in the App_Start/WebApiConfig.cs file, call StructuremapWebApi.Start();
Refer: https://www.exceptionnotfound.net/setting-up-dependency-injection-in-web-api-with-structuremap/