GetSection.Bind not working in .NET 4.7.2 - c#

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.

Related

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

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.

CircuitHandler Equivalent for .NET 6?

Where is CircuitHandler in Blazor .NET 6?
As I am upgrading packages to .NET 6, I noticed in nuget package manager that Microsoft.AspNetCore.Components.Server package was marked obsolete and so assuming CircuitHandler was moved to another package and that one is no longer needed (As they have done in the past with IAsyncEnumerable and Span<T>), I removed the package. Then I realized that package has completely been removed from nuget browser altogether, and there is no word anywhere I can find on any alternative for .NET 6. MSDN even has Blazor .NET 6 articles talking as if CircuitHandler is easily found in Microsoft.AspNetCore.Components.Server namespace, but no mention of what package or how to get access to that namespace! Example here
I figured this out, as I noticed the type exists in my BlazorApp project, but not my library project. It was a matter of figuring out what the differences are, and it turns out that you need to put <Project Sdk="Microsoft.NET.Sdk.Web"> in the project file instead of <Project Sdk="Microsoft.NET.Sdk">. So just adding the .Web part fixed the issue!
Note: Now my library won't build because it says I need a static Main method entry point. So it seems they aren't allowing CircuitHandlers in class libraries anymore, they must all reside in the Web App itself. Total bummer!
The real solution is to add the following to your project file. What a runaround this was for me to figure out! And I could not find this documented anywhere!
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Could not load type 'System.Web.HttpPostedFileBase' from assembly System.Web

I am working on a solution which has two projects, one is a .NET Core 3 project and the other is a .NET Framework 4.7.2 project.
The .NET Framework 4.7.2 project has a class which uses HttpPostedFileBase on a method.
The problem is that when I use Autofac to register that class on my .NET Core project, I get this error:
Could not load type 'System.Web.HttpPostedFileBase' from assembly
'System.Web, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
I am aware that HttpPostedFileBase does not exist on System.Web on .NET Core 3, so my guess is that when I try to register the class with Autofac, the System.Web being used is the one from my .NET Core project and not the one from the .NET Framework project. Would it be viable to try to use only the System.Web from the other project, into my .NET Core project?
I've tried adding the System.Web.dll from the other project, in to my .NET Core project but I get the error:
One or more errors occurred. The reference is invalid or unsupported.
I hope this answer helps you get on the right track.
Few options to resolve your problem come to my mind:
1. Completely abandon .NET Framework
Change project using .NET Framework 4.7.2 to use .NET Standard if that is possible and rewrite method in question completlty abandoning .NET Standard. This is in some ways easier options and I advise you to take it if possible.
2. Target both .NET Standard and .NET Framework
If this is not possible, because of some issues e.g. project is used in other solutions that cannot target .NET Standardyou can make this project target both platforms.
You can do second option through VisualStudio or directly in csproj file editing TargetFramework attribute in newer csproj format (I strongly advise you to migrate project to new format with some wizard). Then you should add condition on ItemGroup in csproj to only include System.Web if you are using .NET Framework 4.7.2 and .NET Standard counterpart if using .NET Standard. Then write the method in question again using .NET Standard, but do not delete the old one. Instead you should make compiler choose the right one with #if compiler instructions.
Below you can find examples with new csproj format. If you use old one you can use e.g THIS WIZARD to automatically migrate.
<TargetFramework>net472</TargetFramework>
Should be changed to (mind s):
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
And then to include System.Web only when using net472:
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Reference Include="System.Web" />
</ItemGroup>
And later the same for .NET Standard.
Last part should be done in C# code itself:
#if NETFRAMEWORK
... method using **HttpPostedFileBase** ...
#elif NETSTANDARD
... its .NET Standard counterpart ...
#endif
Constants used above - NETSTANDARD and NETFRAMEWORK are wildcards for more specific version of mentioned platforms. You can find them all HERE
You might be forced to do similar things with callers of method in question.
If those things do not help you might want to check assembly binding redirects if you have any as this issue I did post about might not be the only one there is.
Good luck, I hope my answer helps in any way.
for some reason when I had registered the class I was talking about with Autofac like this: builder.RegisterType() .EnableClassInterceptors().InterceptedBy(typeof(LogInterceptors), typeof(CacheInterceptor)); and then removed the class interceptors and registered the class like builder.RegisterType(); , then the error disappeared.

WCF WebService with warnings and InvalidOperationException

I am currently trying to connect my ASP-NET Core 2 application to Magento's API (WSDL v2), but keep receiving the following errors:
*Doesn't matter which method I call, even the new PortTypeClient().loginAsync(username, password); throws that, with this random endpoint name salesOrderShipmentAddComment.
InvalidOperationException: The operation
'salesOrderShipmentAddComment' could not be loaded because it
specifies \"rpc-style\" in \"literal\" mode, but uses message contract
types or the System.ServiceModel.Channels.Message. This combination is
disallowed -- specify a different value for style or use parameters
other than message contract types or
System.ServiceModel.Channels.Message.
However, using SoapUI, postman or importing with framework 4.6, it works perfectly.
Using the Magento v1 API it works too, but I don't want to use this version.
During the import in asp-net core, I also recieve those warnings (importing v1 doesn't shows any warning):
I'm almost creating a new 4.6 application, hosting it separately to work as a bridge between my application and Magento, even if it hurts my performance and architeture.
This is how I'm doing the import:
*Obs: In Data Type Options, I also tried checking different options, and selecting other data types.
Anything would help...
Thanks in advance
I had the same problem. Make sure you update your Nuget Packages to 4.5.3. After, works correctly for me.
Indeed updating the following packages to version 4.5.3 seems to resolve the issue:
System.ServiceModel.Duplex
System.ServiceModel.Http
System.ServiceModel.NetTcp
System.ServiceModel.Security
Seems like 4.4.4 version of those packages are installed by default, while adding WCF service reference via VS 2017 15.9.4 UI
Just update System.ServiceModel.Http to 4.5.3
According to
https://ozguradem.net/english/coding/2018/11/06/soap-services-with-dotnet-core/
Open .csproj file and edit following packages minimum version like these
<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.5.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.5.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.5.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.5.*" />
</ItemGroup>

Unable to declare an ArrayList using Code on OSX

I'm using Code on OSX together with the .NET Core. While trying to create an ArrayList I'm getting an error that the namespace cannot be found.
The System.Collections is used already. While examining what is suggested from System.Collections I see that not all of the methods are listed as in the documentation.
What could be the problem? I installed the latest .NET Core from MS site.
You cannot use the type because it is defined in a separate NuGet package,
http://www.nuget.org/packages/System.Collections.NonGeneric/
Note that only a few platforms are supported by this package, so for some monikers, you cannot reference it.
The recommended approach is to switch to the generic version of the collection types.
Looks like System.Collections.NonGeneric is not supported in Mac. It didn't work for me even though I had that package installed. Looks like it's deprecated and also Microsoft suggests that to use Generic lists instead.
Open you .csproj and add the following under
<ItemGroup>
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0">
</PackageReference>
</ItemGroup>
in the terminal or command prompt, make sure you are on the same working directory as your project and run
# dotnet restore
You can now use it in your project

Categories

Resources