Failed to deserialize response message. gRPC client - c#

When I call my gRPC servers method, client throws an exception
Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Failed to deserialize response message.")
Register my servise hier.
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpLogging();
app.MapGrpcService<LocalityService>();
app.MapControllers();
app.Run();
I tried to change the versions of packages there, but nothing helped.

The problem here is in using the app.UseHttpLogging();. This is a bug in the asp net core internal library. It tries to log grpc requests, but due to the fact that grpc encrypts data in a special way - there is a problem of deserialization.
Microsoft has ported the hotfix to .net version 8. For now, you can fix it this way:
builder.UseWhen(
ctx => ctx.Request.ContentType != "application/grpc",
builder =>
{
builder.UseHttpLogging();
}
);
Below is a link to the github discussion and official microsoft replies:
https://github.com/dotnet/aspnetcore/issues/39317

Related

Issue while initilaztion OpenTelemtry for Honeycomb

I am trying to setup honeycomb in my .net sample app with .net 6.
https://docs.honeycomb.io/getting-data-in/opentelemetry/dotnet-distro/#initialize
I am getting an error when I run my code with the below exception:
System.MissingMethodException: 'Method not found: 'OpenTelemetry.Trace.TracerProviderBuilder OpenTelemetry.Trace.TracerProviderBuilderExtensions.ConfigureBuilder(OpenTelemetry.Trace.TracerProviderBuilder, System.Action`2<System.IServiceProvider,OpenTelemetry.Trace.TracerProviderBuilder>)'.'
Below is my program.cs file
using Microsoft.EntityFrameworkCore;
using ProductCRUDAPI.Models;
using OpenTelemetry.Trace;
using OpenTelemetry;
using Honeycomb.OpenTelemetry;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DB");
builder.Services.AddDbContextPool<DbContext>(option => option.UseSqlServer(connectionString));
// Add services to the container.
var honeycombOptions = builder.Configuration.GetHoneycombOptions();
builder.Services.AddControllers();
builder.Services.AddOpenTelemetry().WithTracing(configure: otelBuilder =>
otelBuilder
.AddHoneycomb(honeycombOptions)
.AddAspNetCoreInstrumentationWithBaggage()
).StartWithHost();
builder.Services.AddSingleton(TracerProvider.Default.GetTracer(honeycombOptions.ServiceName));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
I am trying to setup honeycomb in my .net sample app for testing using the below guide
https://docs.honeycomb.io/getting-data-in/opentelemetry/dotnet-distro/#initialize
Was expecting to get the connection to go through and the logger to initialize successfully so I can attach it to my endpoints.
Just experienced this error myself, there's a package missing, go to the NUGet Package Manager in Visual Studio, checked the "include prerelease" box and then install the
OpenTelemetry.Instrumentation.AspNetCore
prerelease package.
Honeycomb have fairly recently upgraded their OpenTelemetry package from prerelease, but it seems some other dependencies are required from OpenTelemetry packages still in prerelease, some issues like these may keep occurring until all the dependent packages are out of prerelease and development stabilises a bit!

running Service Swagger error " TypeError: Failed to fetch"

I
I have a Asp.Net Core MVC Api, and published as a Windows Service. I have installed the Service locally and tried to run it from Chrome or Vista like this
http://localhost:5000/swagger/index.html
When I execute an Api Endpoint
I have this error
Failed to fetch.
Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.
This service has been deployed in several laptop without problems, but I am the only who has problems.. I do not think it is a problem in the Api, though I did some changes following every example I found.
I added in my program.cs
builder.Services.AddCors(o => o.AddPolicy("AllowAnyOrigin",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
.........
app.UseCors("AllowAllOrigins");
But still have the same problem.
I do not think it is a Cors or Service problem.
What could be the problem?
Thanks

Unable to find an OpenAPI description

I am working the Microsoft Learn tutorials to "Create a web API with ASP.Net Core".
I used .NET5
I have a problem when I run the command:
httprepl connect http://localhost:5000
I am getting a response, "Unable to find an OpenAPI description".
And the following command "ls" returns me from it not trooping endpoints.
c:/xxx/Source/Repos/ContosoPizza
$ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc
http://localhost:5000/> ls
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.
I tried all the solutions offered by #Gowiser in the question launched by #Nuse Why is HttpRepl unable to find an OpenAPI description? The command "ls" does not show available endpoints
But nothing worked.
Working through Create a web API with ASP.NET Core Controllers i also did run into the error
Unable to find an OpenAPI description
The tutorial uses dotnet 6, i was using dotnet core 3.1.
I did the changes from sasha answers here and had to add these packages
dotnet add package Microsoft.OpenApi --version 1.2.3
dotnet add package Swashbuckle.AspNetCore.Swagger --version 6.2.3
dotnet add package Swashbuckle.AspNetCore.SwaggerUI --version 6.2.3
dotnet add package Swashbuckle.AspNetCore.SwaggerGen --version 6.2.3
After all that i still got error messages. In step 4 of Exercise - Create a web API project the following is written
Connect to our web API by running the following command:
httprepl https://localhost:{PORT}
Alternatively, run the following command at any time while the HttpRepl is running:
(Disconnected)> connect https://localhost:{PORT}
I was not aware that running httprepl like this
httprepl https://localhost:{PORT}
still requires that your web app is running. So you have to open a second terminal (cmd.exe) to run your webapi project dotnet run. After that you can connect to the swagger docs using httprepl like this
connect http://localhost:5000 --verbose --openapi /swagger/v1/swagger.json
http://localhost:5000/> ls
. []
WeatherForecast [GET]
http://localhost:5000/> cd WeatherForecast
/WeatherForecast [GET]
http://localhost:5000/WeatherForecast> get
HTTP/1.1 200 OK
This did the trick for me.
Adding the swagger ui
Swashbuckle.AspNetCore - Getting Started contains
Optionally, insert the swagger-ui middleware if you want to expose
interactive documentation, specifying the Swagger JSON endpoint(s) to
power it from.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
If you change startup.cs to include this
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ContosoPizza v1"));
}
....
You get a nice gui under http://localhost:5000/swagger/ which looks like this
I am assuming you are following the steps described in the 'Create a web API project' exercise of the Create a web API with ASP.NET Core course.
Before going through step 5. make sure in your project there is:
a services.AddSwaggerGen() call in the Startup.ConfigureServices() method
an app.UseSwagger() call in the Startup.Configure() method
Here is a working version of Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
namespace ContosoPizza
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ContosoPizza", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ContosoPizza v1"));
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Calling ls in the httprepl session shows expected results:
❯ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Using OpenAPI description at http://localhost:5000/swagger/v1/swagger.json
For detailed tool info, see https://aka.ms/http-repl-doc
http://localhost:5000/> ls
. []
WeatherForecast [GET]
http://localhost:5000/>
Here you can find a full project for this exercise.
Indeed #surfmuggle to connect to the API via httprepl, the API had to be running in another terminal, so 2 terminals in total:
1 to run the API with dotnet run
1 other to call instructions like ls or other later
run this command first:
dotnet dev-certs https --trust

IIS8.5 .NET Core 3.1 Web API POST - No response externally/on public domain

I have a fully working Web API, as long as I'm making calls from the host environment. If I make a Postman call remotely to my IIS 8.5 server using the public DNS hostname it "Could not get any response".
UPDATE 21/05/2020: I've narrowed the issue down to app.UseHttpsRedirection(), which leads me to believe that the CORS preflight (OPTIONS) request that's sent is being rejected somehow once it enters the server through the redirection middleware. Probably thinks its malicious and rejects but if anyone can provide insight as to why this is, it would be GREATLY appreciated.
Please note the following:
Its a .NET Core 3.1.4 Web API template with two controllers.
These controllers can only perform POST (JSON Data From Body) requests.
I'm using IIS 8.5 on Windows Server 2012 R2, it exists alongside other live .NET framework apps.
I've installed the .NET Core hosting 3.1.4 package on the IIS server.
To visit it an example would be https://localhost/[MyProjectsName]/api/Customers
I've given the app its own dedicated app pool and dedicated identity/user to run on.
I've set .NET CLR version to No Managed Code.
The app is published with dotnet.exe using a TFS2015 build agent with the following arguments
publish "[CSPROJ LOCATION]" -c "TEST" -o "$(build.artifactstagingdirectory)\published" -f "netcoreapp3.1" -r "win81-x64"
I'm using InProcess hosting and specified webBuilder.UseIIS().
I've tried UseUrls("http://*:80;https://*:443") although I fear that's only relevant to Kestrel users.
I've defined <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> within my CSPROJ.
For the sake of troubleshooting I've opened up my CORS policy with the following...
services.AddCors(options =>
{
options.AddPolicy(name: specificOrigins,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.SetIsOriginAllowedToAllowWildcardSubdomains();
});
});
...And added app.UseCors(specificOrigins) to the Configure method within Startup.
I've also added [EnableCors("_AllowSpecificOrigins")] attribute to the controllers.
Any help would be greatly appreciated. If more code snippets or info is needed I'll try to update if possible. First time poster, long time lurker, so very unfamiliar with Stack Overflow posting.

This site can’t provide a secure connection asp.net core from dotnet linux

I am currently developing an API using asp.net core 2.1. When I use the ide visual studio 2019 in windows I don't have problems running the project, but now I am using manjaro linux and I compile by using dotnet console I have a error:
This site can’t provide a secure connection
err_ssl_protocol_error
Most of the solutions I've seen are made using the framework asp.net or changing options the visual studio IDE, so I can not implement it in my project. I have tried adding:
.UseSetting("https_port", "5000")
inside program.cs but it didn't work
class program.cs
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
RunSeeding(host);//esta llamando el alimentador de la base de datos
host.Run();
}
private static void RunSeeding(IWebHost host)
{
var scopeFactory = host.Services.GetService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var seeder = scope.ServiceProvider.GetService<SeedDb>();
seeder.SeedAsync().Wait();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSetting("https_port", "5000")
.UseStartup<Startup>();
configure.cs class
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.UseCors("AllowSpecificOrigin");
}
Admittedly not using Linux, but in my case I was able to fix this on my asp.net core project by:
Open the project properties by right-clicking the project from the solution explorer and selecting properties
Open the Debug tab
Under "Web Server Settings", check "Enable SSL"
Now when I debug the application it automatically launches to the https URL.
We have a known issue on which we're working currently for ASP.NET Core apps running in containers on app service. This issue is described here, as well as our plan to educate the community on how to work around this issue today and on how we plan on repairing it long-term. May not be related but wanted to drop it in here just in case.
I'm working a localhost testing instance. I don't need to work with https so disable https.
class program.css
//.UseSetting("https_port", "5000")
class startup.css
//app.UseHsts();
I am most likely late to the party but perhaps this might help someone else who will visit.
You can say the error is almost self explanatory, you are trying to reach the site using a non supported protocol. Please make sure you are using the correct protocol i.e if your site is http then make sure the url typed on the browser is indeed http, example https://localhost:8000 vs http://localhost:8000, note the letter s difference in the URLs. I ran into such issues and changing to the correct protocol solved my problem.
I am using vs Code . .net version 6.
Removing below line from launchSettings.json worked for me
"sslPort": 44308 <-- Remove this.
I run the project with
dotnet run --urls "http://localhost:5100"
In ASP.net Core 6, go to:
Project --> <> Properties --> Debug --> General --> Open
debug launch profiles UL --> IIS Express --> untick the Use SSL
option.
Clean --> Recompile --> Publish
Make sure your using http and not https

Categories

Resources