Skip to main content

Posts

Azure Static Web App–Assign roles through an Azure Function

As a follow-up on the presentation I did at CloudBrew about Azure Static Web Apps I want to write a series of blog posts. Part I - Using the VS Code Extension Part II - Using the Astro Static Site Generator Part III  – Deploying to multiple environments Part IV – Password protect your environments Part V – Traffic splitting Part VI – Authentication using pre-configured providers Part VII – Application configuration using staticwebapp.config.json Part VIII – API Configuration Part IX – Injecting snippets Part X – Custom authentication Part XI – Authorization Part XII(this post) -  Assign roles through an Azure function We ended last post about Azure Static Web Apps talking about authorization and you can use role based security by assigning either a built-in role or a custom role. I showed how you could use invitations to assign a custom role. Today I want to show a second option to assign a custom role using an Azure Function. We
Recent posts

EF Core–.NET 8 update

The .NET 8 release of Entity Framework Core offers a large list of new features. The goal of this post is not to walk you through all these features, therefore you can have a look at the What's new page on Microsoft Learn , instead I want to talk about a specific feature as a follow-up on the previous posts about EF Core I did this week. In those posts I talked about the FromSql method to use your handwritten SQL statements to fetch EF Core entities. What I didn’t mention is that this only worked for entities that were registered as an entity to the DbContext. Starting with the EF Core 8 release, this condition has been removed, allowing us to create any SQL statements you want and map them to C# objects. This means that EF Core can now become an alternative to micro-ORM’s like Dapper . Of course there is maybe still a performance difference(I’ll do a benchmark and share the results) but feature wise this is a great addition. To use this feature, we need to call the SqlQue

EF Core - System.InvalidOperationException : The required column 'Id' was not present in the results of a 'FromSql' operation.

Yesterday I talked about an error I got when using the FromSql method in Entity Framework Core(EF Core). It allows you to execute raw SQL queries against a relational database. Here is the example I was using yesterday: Remark: The FromSql method was introduced in EF Core 7.0. In older versions, you should use FromSqlInterpolated instead. However when we tried to execute the code above it failed with the following error message: System.InvalidOperationException : The required column 'Id' was not present in the results of a 'FromSql' operation. You typically get the  error message above when there isn’t a matching column found in the SQL result for every property in your entity type. In our case the query was using a ‘SELECT *’ so I couldn’t be that we missed a column. To explain what was causing the issue, I first have to show our entity type and mapping class: As you can see there is a difference between the name of one of our columns(ProductID) and t

EF Core - Cannot convert from 'string' to 'System.FormattableString'

While doing a pair programming session with a new developer in one of my teams, we ended up with a compiler error after writing the following code: This is the error message we got: Argument 2: cannot convert from 'string' to 'System.FormattableString'   The fix was easy just add a ‘$’ before the query: However it would not have been a good pair programming session if we didn’t drill down further into this. What is a FormattableString? A FormattableString in C# is a type introduced in .NET 4.6. It represents a composite format string, which consists of fixed text intermixed with indexed placeholders (format items). These placeholders correspond to the objects in a list. The key features of FormattableString are: Capturing Information Before Formatting : A FormattableString captures both the format string (similar to what you’d pass to string.Format , e.g., "Hello, {0}" ) and the arguments that would be used to format it.

Dapper - Return dynamic data

For simple data fetching scenario's, I tend to keep away from Entity Framework and go the micro-ORM approach using libraries like Dapper . Today I had a small use case where I wanted to return some data using dynamic types in C# and handle it like a list of key-value pairs instead of using strongly typed objects.  The good news is that this is supported out-of-the-box in Dapper. Dapper gives you multiple overloads that returns a dynamic type. Here is an example using Query() : Simple but effective! Remark: If you are not really looking for a dynamic result set but don’t want to create a class or record type, you can also use value tuples to map the returned rows: More information Dapper Anonymous Result - Learn How to Map Result to Dynamic Object (dappertutorial.net)

Implementing the decorator pattern using System. Reflection.DispatchProxy

If you are new to the decorator pattern, let me start with a short explanation: Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors. A common use case for decorators is to implement Aspect Oriented Programming (AOP) which allows you to implement cross-cutting concerns like logging, caching, … There are multiple ways to implement this pattern, you can manually implement it, use your DI container , use a source generator to write the boilerplate code or use a dynamic proxy that wraps call to the original class. It’s this last approach I want to focus on in this blog post. You could use the great  Castle.DynamicProxy library but for simpler use cases, there is a built-in alternative through the System.Reflection.DispatchProxy class. Let’s have a look at a small code example on how to use this class. First we need to create a Decorator class that impl

AddConsole is obsolete: This method is retained only for compatibility

While working on a POC I got the following warning/error when trying to add some logging: Here is the exact error message: CS0619: ‘ConsoleLoggerExtensions.AddConsole(ILoggerFactory)’ is obsolete: ‘This method is retained only for compatibility. The recommended alternative is AddConsole(this ILoggingBuilder builder). There are multiple ways to get rid of this warning, but I solved it by using the following code: Happy coding!