Can't add Sentry to ASP.NET Core Web API project in .NET 6 - c#

I am trying to add Sentry to my ASP.NET Core Web API project using .NET 6. However, I am encountering an issue where I cannot find the AddSentry method in IServiceCollection, even though I have installed the Sentry.AspNetCore package.
I have followed the instructions on the Sentry documentation and added the SentryAspNetCore package to my project. I also added the following line to my Program.cs file:
builder.Services.AddSentry();
However, I get the following error when building my project:
'IServiceCollection' does not contain a definition for 'AddSentry' and the best extension method overload 'LoggingBuilderExtensions.AddSentry(ILoggingBuilder, string)' requires a receiver of type 'ILoggingBuilder'
It seems that AddSentry is not recognized as an extension method of IServiceCollection. I have searched for similar issues and found some suggestions to add the Sentry.Extensions.Logging package, but that did not help.
Can anyone help me figure out what I am doing wrong and how I can add Sentry to my ASP.NET Core 6 Web API project? Thank you in advance!

You need to install Sentry.Extensions.Logging nuget and add using Sentry.Extensions.Logging.Extensions.DependencyInjection; and then call:
builder.Logging.AddSentry(); // use Logging, not Services
Docs.

Related

Why is ASP.NET Core 6 not discovering controllers when run from another .NET Core app

I have created an out of the box ASP.NET Core 6 Web API project.
When I launch the project I can see Swagger loaded, displaying a single WeatherForecast endpoint which works when tested.
However, when I launch the Web API project from another .NET Core app, the controllers are not discovered. Swagger returns the following message for reference. Also manually calling the endpoint Url fails.
After some investigation into this issue, I found that if I register the WeatherForecast controller manually it will become visible in Swagger and accessible when requested.
Why is the WeatherForecast controller not being discovered when running the ASP.NET app from another .NET Core App ?
Source Code: GitHub
Have you tried this:
builder.Services.AddMvc()
.AddApplicationPart(typeof(WeatherForecastController).Assembly)
.AddControllersAsServices();
Using the answer linked to by #RichardDeeming I was able to get it to work in .Net 6. Find AddControllers in Program.cs and adapt it to look like this (substitute TestServiceController for one of your controllers that is in the target assembly):
var assembly = typeof(TestServiceController).Assembly;
builder.Services.AddControllers()
.PartManager.ApplicationParts.Add(new AssemblyPart(assembly) );

GetSection.Bind not working in .NET 4.7.2

I am following a tutorial for implementing Azure Notification Hubs in my backend application (https://learn.microsoft.com/en-us/azure/developer/mobile-apps/notification-hubs-backend-service-xamarin-forms).
The tutorial focuses on using a API build in .NET Core, though my existing API is build in .NET 4.7.2 and for now it is to much of a hassle to rewrite the entire API.
Although I got most of it working in .NET 4.7.2, I am stuck on the following.
In the tutorial the following is being set for Dependency Injection:
services.AddOptions<NotificationHubOptions>()
.Configure(Configuration.GetSection("NotificationHub").Bind)
.ValidateDataAnnotations();
I have rewritten this to:
services.AddOptions<NotificationHubOptions()
.Configure(ConfigurationManager.GetSection("NotificationHub").Bind)
.ValidateDataAnnotations();
Sadly, I get a error on the Bind property that apparently does not exist:
'object' does not contain a definition for 'Bind' and no accessible extension method 'Bind' accepting a first argument of type 'object' could be found.
Any clue how I can get this to work (or with the same functionality) in .NET 4.7.2?
The NotificationHub config setting is a setting stored in a secrets file.
You need to install the Microsoft.Extensions.Configuration.Binder package.
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
The ConfigurationBinder.Bind method is part of that package.

Microsoft OData in .NET CORE 5 - Adding OData to services throws up a missing using directive yet the package is there

I am developing in .net core 5.0. (There is a tutorial by Sam Xu on moving to dotnet core 5)
I have gone back to the absolute bare minimum with the most simple API project in Visual Studio.
I had this working in my project earlier in the year and it was running on .net core 5.0. See tutorial above.
In this project I have created a new project. Then I went to NuGet to get the package "Microsoft.AspNet.OData" version 7.4.1
I then added the following to the startup file.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddCors();
services.AddControllers();
services.AddOData(); //THIS ONE
}
I added "services.AddOData" and its throwing up the error,
Error CS1061 'IServiceCollection' does not contain a definition for 'AddOData' and no accessible extension method 'AddOData' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?) JobsLedger.API C:\Users/.../JobsLedger.API\Startup.cs 35 Active
I had already added the package required for this service. Now I had this working a couple of months ago.
Is there a new package that you need to add?
What am I doing wrong or is this a "breaking change" that I dont know about?
if you are using .net5.0 it required odata 8.0 preview.
In rc2, according to this article https://devblogs.microsoft.com/odata/attribute-routing-in-asp-net-core-odata-8-0-rc/, Sam Xu made a breaking change.
"AddOData is changed from extensions on ISerivceCollection to extension on IMvc(Core)Builder.
The migration is easy by calling AddControllers() first, then calling AddOData()."
services.AddControllers()
.AddOData(opt => opt.AddModel("odata", GetEdmModel()));
[UPDATE - 11/2021]
In odata 8.0 AddModel doesn't exist anymore, it was renamed with AddRouteComponents
services.AddControllers()
.AddOData(opt => opt.AddRouteComponents("odata", GetEdmModel()));
https://devblogs.microsoft.com/odata/api-versioning-extension-with-asp-net-core-odata-8/

Swagger in Net 4.5.1 existing project

I have an existing project that contains a number of APIs. I would like to do a documentation of these APIs. While searching I came across Swagger which seems not bad. So I installed the nugets packages (Swashbuckle.Net45 -Version 5.2.1). I configured SwaggerConfig and started my project. When I go to the URL localhost [port_num] / swagger I have the following error:
"ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/Pdf' and method 'GET'.
I do not really understand what this error is so if someone has already had this and has a solution I am a taker.
Swagger has a stricter rule set for the naming / routing of your controller methods.
The error suggests that you have multiple methods on a controller that are mapped via the same Http Method and path.
Check for any methods in your controllers that match api/Path and change one of them.

ASP.NET Core MVC application insights stopped working after upgrade to .NET Core 1.1.1

I have one simple ASP.NET Core application I was working on back in December 2016. It worked just fine with application insights and telemetry.
Now after 4 months I wanted to pick up this work and started with upgrade from .NET Core 1.1.0 to 1.1.1. In this process package Microsoft.ApplicationInsights.AspNetCore got updated from version 1.0.2 to version 2.0.0.
This unfortunately caused my app to stop working, in particular I get this error:
An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.
/Views/Shared/_Layout.cshtml
'IHtmlHelper<dynamic>' does not contain a definition for 'ApplicationInsightsJavaScript' and no extension method 'ApplicationInsightsJavaScript' accepting a first argument of type 'IHtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)
+
#Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
Show compilation source
#pragma checksum "/Views/Shared/_Layout.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "050a2afdfb4a44e17be72e76627a3d3a0d0b7d5a"
namespace AspNetCore
{
#line 1 "/Views/_ViewImports.cshtml"
Screen:
Upgrade from project.json to new csproj and using new Visual Studio 2017 doesn't help.
It looks like ApplicationInsightsJavaScript was basically removed from API. How do I enable javascript application insights then?
There are breaking changes from ApplicationInsights 1.x to 2.x which are documented on GitHub release notes.
This release contains a rewrite of the SDK internals for better .NET
Core integration and initialization.
UseApplicationInsightsRequestTelemetry is obsolete, the logic it used
to perform is handled automatically now and calls to this method
should be deleted from Startup.cs.
UseApplicationInsightsExceptionTelemetry is obsolete, exception
telemetry is handled automatically internally now. You should delete
calls to this method from Startup.cs otherwise you will get duplicate
exception telemetry reported.
The MVC dependency for the JavaScript
snippet has been removed so in order to include the JavaScript snippet
now you need to make the following changes:
In _ViewImports.cshtml replace #inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration with #inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
In _Layout.cshtml replace
#Html.ApplicationInsightsJavaScript(TelemetryConfiguration) with #Html.Raw(JavaScriptSnippet.FullScript)

Categories

Resources