Esa
✅ Legacy project, `using System.Net.HTTP` does not compile. What do?
Hi there, I'm setting up the dev environment for a legacy project. There's one using directive in it that does not compile, and that is
using System.Net.HTTP
. Now this does compile for a colleague of mine, and we both have .net fx4.8 SDK installed.
I'm not quite sure where to go from here. Any suggestions?62 replies
dotnet publish fails due to a build error that doesn't happen when I run just dotnet build
Hi there,
I'm having a weird thing going on here.
I use Jetbrains Rider, and have a project that uses a class library a colleague made. The error that is happening says that
Required member X.Y must be set in the object initializer or attribute constructor
. And this is super weird to see as an Error and not a warning, because 1) we never instantiate that class, it's resolved through DI and 2) this works in a regular build. So it's only during the publish part this goes wrong.
I don't even know where to begin troubleshooting this. Any advice?18 replies
Need help debugging a tiny console app on server
Halp. I'm losing my mind rapidly.
Scenario: I'm writing a tcp client tester to PoC something, and I've written it as a tiny console application where I use dependency injection to see that that aspect also works.
This is how I normally do it.
However... When I run this exe on the target server, it fails on the Validation step. It says the values are default. And this is where it gets weird. If I replace that neat configuration oneliner with an explicit behemoth that manually gets the required section, retrieves the string and binds it to the property - then it works:
But attempting to oneline it causes the error where everything is default, and thus failing validation.
This only happens on the target server which has a .net 8.0 x64 runtime installed, whereas I have a .net 8.0 x64 SDK installed on my dev environment.
Does this make sense to anyone? I don't really know where to start looking, apart from installing an SDK on the server to see - but that sounds very wrong lol.
41 replies
HttpContext and TraceIdentifier
Anybody familiar with how the TraceId of HttpContext works?
I am aware it represents a {TraceIdentifier}:{Span} format with the former being the overall identifier of an Activity and the latter being a specific sub-operation performed in the context of that activity. But I don't understand when one activity starts and another one stops.
I tried performing multiple repeat API requests through swagger, and they all got the same TraceIdentifier but incrementing Spans.
Can anyone clarify what is happening? I have a common .net 8.0 setup of program.cs for a WebAPI solution.
5 replies
BinarySerialization - need some design help
Hi, this is my first foray into lower level programming and I could use some help.
I am writing a DOCSIS-compliant binary serializer for C#. It will take as input a
.cfg
or .txt
file that resembles a JSON, but is a bit different:
This is a barebones, minified example of what these files will contain. Every line is either a property or a collection of properties.
I will refer to these properties as DocsisProperty<T>
, since they will be deserialized into a C# type and then encoded into byte[]
later on.
Now the issue I am facing is that there are 25 ish encoding methods and 25 decoding methods, but hundreds of different properties. All unique properties are stored in a property map with name, id and a reference to the correct method for encoding or decoding.
This can be seen here: https://github.com/rlaager/docsis/blob/master/src/docsis_symtable.h
My original idea was that when I am parsing a file, I can initialize a new DocsisProperty<T>
based on the identifier of the value, then later on when I want to encode it I call docsisProperty.Encode()
, and it will look up some static dictionary to figure out which encoding method is the appropriate one. However that gets complicated quickly because of the generic type on DocsisProperty<T>
. The dictionary would then have to use object
or dynamic
as it's return type, and I am not a fan of either approach.57 replies
✅ class library and exposing a type from another namespace
Hi,
in my company we have an underlying core application which runs things. This application has many modules (.dlls) which have apis we can call. So we usually reference those binaries in our solutions. However the way this is done currently is by downloading the binaries from an internal repository and referencing them in our solutions.
I'd like to move this entire process to our private nuget, so that we never have to touch a binary manually again. So currently I've setup a release pipeline to that private nuget source.
Consider the following modules:
I have created a class library solution that mirrors this somewhat:
So what I want to achieve is that a developer that needs
ICustomersService
in their solution can install my nuget package in their solution, and use my extension methods on IServiceCollection
to add ICustomersService
to the DI in their .net 7.0 project. And it works so far.
However, when I'm testing this from a different project that has installed my nuget package, I don't have access to the type ICustomersService
. I think that is because it doesn't come from the same namespace and is hidden behind my abstraction layer, so how can I expose this?2 replies
Need help structuring a class library for internal tools
Hi there, I'm working on creating an internal tool for exposing some commonly used functionality that requires external binaries.
So we have a business critical core application that consists of many modules. These modules all have their own APIs available through .dll files. Assume
Customers.dll
, Finance.dll
, Products.dll
and so on. Each one of these have IProductService
and IProductConfigurationService
for example.
When we create solutions, tools or integrations we often use some subselection of these modules, but rarely all. Currently we bake this into the solutions by moving the relevant binaries we'll be using into the repository so that it'll be available to the CI / CD pipelines.
But ideally I'd want to have this as a nuget package from our private nuget repo instead. So instead of finding the binaries we need and physically move them into our repo, I'd prefer something like nuget install OurCoreApp.Customers
, nuget install OurCoreApp.Products
and for that to add the required binaries. That means we only have to update binaries in one place (the OurCoreApp
solution that we use as a nuget package).
However I've never really done this before. I assume this means I'll create a class library called OurCoreApp
, but how do I make the different modules available as separate nuget packages?3 replies
✅ Help with authentication/authorization for my application with .net 7
Hi,
I was hoping someone could help me out with Claims/Identity/Roles.
I have an application with a couple system users. These users pass an API Key along their requests.
Through this api key, I find their actual user in a DB. In addition to the
User
table, I have a Feature
table and a UserFeature
table that connects a user with some feature of the application. This is how I can see that userA has access to the feature CustomerSearch for example.
I would like to move away from gnarly if-checks in the controller to see if a UserFeature entity exists with the userId, and instead make use of the annotations found in .net on the controller endpoints so that I can simply annotate that a method requires a certain feature. Any ideas?39 replies
❔ Locking of serialized inventory
Hi, me and a colleague are discussing the topic of locking. In my mind locking is just a simple means to an end that shouldn't require much magic.
Our usecase is that we have a database of entities with serial numbers. We support read/write operations on these, but whenever one entity receives a write operation we want to lock that entity until the write operation is done in a way that blocks concurrent write operations for specifically only that entity but no other entities.
We also use async/await a fair bit, so we cannot perform our logic inside a lock-block. So I opted for a ConcurrentDictionary<string, SemaphoreSlim> where string is the key we use to lookup the entries, and a semaphoreslim blocks access with its timeout set superlow (1 nanosec). This is obviously a hack and my suggestion doesn't feel right. But adding a full lock-class also feels overkill and like we're missing something obvious.
Behold my hacky suggestion:
Any thoughts?
2 replies
❔ How to handle longrunning tasks and cancellation in asp.net core?
Hi,
one of the applications we have at work has a long-running background task. The application is an older fx472 app where we just have a
public static Task LongRunningTask;
and public static CancellationTokenSource source;
, and these are defined in global.asax.cs
which i'm now trying to migrate to Startup.cs
instead.
This task is not dependent on user input and just silently runs every hour.
How does one usually handle long-running tasks that may need to be cancelled in asp.net core (.net 7.0)?12 replies
❔ Mocking with EntityFrameworkCore
Hm,
I'm creating unit tests for a class that has a
private readonly MyDbContext dbContext;
that causes a few problems.
And you can't really mock that as you would everything else, because you cannot instantiate any DbSet collections as they don't have a non-private constructor.
Anybody who has mocked stuff with DbContexts on them? I'm not sure how to approach this.4 replies
❔ Need help with a LINQ query
Hi,
I could use a bit of help building a LINQ query.
Assume a simple entity like this:
Then you have an IEnumerable<Event>. From this collection I want to extract all customer ids that only have eventTypeId == 1 associated with them.
So if there are two events for one customerId, and those two eventTypeIds are 1 and 2 then I don't want that associated customer id.
If both eventTypeIds are 1 then I want that specific customerId. Does that make sense?
Currently I have this
But looking at it it doesn't really make sense. And it doesn't return what I thought it would. Any ideas?
21 replies
❔ EntityFramework async where possible?
So I want to make sure I don't do something stupid here.
I'm trying to convert my DbContext-calls to async calls where possible.
I see EF provides
.FirstOrDefaultAsync()
which works nicely for the Get calls I have.
However I need to do stuff like join
and where
which I see has no async overload.
I started doing
but that feels wrong. How do I make this particular call async without doing something explosively stupid?23 replies
❔ Help with designing a functional container object
Hi,
I could use some input for a functional container/result object I'm working on. I want it (the struct itself) to never be able to be null or contain null, but this is a situation I'm not quite sure how to handle.
It's meant to return the result of invoking a function, but some functions will evaluate to null and I'm not sure how to model that. 🤔
I know that functions that directly accept a
T
value can throw an exception if that is null as it can be seen in compiletime, but in this case I'm not quite sure what should happen as I cannot check a Func
for null until it is actually ran and then it is too late.
3 replies
How can I inject methodname into ILogger.Debug()-call without changing useage everywhere?
Hi,
I have some property I wish to inject into my log messages (Serilog). I found a way to obtain it, but it requires a new method:
The idea here is that I can enrich the log template with the method name of the callsite. But this isn't the same Debug method found in the ILogger interface - so I'll have to use this method instead of that one. Is there a better way to achieve the same without using this method (since I'll have to add a
using
statement to use it)?2 replies