Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
distortion park
Apr 25, 2011


Nice!

Adbot
ADBOT LOVES YOU

brap
Aug 23, 2004

Grimey Drawer
Or you could make a static abstract Create method on an interface and have your types implement it :)

Polio Vax Scene
Apr 5, 2009



I've got a weird issue where I'm trying to separate strings into segments and if a unicode character happens to be right at the segmentation point it'll get "chopped in half"



What would be a good way to avoid this?

mystes
May 31, 2006

Polio Vax Scene posted:

I've got a weird issue where I'm trying to separate strings into segments and if a unicode character happens to be right at the segmentation point it'll get "chopped in half"



What would be a good way to avoid this?
Maybe use StringInfo.SubstringByTextElements or something?

mystes fucked around with this message at 20:05 on Nov 4, 2021

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

brap posted:

Or you could make a static abstract Create method on an interface and have your types implement it :)

Yeah, or define a plain struct for Id + Description and use composition so you just pass around a single parameter.

Plenty of options if Boz0r's requirements are as simple as his post suggested, i.e. a semantic type hierarchy without any extra fields in the subtypes, and none of those options are anywhere near as masochistic as AutoMapper. But I'm guessing there are other requirements he didn't mention yet.

Mata
Dec 23, 2003

Polio Vax Scene posted:

I've got a weird issue where I'm trying to separate strings into segments and if a unicode character happens to be right at the segmentation point it'll get "chopped in half"
What would be a good way to avoid this?

I think you can use char.IsLowSurrogate or IsHighSurrogate to detect this case.

I actually did a pull request a while back to fix an issue where deleting chars from a textbox one at a time using backspace would break if the textbox contained emojis and such. https://github.com/rds1983/Myra/pull/316/commits/07a29096bb852f9090c99740a7d3514202a78a70

Polio Vax Scene
Apr 5, 2009



Mata posted:

I think you can use char.IsLowSurrogate or IsHighSurrogate to detect this case.

I actually did a pull request a while back to fix an issue where deleting chars from a textbox one at a time using backspace would break if the textbox contained emojis and such. https://github.com/rds1983/Myra/pull/316/commits/07a29096bb852f9090c99740a7d3514202a78a70

Thanks, this is exactly what I needed.

fuf
Sep 12, 2004

haha
I'm messing around with Blazor Server but I don't really know what I'm doing.

The app is gonna run completely locally and I need the user to be able to select a local directory that the app will then scan for containing files.

But I know that browsers aren't really allowed to access the local file system so I dunno how to achieve this? There is a blazor component called <InputFile> for selecting local files, but the resulting IBrowserFile object doesn't provide the path to the directory the file is in.

I've definitely used other web apps that run locally and scan / watch local folders that the user selects, so I guess it's possible, but does anyone know how?

GI_Clutch
Aug 22, 2000

by Fluffdaddy
Dinosaur Gum

fuf posted:

I'm messing around with Blazor Server but I don't really know what I'm doing.

The app is gonna run completely locally and I need the user to be able to select a local directory that the app will then scan for containing files.

But I know that browsers aren't really allowed to access the local file system so I dunno how to achieve this? There is a blazor component called <InputFile> for selecting local files, but the resulting IBrowserFile object doesn't provide the path to the directory the file is in.

I've definitely used other web apps that run locally and scan / watch local folders that the user selects, so I guess it's possible, but does anyone know how?

What exactly is the goal? Is this supposed to be a web site that's running locally or do you just want to use Blazor so you can render your UI using HTML/CSS? If the latter, you might be looking for BlazorWebView. You can use it in conjunction with WebView2 to host Blazor content within a winform or WFP app. Then you're not sandboxed and you get full access to the framework and the user's PC. So you could show a FolderBrowserDialog or whatever when the user clicks the button. It's only in preview right now though.

This older blog entry goes into some details and I think is what I used to kick off my test app I goofed around with last week: https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-net-6-preview-3/

GI_Clutch fucked around with this message at 16:09 on Nov 11, 2021

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

fuf posted:

I'm messing around with Blazor Server but I don't really know what I'm doing.

The app is gonna run completely locally and I need the user to be able to select a local directory that the app will then scan for containing files.

But I know that browsers aren't really allowed to access the local file system so I dunno how to achieve this? There is a blazor component called <InputFile> for selecting local files, but the resulting IBrowserFile object doesn't provide the path to the directory the file is in.

I've definitely used other web apps that run locally and scan / watch local folders that the user selects, so I guess it's possible, but does anyone know how?

Blazor Server is the opposite of what you're after.

In Blazor Server, the UI is rendered server-side and provided to the client via a SignalR websocket. This causes all sorts of fun latency-related nightmares. Don't use Blazor Server.

Blazor WebAssembly is what you're after: The entire application is compiled and run in your browser, unless you expose REST API methods in your server that the browser calls.

Beyond that, an explanation of how to use InputFile is here: https://docs.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-6.0&pivots=server

But ultimately understanding your full objective is important, a webapp might not be the best fit for you here.

fuf
Sep 12, 2004

haha
Thanks for the replies :)

Yes the plan is to run it completely locally on localhost. Like how something like SABnzbd or Plex works.

The app will let a user select a folder on their local drive that contains files and then loop through and do some operations on those files. It's just a little personal project to try and help me get better at C# and web development (hence wanting to do it as a web app, even though something else might be more suitable).

I figured since it was running locally the SignalR latency wouldn't be an issue.

As far as I know Blazor WebAssembly apps are not allowed to access the local filesystem (beyond the file upload capability provided by InputFile) so I would need to build a separate backend with an API to do the file operations. I know this would probably be good as a learning experience but part of the appeal of Blazor Server is just being able to access the backend models directly in Blazor components and render them without having to do any API stuff.

And even if it was WebAssembly I think I'd still have the same issue of how to let the user select a directory and pass that path to the server / backend somehow?

fuf fucked around with this message at 17:01 on Nov 11, 2021

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost
IMO you might be better off starting your idea as a Console app / Library, to see what APIs you need at the OS level, creating the abstracting interfaces and classes.

Then when you got that working, you can then try Blazor and see what's available there. Then, if you can't use WASM you'll know it right then, and if you need to set up a server to do local operations, you'll have the library ready to go. Or, if none of that works, you can build a new UI using some other stack.

mystes
May 31, 2006

It's sort of unclear when you say app whether you're trying to make a webapp or a .net application that you will distribute as a program.

If the latter then blazor server (with the server part running on the same program as the frontend) is fine and you can just use one of the libraries that embeds it as a webview and then pop up a dialog box from the server code.

If you want to make it a webapp you won't have any server code so you will need to use normal browser apis, but apis that allow you do to file access beyond simply accepting uploaded files aren't widely supported (I think they're in beta in chrome but you have to specifically enable them).

mystes fucked around with this message at 18:14 on Nov 11, 2021

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost
The problem is file scanning on disk. You can't scan a user's local disk with WASM, only pick files for uploading. So if they want to do file scans, the server approach is really the only way to go (provided it's run on the same computer or has some other way to run some remote process on another computer I guess?).

But that's also why I think going with a library first approach would make more sense than trying to build everything as a web app all at once. Building out the functionality you need as a separate library doesn't make you think about building an API that matches your UI. IMO it's best to build out a structure that's best for getting the data, then trying to build the UI and see what's needed or what can be moved.

EDIT: Another thing is MAUI Blazor, which hosts a Blazor webview inside of an MAUI app, letting you have a cross-platform app with Blazor that can also access native features. So if it doesn't need to be an actual server, that could work.

Drastic Actions fucked around with this message at 18:14 on Nov 11, 2021

mystes
May 31, 2006

Drastic Actions posted:

EDIT: Another thing is MAUI Blazor, which hosts a Blazor webview inside of an MAUI app, letting you have a cross-platform app with Blazor that can also access native features. So if it doesn't need to be an actual server, that could work.
There have been a several libraries like this, the oldest being webwindow which was a personal project by a ms employee. I think that was unfortunately abandoned, though and I'm not sure the status of maui blazor but I think there's another recent lightweight one similar to webwindow that I forgot the name of if that's not ready yet.

I previously made a thing with a blazor server frontend using webwindow that did raw filesystem access and it was dead simple so I would recommend something like that approach if it's something that sounds like what you want.

By using blazor server in this way you get to make the ui in blazor but have all the capabilities of a normal program and you don't even really have to think about what's happening where (since with blazor server basically everything actually happens on the server).

mystes fucked around with this message at 18:20 on Nov 11, 2021

nielsm
Jun 1, 2009



Is this just to learn the tech? Because it seems an odd choice to use Blazor for the UI otherwise, when the app is meant to run entirely on localhost. A more traditional GUI app seems like it'd be the path of less resistance right now.

mystes
May 31, 2006

nielsm posted:

Is this just to learn the tech? Because it seems an odd choice to use Blazor for the UI otherwise, when the app is meant to run entirely on localhost. A more traditional GUI app seems like it'd be the path of less resistance right now.
For better or worse, using some sort of webview type thing for a local desktop app is not exactly extremely uncommon in 2021. It may very well be the most common way to make desktop apps now.

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost

mystes posted:

There have been a several libraries like this, the oldest being webwindow which was a personal project by a ms employee. I think that was unfortunately abandoned, though and I'm not sure the status of maui blazor but I think there's another recent lightweight one similar to webwindow that I forgot the name of if that's not ready yet.

MAUI Blazor is an officially supported product, maintained by .NET. It will ship GA when MAUI does.

Considering I work on MAUI and its related UI tooling for it, I really hope MAUI and MAUI Blazor doesn't die.

fuf
Sep 12, 2004

haha

mystes posted:

It's sort of unclear when you say app whether you're trying to make a webapp or a .net application that you will distribute as a program.

Yeah sorry I think it's because I'm imagining this halfway thing like the Plex and SABnzbd examples I gave. Like it's an executable that you run locally but you access the UI through your browser by going to localhost:4444 or whatever. I'm not sure whether that's still called a webapp?

I honestly hadn't really thought ahead to the "run as an executable" part but MAUI actually looks like it could be pretty ideal. If it can provide access to the local filesystem but also use Blazor for the UI all as part of the same program that would be great.

nielsm posted:

Is this just to learn the tech? Because it seems an odd choice to use Blazor for the UI otherwise, when the app is meant to run entirely on localhost. A more traditional GUI app seems like it'd be the path of less resistance right now.

Yeah it's basically just to learn the tech. I sort of picked a bad project because of the complication of this local file stuff, but I'm excited about it so I want to stick with it. I also want to stick with web/html/css for the UI because that's where all my experience lies and they're the skills I want to build on.

fuf fucked around with this message at 19:25 on Nov 11, 2021

Riot Bimbo
Dec 28, 2006


It has been a loving trip, you know? I picked up C# in September of last year, when someone informed me that visual studio had a pretty robust free version. I took a pretty janky but ultimately effective intro course with codecademy, and quickly completed it, and followed it up by buying reference books and diving the .net documentation, learning stuff here and there.

While my, well, discipline has dropped off, and I maybe mess around maybe once or twice a week, where it used to be daily, the fact is I have voluntarily maintained an interest and continue to develop what I can do, and it is bar-none the most attention and progress I've made towards something in life post-education, where there was structure in place to teach that I have traditionally struggled to imitate in my own life.

I'm still really not where I want to be with it all, though.

I have a goal in mind, because it's the kind of thing I'd like to do: I'd like to make a text-based game, that's a simple roguelike thing.

there are tons of "Make a game in C#" tutorials out there, a not-small portion of that is Unity stuff of course, and I *have* messed with Unity, but that's not really the direction I want to take. I want to learn how to do a ton of stuff that Unity automates and abstracts away.

The problem is, at this point, that I don't know what to ask, what information to seek.

Generally speaking, I'd love resources that would let me learn to create a grid of chars, that represent non-moving map objects, as well as moving objects, and the player, and all of that?

I've kind of hacked at the idea, but generally not succeeded in bumbling into a working solution.

does anyone know a good source that teaches this stuff? Assume an intermediary knowledge of the language; I can implement basic forms of just about all of the base features of the language, but using them all in a way that makes a functioning and robust game loop without something like Unity is still beyond me and I hate that and wanna fix it badly

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

fuf posted:

Yeah sorry I think it's because I'm imagining this halfway thing like the Plex and SABnzbd examples I gave. Like it's an executable that you run locally but you access the UI through your browser by going to localhost:4444 or whatever. I'm not sure whether that's still called a webapp?

AFAIK applications like these usually solve that problem by either having the user type in the path relative to where the server is running, or providing a custom file browser component backed by an API which lists the server's file system. This is because they don't always necessarily run on the same machine as the user's web browser.

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost

Riot Bimbo posted:

does anyone know a good source that teaches this stuff? Assume an intermediary knowledge of the language; I can implement basic forms of just about all of the base features of the language, but using them all in a way that makes a functioning and robust game loop without something like Unity is still beyond me and I hate that and wanna fix it badly

IMO I would try MonoGame if you want a game engine. It should be much more straightforward to me than something like Unity since you are nearly always within the IDE of your choice, rather than in the Unity Editor and having to branch out to VS for scripts. It gives you a game loop and a canvas for drawing.

For a game loop with just text characters, make a console app with a while loop. The same logic would apply for drawing a UI as it would with any terminal-based graphics GUI at that point. If you need ideas for that, maybe look at Terminal.Gui.

LongSack
Jan 17, 2003

Riot Bimbo posted:

It has been a loving trip, you know? I picked up C# in September of last year, when someone informed me that visual studio had a pretty robust free version. I took a pretty janky but ultimately effective intro course with codecademy, and quickly completed it, and followed it up by buying reference books and diving the .net documentation, learning stuff here and there.

While my, well, discipline has dropped off, and I maybe mess around maybe once or twice a week, where it used to be daily, the fact is I have voluntarily maintained an interest and continue to develop what I can do, and it is bar-none the most attention and progress I've made towards something in life post-education, where there was structure in place to teach that I have traditionally struggled to imitate in my own life.

I'm still really not where I want to be with it all, though.

I have a goal in mind, because it's the kind of thing I'd like to do: I'd like to make a text-based game, that's a simple roguelike thing.

there are tons of "Make a game in C#" tutorials out there, a not-small portion of that is Unity stuff of course, and I *have* messed with Unity, but that's not really the direction I want to take. I want to learn how to do a ton of stuff that Unity automates and abstracts away.

The problem is, at this point, that I don't know what to ask, what information to seek.

Generally speaking, I'd love resources that would let me learn to create a grid of chars, that represent non-moving map objects, as well as moving objects, and the player, and all of that?

I've kind of hacked at the idea, but generally not succeeded in bumbling into a working solution.

does anyone know a good source that teaches this stuff? Assume an intermediary knowledge of the language; I can implement basic forms of just about all of the base features of the language, but using them all in a way that makes a functioning and robust game loop without something like Unity is still beyond me and I hate that and wanna fix it badly

Hit google and search for “random dungeon generator c#” that will send you down a deep rabbit hole about roguelikes…

I’m working towards something similar, but I’m approaching it from the “write my own interpreter” direction - where rooms in the dungeon will be generated from interpreted “diggers” which are given a block of the dungeon to work with and can make a room to fit inside that block.

fuf
Sep 12, 2004

haha

Quebec Bagnet posted:

AFAIK applications like these usually solve that problem by either having the user type in the path relative to where the server is running, or providing a custom file browser component backed by an API which lists the server's file system. This is because they don't always necessarily run on the same machine as the user's web browser.

Thank you for this, you are absolutely right and it made me realise I was thinking about it the wrong way around. The task isn't to let the user select a local directory and somehow pass it to the server, it's to show the user the server's filesystem and have them select from there (even if ultimately it's the same filesystem for this particular project).

And now that I think about the examples I was giving, they all have their own custom way of browsing the server's filesystem.

It's still gonna be hard to come up with custom file browser component but at least I can see a solution now.

I tried MAUI and it's pretty cool but definitely overkill and too premature for where I'm at with this little hobby project. I also gave up on Blazor Server and switched to Blazor WebAssembly with a more traditional API. Mostly because all the tutorials and courses online are for Blazor WebAssembly, but also because I guess it's good practice to get used to doing everything via APIs.

epswing
Nov 4, 2003

Soiled Meat
Is there a difference between this

C# code:
class SerialWorker
{
    private static readonly object LOCK = new object();
    
    public void DoTheBusiness()
    {
        Monitor.TryEnter(LOCK, Timeout.InfiniteTimeSpan);
        
        try
        {
            // do some work
        }
        finally
        {
            Monitor.Exit(LOCK);
        }
    }
}
and this

C# code:
class SerialWorker
{
    private static readonly object LOCK = new object();
    
    public void DoTheBusiness()
    {
        lock (LOCK)
        {
            // do some work
        }
    }
}

Red Mike
Jul 11, 2011

epswing posted:

Lock vs Monitor.TryEnter

I believe the lock statement and Monitor.Enter/TryEnter are equivalent, with the exception: lock will actually call Monitor.Enter (which in practice is equivalent to Monitor.TryEnter with an infinite timeout like you wrote) which means no possible way to fail out/not block.

TryEnter returns void however it also returns a lockTaken boolean value (by ref) so that you can check if the lock was acquired or not. Obviously that only matters if you pass a non-infinite timeout, which generally you should (or else just use Enter/lock statement).

Do note that Monitor.Enter/TryEnter may have some funky behaviour depending on what you pass in because of boxing, if you don't pass in an object of type object.

mystes
May 31, 2006

quote:

I believe the lock statement and Monitor.Enter/TryEnter are equivalent, with the exception: lock will actually call Monitor.Enter

Indeed
https://sharplab.io/#v2:CYLg1APgAgT...xeIURAM2qOQQA==

epswing
Nov 4, 2003

Soiled Meat
OK thanks.

Sounds like if the timeout is infinite (i.e. I want competing threads to wait forever until it's their turn), there's no need to use Monitor and I should just use the lock keyword.

If there's a timeout, and/or I want the block to be skipped if another thread is already in there, looks like Monitor.TryEnter will do the trick.

epswing fucked around with this message at 16:03 on Nov 15, 2021

distortion park
Apr 25, 2011


In practice I've found that I almost always end up using SemaphoreSlim instead of lock statements. It has a lot of extra functionality and is only a little harder to use safely

Riot Bimbo
Dec 28, 2006


LongSack posted:

Hit google and search for “random dungeon generator c#” that will send you down a deep rabbit hole about roguelikes…

I’m working towards something similar, but I’m approaching it from the “write my own interpreter” direction - where rooms in the dungeon will be generated from interpreted “diggers” which are given a block of the dungeon to work with and can make a room to fit inside that block.


Drastic Actions posted:

IMO I would try MonoGame if you want a game engine. It should be much more straightforward to me than something like Unity since you are nearly always within the IDE of your choice, rather than in the Unity Editor and having to branch out to VS for scripts. It gives you a game loop and a canvas for drawing.

For a game loop with just text characters, make a console app with a while loop. The same logic would apply for drawing a UI as it would with any terminal-based graphics GUI at that point. If you need ideas for that, maybe look at Terminal.Gui.

thank you both so much! These are both helpful ledes, much appreciated

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

pointsofdata posted:

In practice I've found that I almost always end up using SemaphoreSlim instead of lock statements. It has a lot of extra functionality and is only a little harder to use safely

semaphoreslim works for async code and lock/monitor only work in synchronous code. the compiler will try to save you from this (e.g. you can't await within a lock and the compiler will warn you) but generally lock when mixed with async will either deadlock or not actually synchronize anything (common jimmy mistake).

Mr Shiny Pants
Nov 12, 2012
F# 6 looks nice. Debugging pipeline operators looks like a big win.

epswing
Nov 4, 2003

Soiled Meat
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.

Projects like Hangfire (and in the past, NCron) strike me as "Schedulers" for jobs that need to run daily, once an hour, cleanup tasks, etc. Apparently as of Hangfire 1.7 you can set
C# code:
var options = new BackgroundJobServerOptions
{
    SchedulePollingInterval = TimeSpan.FromMilliseconds(5000)
};
and use the 'sixth' cron slot
C# code:
RecurringJob.AddOrUpdate(() => YourJob(), "*/5 * * * * *");
to specify a job that runs e.g. every 5 seconds. But with Hangfire this also means hitting the DB every 5 seconds and retaining information about the job, resulting in silly numbers of "succeeded" jobs. Such scheduling tools don't feel right.



More lightweight solutions like a Thread with a loop containing a Sleep, or a Timer with an interval and an Elapsed event, feel janky and I don't know how they'll react to an Application Pool in IIS going to sleep/recycling, or whatever the Azure App Service equivalent is. Basically with ASP.NET I don't control the process running the site, so how can I ensure a Thread/Timer will actually run correctly. I've also thought about just hitting an api endpoint from outside the site via some task scheduler or other process but that feels even worse.

For what it's worth, I have been starting a thread in Application_Start() which starts a Timer, and then blocks on a ManualResetEvent. Later, in Application_End() I Set() the ManualResetEvent which stops the timer and allows the thread to finish. This has actually worked fine for years, but I'm aware it's probably a coding horror, and I'm sure there's a better way.

epswing fucked around with this message at 17:57 on Nov 17, 2021

raminasi
Jan 25, 2005

a last drink with no ice
There's an in-memory Hangfire storage engine you could consider but I think an important question to ask is why you want to run a background loop in a web server in the first place. It's not necessarily "wrong" but it's kind of unusual and there might be a better overall solution. (What does it do? Can it safely be suspended? What should happen if it fails? Can two invocations of the loop safely run concurrently? Answers to questions like these will inform your solution.)

epswing
Nov 4, 2003

Soiled Meat
These are good questions.

It polls an external service for updates every 30 seconds. Fires events when updates are received. Handlers registered to those events update the database, and synchronize with SignalR clients.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

I tried various approaches to running background jobs as part of an ASP.NET Core and eventually found IHostedService to be the most reliable, compared to various third-party approaches (from spinning up a plain while loop in a thread to various message loop libraries), which would regularly get silently 'killed' and stop running, even as the web service stayed up.

However, I did need to add a Redis service to store the glut of data being munged, rather than simply holding it in-memory as a reference within the IHostedService; I don't know if this was something to do with ASP.NET's memory management or - far more likely - an issue of the underlying layers, but when I stored at the very most a couple hundred megs of incoming JSON in an in-memory objects, the IHostedService would still fail every now and then.

I've also used Hangfire, but to run a separate console application, not as part of an ASP.NET service. It's reasonably reliable but still kinda clunky, poorly documented, and not very well maintained. In particular, the use of attributes for configuration means a bunch of settings like 'how many copies of this job can run concurrently?' or 'how long is this job allowed to run at the longest?' or even 'which named job queue should this job be assigned to?' have to be hardcoded as string literals instead of being configurable at runtime.

If you're using non-Core ASP.NET then I doubt that you can use IHostedService. On the other side, the engine guts are different so you may have a better experience with spinning up a third-party loop of some kind.

epswing
Nov 4, 2003

Soiled Meat
Running non-Core ASP.NET for now (.NET Framework 4.8) but excited to transition to .NET 6 soon.

I also dislike some of Hangfire's warts, but after NCron was abandoned it appeared to be the best option at the time (5ish year ago?).

I'll look at IHostedService, thanks.

epswing fucked around with this message at 18:22 on Nov 17, 2021

insta
Jan 28, 2009
Yeah, BackgroundWorker is exactly what you're looking for. They run when your app pool starts, stop when you app pool stops, and are in the same process space for easy communication between web and service.

If you have a Singleton instance between the web and service, you can have the web project directly talk to data populated by the service (like an in-memory ConcurrentDictionary).

Kyte
Nov 19, 2013

Never quacked for this

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

Adbot
ADBOT LOVES YOU

Canine Blues Arooo
Jan 7, 2008

when you think about it...i'm the first girl you ever spent the night with

Grimey Drawer

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.

If this is on IIS, there is no reason to get a library or a service involved to get a computer to count to 5. You could use DispatchTimer if you don't need better than 1/10th a second of precision and are hosting it on a Windows box.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply