cow
cow
CC#
Created by cow on 4/30/2024 in #help
Prepend string to git commit messages via VS2022 git interface?
I want to prepend a string to each of my commits. Is this possible? I've been searching and it looks like you can achieve it with "git hooks", but git hooks are not supported with Visual Studio? My end goal is to have Jira ticket number prepended to each commit, but I need to start with just figuring how to prepend a basic string. Thanks
18 replies
CC#
Created by cow on 4/22/2024 in #help
✅ How to Deserialize dd-MM-yyyy in System.Text.Json?
Hi guys. It seems I can happily deserialize a yyyy-MM-dd formatted date in C# using System.Text.Json However, when I try with a dd-MM-yyyy formatted date, I get the error: The JSON value could not be converted to System.DateTime How can I accept both date formats in System.Text.Json?
41 replies
CC#
Created by cow on 3/15/2024 in #help
Possible to F12 into C# source?
No description
60 replies
CC#
Created by cow on 3/13/2024 in #help
Is there a tool to view c# generated code but still at high level?
No description
6 replies
CC#
Created by cow on 3/5/2024 in #help
From HttpClient, can I save/copy the request to share with someone?
Is there a way to copy raw request details? For example, I'm looking for a similar output like: - in Bruno you right-click a request and click "Generate Code" - in Postman you click the "view code" button on a request Thanks
25 replies
CC#
Created by cow on 2/6/2024 in #help
User-secrets per machine?
I have many solutions and projects that use the same sql connection string user/password. Currently I use DotNet User Secrets like this:
dotnet user-secrets set "sql-dev-user" "MyUser"
dotnet user-secrets set "sql-dev-pass" "MyPassword"
dotnet user-secrets set "sql-dev-user" "MyUser"
dotnet user-secrets set "sql-dev-pass" "MyPassword"
Which works fine, but it's at project level so I need to do it for each individual project, and I have many solutions/projects. And each dev would need to set those for each project Is there a way to set a global/machine-level User Secret? It would be great if all project could just read a central "sql-dev-user"/"sql-dev-pass"
81 replies
CC#
Created by cow on 2/1/2024 in #help
✅ Nice way to get an item from a list only if there is one, otherwise return null?
Is there something like SingleOrDefault() that returns default instead of exception?
var list = new List<char> { 'a', 'b', 'c', 'a' };

var oneOrNone = list.Where(x => x == 'a').SingleOrDefault();
//Exception: Sequence contains more than one element
var list = new List<char> { 'a', 'b', 'c', 'a' };

var oneOrNone = list.Where(x => x == 'a').SingleOrDefault();
//Exception: Sequence contains more than one element
This throws an exception if more than 1, however I want 'default' if more than 1
77 replies
CC#
Created by cow on 12/20/2023 in #help
Encrypted file/document storage options for asp.net core website?
Hi guys, I'm maintaining an asp.net core website and I've been told it's a requirement to encrypt stored documents "at rest". Currently we generate pdf's and store on the server disk, and serve them via the asp.net website. I'm very unfamiliar with modern document storage and looking for some suggestions to go research. What do you guys use? Could I just add bitlocker to the server drive and be done with it? Or does that come with big performance hit? Cheers
38 replies
CC#
Created by cow on 12/18/2023 in #help
Can you still create a SPA app inside an asp.net website? Or is that obsolete?
I've inherited some oldish Angular projects and was looking at upgrade options, and what the latest Visual Studio Angular template is like. It seems everything now is a Web API backend, and a separate Angular project for frontend? My projects have the Angular app as a folder inside the asp.net project, and when you hit debug or publish, both the asp.net and angular launch/publish together. I'm happy to abandon that old style of project if it's no longer recommended, i'm just struggling to find info about it Thanks
11 replies
CC#
Created by cow on 12/7/2023 in #help
How to create/get a class instance progamatically? (Maybe it's called a Factory?)
Hi guys, Based on string/switch, I want to get a class. I'm new to dependency injection and I'm struggling to find examples, probably because I'm searching for wrong keywords (maybe it's not called a factory?) This is what I have so far:
public class GenericReportFactory
{
public IGenericReport GetReport(IServiceProvider serviceProvider, string reportType)
{
switch (reportType)
{
case MonthlyReportType.ThingOne:
return serviceProvider.GetRequiredService<ThingOneReport>();
case MonthlyReportType.ThingTwo:
return serviceProvider.GetRequiredService<ThingTwoReport>();
default:
return null;
}
}
}
public class GenericReportFactory
{
public IGenericReport GetReport(IServiceProvider serviceProvider, string reportType)
{
switch (reportType)
{
case MonthlyReportType.ThingOne:
return serviceProvider.GetRequiredService<ThingOneReport>();
case MonthlyReportType.ThingTwo:
return serviceProvider.GetRequiredService<ThingTwoReport>();
default:
return null;
}
}
}
//DI setup has:
services.AddTransient<IGenericReport, ThingOneReport>();
services.AddTransient<IGenericReport, ThingTwoReport>();
services.AddTransient<IGenericReportFactory, GenericReportFactory>();
//DI setup has:
services.AddTransient<IGenericReport, ThingOneReport>();
services.AddTransient<IGenericReport, ThingTwoReport>();
services.AddTransient<IGenericReportFactory, GenericReportFactory>();
Currently I'm getting the error No service for type 'ThingOneReport' has been registered. I set a breakpoint at services.AddTransient<IGenericReport, ThingOneReport>(); and I confirm it definitely hits that line, so it should be registered Thanks
6 replies
CC#
Created by cow on 11/27/2023 in #help
Is there a way to ensure a function can't modify a parameter?
I often want to pass objects or values into a function, and make sure the function cannot change it... is it possible? Example scenario:
public void UpdateUserAge() {
var user = _dbContext.Users.Find(4);
user.Age = 111;

// How to make sure WriteUserLog can not modify user?
WriteUserLog(user);

_dbContext.SaveChanges()
]


private void WriteUserLog(UserEntity user) //can this parameter be made readonly somehow?
{
user.Name = "Shouldnt be able to change this";
Console.WriteLine($"User {user.Age} was changed")
}
public void UpdateUserAge() {
var user = _dbContext.Users.Find(4);
user.Age = 111;

// How to make sure WriteUserLog can not modify user?
WriteUserLog(user);

_dbContext.SaveChanges()
]


private void WriteUserLog(UserEntity user) //can this parameter be made readonly somehow?
{
user.Name = "Shouldnt be able to change this";
Console.WriteLine($"User {user.Age} was changed")
}
25 replies