|
distortion park posted:Instead of depending directly on those transient view models, you could depend on a view model factory (or one per VM depending on if the dependencies have much overlap). There are a few different ways to set this up depending on exact requirements and preferences, there are a bunch explained here: I use MS DI all the time for small console applications. All you need to do is use a .NET Generic Host. https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host A lot of aspnet core's basic mechanisms are actually baked into the generic host, like configuration, DI and logging. It makes it really easy to bootstrap the app and lets you share configuration files, use secrets.json, etc. The defaults even plug into the windows event log so I can easily follow up errors in, say, a scheduled task. In theory you're expected to use the generic host to launch a IHostedService, but nothing stops you from using AddTransient to register your main class and then use a single service locator to load it and execute it manually. You can even open a new scope to do prelaunch tasks in their own scope before you move onto the actual program. I use the latter to do "on boot" tasks in asp.net programs, too. (Stuff like rolling back uncommitted file uploads/deletions in case the server shut down or crashed midway) Kyte fucked around with this message at 22:19 on Jan 23, 2022 |
# ? Jan 23, 2022 22:11 |
|
|
# ? Jun 1, 2024 14:53 |
|
Dependency Injection can absolutely have the same problems, what I look for is being able to use Find all References and similar stuff to figure out the dependency graph if that makes sense. And the biggest culprit there is a bazillion internal nugets without symbols and using reflection/“conventions”/“magic” to wire up the DI-container. (I’m OK with stuff like Controllers, but please don’t go overboard” I do not want to end up at a constructor that has zero usages! It’s not that hard to have a Bootstrap.cs with a method wiring everything up…
|
# ? Jan 23, 2022 23:46 |
|
Thanks for all the responses.quote:you could depend on a view model factory Yep. Here's the final implementation of the factory: C# code:
quote:your ViewModelFactory should either have the ViewModel's dependencies as its own dependencies, and then new one up as required Not necessary. GetRequiredService instantiates the class with all of its (constructor-injected) dependencies. If you want to know the dependencies of a particular VM, check its constructor (MainViewModel (sorta) excepted). quote:All you need to do is use a .NET Generic Host Interesting, I'll definitely check it out. I currently use this to generate a configuration: C# code:
|
# ? Jan 24, 2022 14:28 |
|
LongSack posted:Thanks for all the responses. You can't see ViewModelFactory's dependencies from its constructor though, I thought that was what you were trying to resolve?
|
# ? Jan 24, 2022 14:58 |
|
distortion park posted:You can't see ViewModelFactory's dependencies from its constructor though, I thought that was what you were trying to resolve? It looks like view model factory is just a wrapper around service locator. What s/he’s saying is you can look at the constructor of a view model to see what it depends on, which is what s/he was looking to achieve.
|
# ? Jan 24, 2022 15:34 |
|
epswing posted:It looks like view model factory is just a wrapper around service locator. What s/he’s saying is you can look at the constructor of a view model to see what it depends on, which is what s/he was looking to achieve. I don't really see the benefit in wrapping it like that in that case - just use it directly in your base Application
|
# ? Jan 24, 2022 16:47 |
|
LongSack posted:Interesting, I'll definitely check it out. I currently use this to generate a configuration: I usually let CreateDefaultBuilder() add it, but if you don't wanna use it you can manually install the Microsoft.Extensions.Configuration.UserSecrets nuget, add the AddUserSecrets() line and then refer to the asp.net docs. (I think if you use the right click -> manage user secrets option in Visual Studio it'll actually wire up most of it for you) Kyte fucked around with this message at 17:34 on Jan 24, 2022 |
# ? Jan 24, 2022 17:32 |
|
distortion park posted:I don't really see the benefit in wrapping it like that in that case - just use it directly in your base Application Honestly, you're right, but the factory method looks cleaner (to me) C# code:
C# code:
|
# ? Jan 24, 2022 21:41 |
|
Kyte posted:I usually let CreateDefaultBuilder() add it, but if you don't wanna use it you can manually install the Microsoft.Extensions.Configuration.UserSecrets nuget, add the AddUserSecrets() line and then refer to the asp.net docs. Ahh, I'm missing the nuget package! Thanks. Edit: User secrets are really not designed to be used outside of net projects. According to some google research, it can be done, but it involves a lot of manual steps. Simply right-clicking and choosing “Manage User Secrets” does not work. LongSack fucked around with this message at 00:41 on Jan 26, 2022 |
# ? Jan 24, 2022 21:42 |
|
This might not make any sense but I'm watching this video on state management in Blazor: https://www.youtube.com/watch?v=GIupo55GTro He outlines three methods, but then infuriatingly he doesn't actually show enough code for the third method, which is a hybrid of the first two. I can't find the code online anywhere. The first method uses a cascading parameter like this (cut down): CascadingAppState.razor C# code:
C# code:
AppState.cs C# code:
C# code:
"Use AppState Service as Property of Cascading Component" (he also uses LocalStorage to store the state, but I'm not interested in that) I can see that CascadingAppState.razor now includes the AppState Service as a property like this: C# code:
Component.razor C# code:
I think I'm missing something from CascadingAppState.cs to initialize or hook it up to AppState somehow? I thought maybe I needed to inject AppState as a dependency but it doesn't look like he has an @inject statement... Any ideas? e: you know what I think maybe the hybrid approach isn't what I wanted anyway so maybe it doesn't matter. I thought the point was that the CascadingParameter would take care of UI updates automatically and then there would be separate events in AppState for special circumstances. But it looks like you need events just to update the UI anyway so it's easier just to use a single AppState service I think and forget the CascadingParameter. fuf fucked around with this message at 15:59 on Jan 26, 2022 |
# ? Jan 26, 2022 15:07 |
|
I've got an ASP.NET (Framework 4.8) MVC site, and blob storage, both running on Azure. I'm displaying a list of files in storage that the user can click on to download. When they do, I'm basically doing this (this code is spread across a few classes, but I've boiled it down to one method in a controller):C# code:
I've been attempting various solutions, but the usual googling is resulting in lots of different answers, half of which are .NET Core and don't apply, because the Azure storage libs are quite different. Which Stream should I be giving DownloadToStreamAsync? And how do I feed that back to the client without buffering?
|
# ? Jan 26, 2022 15:22 |
|
epswing posted:I've got an ASP.NET (Framework 4.8) MVC site, and blob storage, both running on Azure. I'm displaying a list of files in storage that the user can click on to download. When they do, I'm basically doing this (this code is spread across a few classes, but I've boiled it down to one method in a controller):
|
# ? Jan 26, 2022 15:33 |
|
mystes posted:Can't you just pass the HttpResponse.OutputStream stream directly to DownloadToStreamAsync? Right, I was playing with Response.OutputSteam, something like this. I'm sure return File(Response.OutputStream, ...) is wrong. I get "System.NotSupportedException: Specified method is not supported" C# code:
C# code:
|
# ? Jan 26, 2022 16:04 |
|
David Fowler tweeted about this the other day, I think this is what you're looking for? https://twitter.com/davidfowl/status/1484985441832951810?s=21
|
# ? Jan 26, 2022 17:50 |
|
I think response.body is the .net core version of response.OutputStream
|
# ? Jan 26, 2022 19:44 |
|
Potassium Problems posted:David Fowler tweeted about this the other day, I think this is what you're looking for? Yep, that's what I want. But I don't know much about the newfangled .NET Core stuff. Really my question is how does this fit into a .NET Framework ASP.NET MVC controller?
|
# ? Jan 26, 2022 19:56 |
|
Whoops, I completely missed the .NET 4.8 part. Sorry!
|
# ? Jan 26, 2022 20:12 |
|
epswing posted:Right, I was playing with Response.OutputSteam, something like this. I'm sure return File(Response.OutputStream, ...) is wrong. I get "System.NotSupportedException: Specified method is not supported" We do this in one of our controllers and it's basically just C# code:
Note that you can also make blob storage give you a temporary access token to append to the blob's Uri by calling CloudBlobContainer.GetSharedAccessSignature and redirect the client to to get the resource directly from there without your server having to do anything else.
|
# ? Jan 26, 2022 21:58 |
|
epswing posted:I've got an ASP.NET (Framework 4.8) MVC site, and blob storage, both running on Azure. I'm displaying a list of files in storage that the user can click on to download. When they do, I'm basically doing this (this code is spread across a few classes, but I've boiled it down to one method in a controller): Another option is to use DownloadToFile() to a temp file and then use Response.TransmitFile. Or return a redirect to a SAS URI and the browser then downloads directly off Azure servers. Or use DownloadToStream and point it to Response.OutputStream? I'm not sure what'd you need to do there wrt buffering and headers and whatnot. You'd basically have to manually set up the headers, then do a loop with stream.Read(), Response.OutputStream.Write(), Response.Flush(), etc. Kyte fucked around with this message at 01:50 on Jan 28, 2022 |
# ? Jan 28, 2022 01:44 |
|
I think the redirect to SAS URI is probably the Right Way (isn’t this what it’s for?), and I don’t have to bother streaming the file through my server at all.
|
# ? Jan 28, 2022 02:36 |
|
Is there something like Python's casefold for strings in C#? ToLower() doesn't appear to work. Casefold will lowercase characters in other languages:code:
|
# ? Jan 28, 2022 07:04 |
|
Rocko Bonaparte posted:Is there something like Python's casefold for strings in C#? ToLower() doesn't appear to work. Casefold will lowercase characters in other languages: Does the culture-specific overload do what you need?
|
# ? Jan 28, 2022 08:12 |
|
If what's in System.Globalization isn't what you're after, you probably need to go directly to Normalizer2 from ICU. Here's an example using icu.net:C# code:
|
# ? Jan 28, 2022 08:32 |
|
You should never use the ToLower() or ToUpper() overloads that don't take a CultureInfo argument in C#. That is because their behaviour is influenced by the user's locale, but it is easy not to realise this when reading the code. You should use (as appropriate to the use case) ToLowerInvariant() or ToUpperInvariant(), or an overload of ToLower() or ToUpper() that takes a CultureInfo argument. A few years ago we released software to an important customer based in Denmark that used those overloads in internal serialization code. Some features were broken in certain locales, including Danish. Since then we implemented static analysis that errors on use of those overloads.
|
# ? Jan 28, 2022 11:51 |
|
Actually, I think that I am wrong, it was double.ToString() that was the problem on that occasion. In the aftermath of that, it was nevertheless decided that we would disallow the ToLower() and ToUpper() overloads without CultureInfo.
|
# ? Jan 28, 2022 11:57 |
|
it always tickles me that the "invariant culture" is america
|
# ? Jan 28, 2022 13:18 |
|
I once discovered an infinite loop in our code, responsible for de-duplicating slashes in file paths. The IndexOf call (our while loop condition) wasn't aware of the current locale but the replace operation was, so it failed to replace two slashes separated by... I want to say it was the Mongolian vowel separator character.
|
# ? Jan 28, 2022 15:32 |
|
redleader posted:it always tickles me that the "invariant culture" is america Is it, or is it the machine's current culture?
|
# ? Jan 28, 2022 16:05 |
|
insta posted:Is it, or is it the machine's current culture? It is based on EN-US conventions and is the same everywhere. If it were based on the machine's current culture then it would not be "invariant". The whole point of it is that it's the same (doesn't vary) regardless of the context in which the software is running, hence invariant.
|
# ? Jan 28, 2022 16:07 |
|
redleader posted:it always tickles me that the "invariant culture" is america System.Globalization.CultureInfo.IdgafAboutCulture
|
# ? Jan 28, 2022 16:09 |
NihilCredo posted:System.Globalization.CultureInfo.IdgafAboutCulture uncultured swine
|
|
# ? Jan 28, 2022 16:19 |
|
NihilCredo posted:System.Globalization.CultureInfo.WhitePrivilege
|
# ? Jan 28, 2022 18:15 |
|
redleader posted:it always tickles me that the "invariant culture" is america Not (entirely) when you're running Core on Linux where it uses https://en.wikipedia.org/wiki/Currency_sign_(typography) for currency instead of $
|
# ? Jan 28, 2022 19:20 |
|
fuf posted:This might not make any sense but I'm watching this video on state management in Blazor: I'm not 100% certain but his Blazortrain series might cover that topic with more complete examples. https://www.youtube.com/watch?v=BB4lK2kfKf0 https://www.youtube.com/watch?v=ib_6mYbkL2s
|
# ? Jan 29, 2022 17:34 |
|
I was looking at some 3+ year old internal library code at work yesterday that is breaking horribly. One thing that stands out is some ambient context that is held in a Dictionary wrapped in an AsyncLocal<...>. I get what async local is for and why we are using that rather than static or thread local (I think), but the weird thing is that it is defined as...code:
code:
|
# ? Jan 29, 2022 18:07 |
|
I can see that being adequate if it's being passed around, especially if you aren't adding keys. The AsyncLocal<>.Value property's get/set methods retrieve and store the actual references to the values on the current ExecutionContext, so there shouldn't be a memory leak concern. (Maybe if you're doing something weird with the event handler delegate, as that's the only state stored by an AsyncLocal instance.)
|
# ? Jan 30, 2022 01:32 |
|
Does anyone have big thoughts on Rider vs Visual Studio, especially for blazor development? I keep flipping between the two and can't really settle on one. Rider seems to be a lot faster and smoother than VS. But one thing I really like about VS is the code suggestions. Not just normal intellisense but when it will suggest an entire line for you based on the previous lines. Sometimes I'm amazed how accurate and helpful it is. I can't figure out if Rider has something similar? I use the vim extensions for both which adds some complications. I sort of half-learned vim about 15 years ago and now I'm stuck with it even though I'm not that good at it.
|
# ? Jan 31, 2022 11:23 |
|
Can anybody provide some insight as to why this FileSystemWatcher code won't fire on change or rename events, but works just fine for the created event? This folder is an FTP folder (network share, maybe that?) that customers upload files to, there's a delay before processing since some files are large and I want the files to be fully uploaded before trying to process them. I need the rename/change to fire because some FTP clients will upload with a .filepart extension, which then gets changed to the actual filetype extension, which then throws an error because the e.FullPath still has the .filepart extension on the raised create event but that file no longer exists so you get a System.IO.FileNotFoundException. Or maybe there's some other way to deal with that scenario I'm unaware of. code:
code:
Just-In-Timeberlake fucked around with this message at 15:57 on Jan 31, 2022 |
# ? Jan 31, 2022 15:37 |
|
fuf posted:Does anyone have big thoughts on Rider vs Visual Studio, especially for blazor development? Assuming there isn't licensing/cost issues, just pick one and stick with it. They're similar enough that you can work one if you know the other, but also once you're familiar enough with either of them you tend to be more productive in it than if you keep flipping back and forth and only know how to use the features they have in common. Using Rider will let you be more productive with features like its DataGrip-like integration, the various tooling integrations, some specific plugins, as well as custom run config/launching/etc configurations, etc etc. Using VS will let you be more productive with intellisense/roslyn-related integrations, better integration of source generators and some extensions around containers/etc, as well as better integration into certain platform you might be using (like Azure), etc etc. The only "big" thing in my mind besides that would be if you're having to work with legacy/specific tech where VS works correctly and Rider sometimes breaks. You mentioned Blazor, but even with Razor I've had nothing but issues in bigger projects. For example, right now in Rider I've got a .cshtml without anything fancy except for a local function used for templating; that just breaks the parsing so Rider keeps insisting there's wrong symbols/it won't build, but it builds it fine. Apparently it's RIDER-34875 which has been open for 2 years and isn't any closer to a fix (and has an unsuitable workaround).
|
# ? Jan 31, 2022 16:40 |
|
|
# ? Jun 1, 2024 14:53 |
|
just spent my first serious 5 hours since high school learning programming, decided to start with c# for no other reason than accessibility. I learned on python and now that I have an incredibly rudimentary knowledge do you guys have any ideas of where to go from here?
|
# ? Feb 1, 2022 11:19 |