Missing Response property - asp.net-core

I am porting over a framework app to .net core.
The app has a HttpHandler that does this:
app.Context.Response.StatusCode = 401;
app.Context.Response.SubStatusCode = 2;
Moving this into a middleware, however there is no SubStatusCode on the Context.Response object in .net Core it seems
After much googling it seems like this is not a common problem, is there an equvalent to this in asp.net core?

Related

Blazor #inject data service depending on project

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

HttpContext.RewritePath in asp.net core

I am new in ASP.Net core and migrating framework code into asp.net core but not understanding how to convert below code into asp.net core.
HttpContext.RewritePath(HttpContext.Request.RawUrl.Substring(0, HttpContext.Request.RawUrl.IndexOf("?", StringComparison.Ordinal)), string.Empty, queryString);
Rewriting the path in ASP.NET Core is as simple as running a middleware very early in the pipeline that manipulated the relevant parts of the http request. You can tweak Path, PathBase, QueryString etc and affect the middleware that runs after the rewrite code. This is what the rewrite middleware does that ships as part of ASP.NET Core.

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 configure MessagePack on SignalR with Owin and WebApi 2?

We have a SignalR service that runs as a self-hosted OWIN app with Web API 2. It is configured like
resolver.Register(typeof(JsonSerializer), () => JsonSerializer.Create(serializerSettings));
app.MapSignalR(new HubConfiguration { EnableDetailedErrors = true, Resolver = resolver});
We want to replace our JsonSerializer with MessagePack. The obvious examples all configure it from a AspNet.Core web application context. I can't seem to find any examples with Owin self hosting.
EDIT: It looks like MessagePack is a new ASP.NET Core feature. We are running the older ASP.NET version and so I guess the real question is how to do binary formatting over ASP.NET SignalR.
It's not possible. I am migrating my solution to the AspNetCore version of SignalR.

ASP.NET Core middleware or OWIN middleware?

As I understand it, ASP.NET Core has support for OWIN middleware (via app.UseOwin()) in addition to its own native middleware.
What is the difference between ASP.NET Core middleware and OWIN middleware?
When designing a new middleware, how do I know if I should design it as a ASP.NET Core middleware or a OWIN middleware?
Your question made me curious and I would like to share, what I have learned so far.
Katana is the implementation of the OWIN spec. After version 3.0 of Katana this technology has been fully integration in the web stack we know as ASP.NET Core today.
While this transition much has stayed similar to the OWIN specifications. Although some changes have been made. In order to use existing OWIN middleware in ASP.NET Core the supports OWIN by an optional feature ("app.UseOwin()").
If you want to target with your middleware ASP.NET apps and ASP.NET core apps, then I would use OWIN middleware. If you want to give ASP.NET Core developers a first class citizen experience, then a ASP.NET Core middleware would be recognized as more "fitting".
Some information about the relationship between ASP.NET Core middleware and OWIN middleware can be found here:
Katana, ASP.NET 5, and bridging the gap
Katana Project
https://docs.asp.net/en/latest/fundamentals/owin.html
I have come to understand it as this; ASP.NET Core middleware is on a higher level than OWIN middleware which is on a lower level.
ASP.NET Core middleware has the advantage that it is much easier to develop a middleware in as you get passed in the HttpContext which you can use. The disadvantage is that the middleware you develop depends on ASP.NET Core.
OWIN is on a lower level and you get a OWIN environment which is a IDictionary<string, object>. The advantage is that is it not tied to ASP.NET hence can run on any OWIN server (such as Nowin). The disadvantage is that it takes more effort to code since you have to build your own context from the OWIN environment or use the OWIN environment dictionary directly and keep track of all OWIN keys and objects.
Edit: You don't have to keep track of OWIN keys yourself, you can use the OwinEnvironment class to get a strongly typed environment.
var environment = new OwinEnvironment(HttpContext);
var features = new OwinFeatureCollection(environment);