|
epswing posted:What's the Right Way to trigger some code frequently (every X seconds) in an ASP.NET site? While there are plans to move it to an Azure App Service, and maybe WebJobs is what I'm looking for, it's running as a Site on IIS for now. You could try https://www.quartz-scheduler.net/ which is a bit more lightweight than Hangfire and runs through IHostedService. It can even share host with ASP.NET and access it directly via DI. IIRC it also supports in-memory, database and redis as backing stores. In summary it's basically everything explained above but without having to roll it on your own. Kyte fucked around with this message at 20:21 on Nov 17, 2021 |
# ¿ Nov 17, 2021 20:19 |
|
|
# ¿ May 17, 2024 01:17 |
|
fuf posted:Thanks for the suggestions guys. ToList is not atomic and assumes the source list is static throughout the lifetime of the operation. What's happening here is that the source list is getting new items in between the destination list's backing store being initialized and all the items being copied over, so eventually it reaches the end, except it's not the end and there's new items, tries to copy them and discovers it's gone beyond the allocated space. Then it crashes. You need to use a lock or similar synchronization mechanic to prevent new insertions while you're copying over, or switch to some collection that guarantees thread-safe enumeration (or maybe an immutable collection, which would be thread-safe by default). Using a simple for works, but it'll have different behaviors based on compiler optimizations. If the compiler checks .Length every time, then it'll loop over everything including whatever was added while the loop was executing. If the compiler optimizes .Length into a local, it'll only loop over the length at the beginning of the loop. And either way you're being saved from a crash because this list happens to only grow. If it ever shrinks the whole thing will blow up in your face. Better to do things properly. Kyte fucked around with this message at 15:53 on Nov 26, 2021 |
# ¿ Nov 26, 2021 15:50 |
|
My guess is because you were initializing a new DbContext the change tracker started at zero so it saw all the elements in the Tags collection and thought "these are all new Tags, gotta insert them all", so the problem wasn't the tags being added or removed but rather the tags that stayed. Loading the entity into the change tracker solves that because EF now knows which tags already exist in the DB. This is going off memory so might be wrong but I'm pretty sure the procedure is to create your dbContext, attach the Video to the context so EF takes it as the base state, then do your change, then save. Btw I'm fairly sure you can do db.Videos.Include(v => v.Tags) and save yourself the tag load. (Or configure the navigation property to auto-include) Kyte fucked around with this message at 16:20 on Dec 12, 2021 |
# ¿ Dec 12, 2021 16:17 |
|
Rocko Bonaparte posted:Is there a way to use the built-in .NET sorting algorithms with async functions? My scripting runtime is using async function calls for its callable stuff. Theoretically, any of these calls could actually block. It would be dumb to happen when comparing for a sort, but the prototype is async nonetheless. I am assuming I couldn't use the Sort functions in the collections with comparers that are awaiting on this stuff, but I thought I'd ask before having to pull out my own sorting algorithm. I can't begin to imagine how'd you have say, a quicksort, work without preloading all the data it needs. Which is effectively a ToListAsync().
|
# ¿ Dec 16, 2021 22:46 |
|
Rocko Bonaparte posted:The sorting problem is that I need to specify an IComparable that invokes functions in the scripting runtime that do the comparisons. The functions are async categorically, even if I don't expect them to actually block in this function. However, that's the interface. The problem is I don't know if I can reasonably switch the IComparables prototype to become async. I'm assuming I have to write my own sort implementation, which isn't the end of the world. If you are willing to risk people loving up, you could write an IComparable wrapper around some kinda IAsyncComparable interface that just does GetAwaiter().GetResult()?
|
# ¿ Dec 18, 2021 18:37 |
|
This is kinda overkill but what about making a self-hosted webapp. Then you can build the frontend with HTML, grab some JS library for dashboard widgets and plug everything together with ajax.
|
# ¿ Jan 2, 2022 05:18 |
|
According to the docs, HashSet.Equals compares the object identities (it just inherits from Object). SetEquals compares the contents. You clearly want the latter. (Element equality is checked using IEqualityComparer btw)
|
# ¿ Jan 21, 2022 09:30 |
|
Boz0r posted:Some people from my work have a boner for MS Power Apps, but I hate working with them, and they always become a pain to maintain beyond a PoC stage. Are there any good alternatives in C# frameworks that can match the PoC speed, but also be robust, where I can still control the code? Isn't that what the azure functions integration stuff is for?
|
# ¿ Jan 22, 2022 08:12 |
|
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 |
|
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 |
|
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 |
|
ControllerBase has Request which has both Form and Body? Are you not inheriting from ControllerBase?
|
# ¿ Feb 3, 2022 16:20 |
|
epswing posted:VS is not required. At minimum DevOps is just git repos + pull request management. So why did I have to wait to have a VS sub attached to my account in the client's DevOps before I could see the repo page? I'm honestly curious.
|
# ¿ Feb 5, 2022 00:30 |
|
epswing posted:DevOps comes with X free seats (I think 5?), beyond which the admin needs to pay for additional seats. Unless they’re VS subs in which case there’s no charge. If you needed to provide your own VS sub I’d guess your client didn’t want to pay for your seat, which was my dilemma above. I actually don't know if it was my management or the client that provided the sub but that makes sense.
|
# ¿ Feb 5, 2022 01:36 |
|
fuf posted:Thank you, that's helpful. fuf posted:man I still have such issues with EF and saving related entities. I just don't get it at all. The problem in the first one is that you didn't load the Filter from database but rather created a new Filter object and added it to the navigation property without attaching it to the context. Since it doesn't exist in the context, the change tracker believes it's something new and tries to INSERT. That can be solved by using Attach(), as you did in the second example, but that errors out if the context already had it attached as from a previous operation. You correctly understood the problem in the second example: you loaded every filter from database, but then instead of picking out the filter from the loaded list that DbContext already knows of, you tried to attach your own untracked filter. For the third case, it's easier if rather than using Where().FirstOrDefault() (which will always load from database, btw), you can use Find() to get the entity from the context (or query the DB if it wasn't already loaded). The correct method to add a preexisting related entity to a new object is to: 1) Obtain your related entity from the context, usually through Find(). 2) Add that entity object to the navigation property. Also do whatever updates you wanna do to it. 3) Save. That said, sometimes you don't want to load a whole entity when a simple attach would work just fine. For that case, I made an extension method. C# code:
(Incidentally this is one of the reasons why you limit your DbContext to a single unit of work) Kyte fucked around with this message at 20:06 on Feb 5, 2022 |
# ¿ Feb 5, 2022 20:01 |
|
TheBlackVegetable posted:What kind of NRE, do you mean like null lists of children references? Isn't the correct thing to initialise those in the entity's constructor? Also I used to be lazy about adding collection initializers to my entity classes because EF takes care of it when it loads an entity. I meant stuff like, say you have an application form for, dunno, social media or whatever. And there's a dropdown to select countries. You received in your viewmodel the country IDs, so you do something like: C# code:
LongSack posted:Also, the DbContext is as ephemeral as I can possibly make it. This is hard on dependency injection because a Service needs an instance of a Repository which needs an instance of the DbContext, so if I inject a service directly into a viewmodel or controller, it leads to problems with the tracker, so I end up having to inject a ServiceFactory and creating services for single operations to make sure the DbContext doesn't outlive its usefulness. Isn't this what transient or scoped dependencies are for? (DbContext is scoped by default) Or you like them to be even more short-lived than per-request? Kyte fucked around with this message at 04:39 on Feb 6, 2022 |
# ¿ Feb 6, 2022 04:37 |
|
LongSack posted:It’s not really an issue with web apps, where a controller is being instantiated for a single request anyway - get me a list of Foos, update this Bar, delete this Dongle. Ah, makes sense. You mentioned controllers and viewmodels so I assumed you were talking web. Yeah, for desktop it makes much more sense to use a factory.
|
# ¿ Feb 7, 2022 01:42 |
|
Given the usage pattern, wouldn't it make more sense to make it a property?C# code:
|
# ¿ Feb 9, 2022 18:22 |
|
A github page, perhaps?
|
# ¿ Feb 16, 2022 11:16 |
|
You don't need to do that
|
# ¿ Feb 18, 2022 11:22 |
|
Just upgrade to .NET 6
|
# ¿ Feb 28, 2022 07:41 |
|
worms butthole guy posted:Is there a way to access the value in a SortedList and then iterate on that value? So i'd like to do (pseudo code): SortedList.Values doesn't work for you?
|
# ¿ Mar 1, 2022 22:05 |
|
rarbatrol posted:Man I wish we had performance problems as subtle as that. I'm dealing with legacy code continuously being glued together in new and terrifying ways, and we keep finding code that performs a database lookup per record, which itself usually comes from an initial database lookup. Oh man that reminded me. Some time ago we received a project made in C# that we were supposed to add new stuff to. To get around the natural limitations wrt cross-database queries and get nice simple LINQ queries, they'd tacked a ToList() to every DbSet call. Before any applicable Wheres, of course. We didn't fix every instance, because it was out of scope and out of budget and frankly I'd've left it as-is as a "go gently caress yourself", but it was so drat slow to test I'd have to fix some anyways just to get things done in time. Kyte fucked around with this message at 00:43 on Mar 7, 2022 |
# ¿ Mar 7, 2022 00:41 |
|
insta posted:Yeah I got it wrong in the rant, and decided to keep it since it was quoted like 5 times. There are many things like this, that show up in other constructs you wouldn't think of: The relative efficiency of the two isn't nearly as relevant as the fact they are not actually equivalent. Some languages do weird things with case conversions. ToUpper == ToUpper is less correct than Equals(IgnoreCase).
|
# ¿ Mar 8, 2022 07:22 |
|
worms butthole guy posted:Thank you for this answer! It got my program running but now when I run it I get: That's normal. A List<T>.ToString() gives you that, since it doesn't (want to) know how to convert the contents to a string. Options: 1) Create a DisplayTemplate for CatData and then use @Html.DisplayFor(list) (it will automatically iterate across the list) 2) Create a DisplayTemplate for CatData, then foreach across the list and use @Html.DisplayFor(item) (if you want to customize the stuff around/between each item 3) Use JsonConverter.Serialize() to get a JSON representation you can throw to the screen. 4) Roll your own thing using foreach and whatnot.
|
# ¿ Mar 8, 2022 14:56 |
|
LongSack posted:Pretty sure this is an async related question. Language-based foreach integrates with the async state machine of the method that contains it. It'll loop and stop as needed. Your ForEach method does not have an async signature, so it'll be run synchronously. async x => { } produces a Func<T, Task>, so basically your ForEach will go through each element, run your action, get a Task and move on. Since ForEach does not await these tasks, it will not wait for completion before moving onto the next item in the loop. (Hence why the compiler isn't asking you to make the ForEach async) What you need is: C# code:
Kyte fucked around with this message at 16:58 on Mar 14, 2022 |
# ¿ Mar 14, 2022 16:54 |
|
I think a good rule of thumb is that if you're passing an async lamba in first place you should be checking if the method can actually do anything with it. The async keyword kinda stands out.
|
# ¿ Mar 14, 2022 22:05 |
|
NihilCredo posted:Yeah, I have the runtime switch, but multithreading Is the problem. This is our cronjob service, and it's running dozens of different jobs, so if we want to "drill down" into a particular one by increasing the switch to Debug/Verbose then all of them start logging at Debug/Verbose and it produces an absolute shitload of log data. This is only a vague idea but what about replacing the global with something that hangs off the current synchronization context, and then set up a new context with a new logger for the code path you want to log differently? Async/await should flow the context across invocations (unless you've got everything with ConfigureAwait(false) in which case rip I guess). Or maybe an async local or thread local depending on whether it's async or not. These are all hacky, but if you want to avoid a major restructuring it'd at least give you global-like behavior without making it fully global.
|
# ¿ Mar 17, 2022 07:50 |
|
He did already consider that but it'd involve refactoring a ton of code to include the new parameter. And this is not for a specific code path, so it'd need everything refactored.
|
# ¿ Mar 24, 2022 23:01 |
|
There seem to be two actors involved here. The request is sent to EndpointRoutingMiddleware, which matches it to the 405 Method Not Allowed endpoint. However, midstream during the response processing, the CorsService evaluates the request/response, recognizes it as a preflight, and modifies the response appropriately. Perhaps if you set up your own endpoint for OPTIONS it'd shut the message up. Supposing CorsService continues to intercept the request/response, the clientside result should be the same. (Or maybe a middleware? I'm not sure where does CorsService sit in the request pipeline) Kyte fucked around with this message at 17:33 on Apr 27, 2022 |
# ¿ Apr 27, 2022 17:30 |
|
At least centralize your service location and member initialization into the viewmodel factory (if necessary add and call an InitAsync after creation and before returning). IMO the combobox should have a simple binding to ObservableCollections in the viewmodel and nothing more. wrt the original issue: Does the Loaded event not fire at all or is the problem somewhere down the chain? Is your loaded event actually being attached by OnWindowLoadedBehaviorChanged?
|
# ¿ Jun 16, 2022 20:23 |
|
Variable 5 posted:Can someone tell me why Second one is nested like @foreach <div> @foreach <tag>. You're missing the underlined @. Opening a HTML tag switches the parser to HTML mode, so you need to preface the inner foreach with a @ to change back to C# mode or it'll treat the foreach as text. As an aside there's a stupid rule in Razor where you can't write @foreach when already inside C# mode and it constantly trips me up. The parser can already tell exactly what's going on, why not let the @ everywhere. Then you can put it everywhere and not get stupid errors when you find you have to wrap your block with another tag. Kyte fucked around with this message at 08:19 on Aug 30, 2022 |
# ¿ Aug 30, 2022 08:17 |
|
worms butthole guy posted:doh I think I figured it out thanks to both of you - I realized i'm overwriting gem every draw update. I still haven't figured out the best way to spawn something and not have it keep spawning over itself but that's my bad lol. still need to figure out OOP better. Add a flag to the object that holds your thing so you can do if (!thingHasSpawned && otherRelevantConditions) { spawnTheThing(); }; // we assume spawnTheThing() sets thingHasSpawned to true. If your program is particularly simple the flag could be the variable itself, like if (this.thing is null && otherRelevant conditions) { spawnTheThing(); };
|
# ¿ Nov 20, 2022 02:03 |
|
Munkeymon posted:Is there a best practice for setting session data after you've logged a user in in ASP MVC 5 (yep, still on Framework)? Logging in invalidates the current session so anything added to it just gets dropped on the floor when the request is done, but I don't see a way to add to the new session before the next request, so I'm planning on stuffing some stuff in a short-lived cookie and adding it to the session on the next request which feels kinda gross and like there ought to be a better way. fwiw I recall that TempData by default works through a short-lived cookie so it's not like you're alone there
|
# ¿ May 30, 2023 07:26 |
|
Red Mike posted:
Why not await the background task?
|
# ¿ Jun 2, 2023 09:55 |
|
Jabor posted:Because then you can't do anything else on the awaiting thread until the background task finishes? No I mean like: code:
|
# ¿ Jun 3, 2023 21:45 |
|
rarbatrol posted:What do you mean by unobserved task exceptions? I thought that back when async/await was introduced, tasks would no longer cause unhandled exceptions unless of course you're doing something nasty like async void. Maybe my knowledge is wrong but I'm pretty sure a faulted task eventually needs to be unwrapped and observed (whether via await or older mechanisms) or it'll eventually bubble up to the runtime? I know async void is dangerous because the task cannot be captured and therefore observed, but if you deliberately let a task go aren't got causing essentially the same behavior?
|
# ¿ Jun 4, 2023 06:37 |
|
Depending on the usage you might do with FileStreamResult from the File(stream) overload and possibly enabling EnableRangeProcessing.
|
# ¿ Jun 13, 2023 00:40 |
|
I don't use tuples for code that crosses file boundaries or anything public but I find it very useful for returning values from local or private functions as well as from Task.Run and cousins (ArcGIS addin development requires a looooooot of switching to the main CIM thread via QueuedTask.Run) where I can immediately unpack them into locals. Oh yeah they're also often more useful than anonymous types for LINQ queries that end in a First/FirstOrDefault. Basically anything that uses tuple unpacking, really.
|
# ¿ Jun 13, 2023 20:10 |
|
|
# ¿ May 17, 2024 01:17 |
|
This might be a bit of a dirty trick but you can have an awaiter for tuples like https://gist.github.com/jnm2/3660db29457d391a34151f764bfe6ef7. And then use it like: code:
|
# ¿ Jun 13, 2023 20:22 |