DotNet Core 2 Console Application Configuration - .net-core-2.0

The Core 1.x configuration has been answered here
ASP.NET Core configuration for .NET Core console application
Unfortunately the Core 2.0.0 API and architecture has changed slightly and by the looks of this article that it uses Dependency Injection on the Startup class now (on a WebAPI based project) which is allot simpler.
But I am not sure how to do that on a Console application as there is no underlying DI. When I try to use the following code none of the methods exist on the new 2.0.0
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true) -ERROR
.AddJsonFile($"appsettings.{environmentName}.json", true, true) -ERROR
.AddEnvironmentVariables(); -ERROR
Does any body know how to add appsettings.json into the ConfigurationBuilder?

Turns out I have to manually add this package which contains the AddJsonFile extension - For some reason Intellisense did not suggest installing this package as it does with other things.
dependencies {
"Microsoft.Extensions.Configuration.Json": "2.0.0"
}

Related

Unable to display APIs in Azure Function App Swagger/OpenAPI

We have an Azure Func app hosted in Linux Premium plan, private network, using only https, but the Swagger UI cannot display APIs like below:
The settings below are set based on the link below, but the error above still occurs.
{
"Values": {
"OpenApi__ForceHttps": "true",
"OpenApi__ForceHttp": "false",
"OpenApi__HostNames": "/api"
}
}
https://github.com/Azure/azure-functions-openapi-extension/blob/main/docs/openapi.md
Settings
.NET Core 3.x
Microsoft.Azure.WebJobs.Extensions.OpenApi 1.0
Refs:
https://github.com/Azure/azure-functions-openapi-extension/blob/main/docs/openapi-core.md
https://github.com/Azure/Azure-Functions/issues/1933
https://github.com/Azure/azure-functions-openapi-extension/issues/352
I faced a similar issue recently caused by a new package reference added to my project: my API operations list suddently became empty, without any error when the swagger (or open API 3.0) file was generated.
The package causing this issue is Microsoft.Azure.WebJobs, with version >= 3.0.32. (issue with 3.0.32 and 3.0.33 which is the current latest version).
Note: I did not use this package directly as it is already a dependency used by other packages of my project, but it was upgraded to a version >= 3.0.32 when I referenced Microsoft.Azure.WebJobs.Extensions.Storage.Blobs in version '5.0.1', see dependencies below:
The workaround I used in my case was to use version 5.0.0 (instead of 5.0.1) of Microsoft.Azure.WebJobs.Extensions.Storage.Blobs, which needs Microsoft.Azure.WebJobs in version >= 3.0.30 only

How to control Adaptive Sampling in .NET Core application

I'm trying to limit the amount of data sent to Application Insights in a .NET Core program. I'm attempting to follow the documention here which says I should use the UseAdaptiveSampling method. It has the rather cryptic instruction:
Use extension methods of TelemetryProcessorChainBuilder as shown below to customize sampling behavior.
However, it doesn't tell me where exactly this extension method lives. My code is as follows:
public AppInsightsStats(string appInsightsKey)
{
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = appInsightsKey;
telemetry = new TelemetryClient(configuration);
// Enable sampling since amount of logging is massive
var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5); // <-- Compiler error here
}
However, the code doesn't compile since UseAdaptiveSampling isn't found. I have the following using statements:
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
The source code I can dig up that uses UseAdaptiveSampling all seems to be .NET Framework 4.5 code, so I'm wondering if this just isn't included with the .NET Core version. These instructions are for ASP.NET Core, but I'm wondering if they mean running that on the .NET Framework on Windows.
For console project, you should use this package Microsoft.ApplicationInsights.WorkerService. It's used for non-http application like console project.
I used your code with this package, everything is ok. Please give it a try, and let me know if you still have the issue.

OpenIdConnectOptions Missing from .net core sample application on linux

I am trying to follow this tutorial for Azure AD authentication in a .NET core web app:
https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/aspnetcore2-2
and I get this error:
The type or namespace name 'OpenIdConnectOptions' could not be found (are you missing a using directive or an assembly reference?)
I am using Linux and have no issue getting the basic templates to generate and build, the issue appears to arise where the tutorial says to add this line:
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Authority = options.Authority + "/v2.0/";
options.TokenValidationParameters.ValidateIssuer = false;
});
I have tried adding packages (semi randomly) e.g.:
Microsoft.IdentityModel.Protocols.OpenIdConnect
Microsoft.Owin.Security.OpenIdConnect
to no avail as the error persists and I get new issues like:
Error: Package 'Microsoft.Owin 4.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.
I am not sure if I am missing some basics with authentication and running .net core on linux, I am new to this and just trying to get the ball rolling with a working demo that actually does something :)
I have not checked on linux yet, but the nuget package you are missing should be produced by ASP.NET Core from the following project: https://github.com/aspnet/AADIntegration. Did you try to update the latest nuget package of ASP.NET Core ?
As a workaround you might want to have a look at the following branch of the sample (which still has the code which was now moved to ASP.NET Core AADIntegration) : https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master
ran into the same issue. After installing these modules, it compiled.

Missing Functions and Changes in Azure Storage SDK 7.2.1

Recently we have started upgrading our projects from ASP.NET 4.5 to ASP.NET Core and we are targeting the .NET Standard 1.6 framework. Most of the projects have been migrated but we are particularly facing issues with projects which have reference to Azure Storage SDK. Initially we were using SDK version 7.0.0, but since it was not supported in .NET Standard 1.6, we had to upgrade SDk to 7.2.1. But it seems that a lot of functions have been removed from the new version, for e.g. CloudTable.CreateQuery(). And also it seems that all the functions have been made async.
Is it an expected thing, or am I missing something here? Is there are a change or a upgrade document for all the changes that has been done? I could not find any document for all these changes.
Is it an expected thing, or am I missing something here?
Yes, this is an expected thing. We could check the CloudTable class in this article. And we could find CreateQuery function in that article. However we could not use CreateQuery function and we only could use async method. I think it is caused by the following, and this is a default behavior:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
As we know that, we only could use Azure storage async method in portable library and win8. In asp.net core it imports "portable-net45+win8". So I think this is a reason. If you update your application to Asp.net core. I think you need to rewrite the Azure Storage code.

Azure WebJobs ServiceBus returns Exception: found 2 DNS claims in authorization context

I'm trying to read a message from an Azure ServiceBus queue using an Azure WebJob but it's throwing and exception:
Unhandled Exception: System.InvalidOperationException: Found 2 DNS claims in authorization context.
I've set the correct connection strings named "AzureWebJobsServiceBus", "AzureWebJobsDashboard" and "AzureWebJobsStorage"
The WebJob Program code has been updated to use JobHostConfiguration:
class Program
{
static void Main()
{
var config = new JobHostConfiguration();
config.UseServiceBus();
var host = new JobHost(config);
host.RunAndBlock();
}
}
And the actual Job method
public class Functions
{
public async static Task ServiceBusResizeRequest(
[ServiceBusTrigger("blah")] string message,
TextWriter log
)
{
await log.WriteLineAsync("got message " + message);
}
}
I can successfully create and write to the queue via a separate console application.
But when I run the webjob application, it throws that exception.
Any ideas?
EDIT: Using .net 4.6.1
The answer marked as solution, is not the solution, it is a botched job.
The solution to use it in .Net Framework 4.6.1 is to add in the rutime block in App.config:
<AppContextSwitchOverrides value="Switch.System.IdentityModel.DisableMultipleDNSEntriesInSANCertificate=true" />
Read this article Mitigation: X509CertificiateClaimSet.FindClaims Method
Very IMPORTANT for now Azure WebApps / WebJob etc, doesn't support 4.6.1 I will note here when (said at jan 21, 2016).
It means, that you can develop a web job application with 4.6.1, but when you push it to Azure, you can see exceptions like Job failed due to exit code -2146232576
January 29th Microsoft released version 3.1.3 of the NuGet package WindowsAzure.ServiceBus.
From the release notes:
• General: .Net 4.6.1+ compatibility fix. Fixing custom DNS IdentityVerifier so that we honor multiple DNS claims returned by WIF
Upgrading the package solved the problem for us.
As outlined in this answer above, the snippet below does the trick
<runtime>
...
<AppContextSwitchOverrides value="Switch.System.DisableMultipleDNSEntriesInSANCertificate=true" />
...
<runtime>
BUT be carefull to add it to the correct project in your solution! Add it to the project containing the Azure code and Azure references.
Microsoft released a new package (under a new name) to fix this issue. So ...
remove the Microsoft.AspNet.SignalR.ServiceBus package,
install the Microsoft.AspNet.SignalR.ServiceBus3 package instead, and
upgrade the WindowsAzure.ServiceBus package.
More info here: https://github.com/SignalR/SignalR/issues/3548#issuecomment-296326048
Downgrading from .net 4.6.1 to 4.6 seems to prevent the issue from occurring.
Today, I ran into this issue and had no idea about it. Finally, I decided to upgrade all the Azure nuget packages that I am using (including webjobs, servicebus ...) and BOOM! it WORKS. Hopefully, it will help if anyone runs into this issue in the future
For me it started failing after I updated .NET Framework from 4.5.2 to 4.7
All I did to fix it was update the Nuget Package WindowsAzure.ServiceBus to 5.2.0

Resources