voltagex
voltagex
CC#
Created by voltagex on 6/24/2024 in #help
LibraryImport examples, especially on Linux
Has anyone got examples of usage of the newer LibraryImport infrastructure (?) as opposed to DllImport? I have seen https://stackoverflow.com/questions/75304403/marshalling-function-pointers-with-net-7-libraryimport which just about melted my face off and https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation isn't bad but no one seems to be using this in open source code.
85 replies
CC#
Created by voltagex on 4/6/2024 in #help
✅ NotSupportedException: JsonTypeInfo metadata for type... & "minimal" APIs
Coming back to C# after quite a while and minimal APIs got complicated. What's going on with the error here? What does it actually want me to add? Why is it trying to serialize a System.Func?
NotSupportedException: JsonTypeInfo metadata for type 'System.Func`2[Microsoft.Extensions.Options.IOptions`1[filelistclient.FileLocationOptions],System.String]' was not provided by TypeInfoResolver of type '[filelistclient.UnneededAddedComplexityJsonContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
NotSupportedException: JsonTypeInfo metadata for type 'System.Func`2[Microsoft.Extensions.Options.IOptions`1[filelistclient.FileLocationOptions],System.String]' was not provided by TypeInfoResolver of type '[filelistclient.UnneededAddedComplexityJsonContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.TypeInfoResolverChain.Insert(0, UnneededAddedComplexityJsonContext.Default));
builder.Services.AddOptions<FileLocationOptions>().BindConfiguration("FileLocation");
var app = builder.Build();


var testApi = app.MapGroup("/options");
testApi.MapGet("/", () => GetOptions);
app.Run();
}

private static string GetOptions(IOptions<FileLocationOptions> options)
{
return $"{options.Value.PathToScan}, {options.Value.SaveScanToPath}";
}
}
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.TypeInfoResolverChain.Insert(0, UnneededAddedComplexityJsonContext.Default));
builder.Services.AddOptions<FileLocationOptions>().BindConfiguration("FileLocation");
var app = builder.Build();


var testApi = app.MapGroup("/options");
testApi.MapGet("/", () => GetOptions);
app.Run();
}

private static string GetOptions(IOptions<FileLocationOptions> options)
{
return $"{options.Value.PathToScan}, {options.Value.SaveScanToPath}";
}
}
namespace filelistclient
{
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(FileLocationOptions))]
[JsonSerializable(typeof(IOptions<FileLocationOptions>))]
internal partial class UnneededAddedComplexityJsonContext : JsonSerializerContext
{
}
}
namespace filelistclient
{
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(FileLocationOptions))]
[JsonSerializable(typeof(IOptions<FileLocationOptions>))]
internal partial class UnneededAddedComplexityJsonContext : JsonSerializerContext
{
}
}
5 replies
CC#
Created by voltagex on 1/8/2023 in #help
❔ ILogger is never enabled
Any idea what's going on here? https://github.com/dotnet/runtime/issues/80336
using Microsoft.AspNetCore.Mvc;

namespace TestWebApp
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthorization();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (HttpContext httpContext, [FromServices] ILogger<Program> logger) =>
{
logger.LogTrace("Trace event from /weatherforecast");
Console.WriteLine($"ILogger enabled for trace? {logger.IsEnabled(LogLevel.Trace)}");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
});

app.Run();
}
}
}
using Microsoft.AspNetCore.Mvc;

namespace TestWebApp
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthorization();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", (HttpContext httpContext, [FromServices] ILogger<Program> logger) =>
{
logger.LogTrace("Trace event from /weatherforecast");
Console.WriteLine($"ILogger enabled for trace? {logger.IsEnabled(LogLevel.Trace)}");
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
});

app.Run();
}
}
}
3 replies
CC#
Created by voltagex on 9/22/2022 in #help
System.CommandLine and injection of IOptions (not Option!) type
I'm trying to work out how to use the hosting and dependency injection parts of System.CommandLine to inject IOptions configuration settings into my app via the command line. See https://github.com/dotnet/command-line-api/issues/1858#issuecomment-1254653273. Short version: how can I make System.CommandLine binding or dependency injection work to change the value of a typed IOptions instance?
1 replies
CC#
Created by voltagex on 8/20/2022 in #help
[solved] Injecting configuration in (minimal) WebAPI
https://git.sr.ht/~voltagex/wherethefile/tree/main/item/src/WhereTheFile.NetworkScanner/Program.cs#L24 How can I make builder.Services.Configure do what I think it should? i.e. when the service is resolved, a configuration object with my configuration is provided? Alternatively, how should I be passing database configuration strings in?
3 replies