I am building a web app that has a dashboard page in which data must be updated in real-time. I'm using ASP.NET Core 3.0, MVC, ADO.NET, SignalR, and SqlDependency in my project.
I chose SignalR and SqlDepdendency (first time i'm developing with those) because data is fed into my SqlServer database from other db sources, and data needs to be pushed to my web app and to my clients. I found a few websites to explain how to use SqlDependency but I found no code examples with ASP.NET Core 3.0.
https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/detect-changes-sqldependency?view=sql-server-ver15
Where and how should I put the SqlDependency.Start() / SqlDependency.Stop() in the Startup.cs ? I currently have them inside my Repository DAL (Data Access Layer) class' method but I don't think it's correct to have the Start() and Stop() there because it doesn't make sense the SqlDependency is initiated and stopped on each call to that method is made by a client loading the dashboard page.
Also, any drawbacks or problems that you know with SqlDependency in terms of performance or bugs?
Related
I'm confused about Blazor server default project.
About two years ago, I tried Blazor server 5.0 template created by Microsoft in Visual Studio 2019.
I see project layout has a 3 subprojects:
Blazor.Client contains the client side code
Blazor.Server contains ASP.NET Core Web API
Blazor.Share contains all shared data in 2 project above.
Now I need to create Blazor server project (version 7.0). When I created a project, there was only one project left.
And now, if we want to write something, we can write it in the initializeAsync method (database operation, file access etc.)
I want to know if this way is correct or not?
I don't need to write logic in Web API and use httpClient to request data, right?
Thanks for your answer, and sorry for my English.
About two years ago, I tried Blazor server 5.0 template created by
Microsoft in Visual Studio 2019. I see project layout has a 3
subprojects:Blazor.Client, Blazor.Server, Blazor.Share
You might misunderstand, The Blazor Server App template will build only one Blazor project. But the Blazor WebAssembly App template with selected the ASP.NET Core Hosted checkbox will generate a solution includes the following ASP.NET Core projects:
"Client": The Blazor WebAssembly app.
"Server": An app that serves the Blazor WebAssembly app and weather data to clients.
"Shared": A project that maintains common classes, methods, and resources.
More detail information, see ASP.NET Core Blazor project structure.
if we want to write something, we can write it in the initializeAsync
method (database operation, file access etc.) I want to know if this
way is correct or not?
Yes, you can access the database or file in the OnInitializedAsync() method.
I don't need to write logic in Web API and use httpClient to request
data, right?
It depends on your choice, by using Web API, you can directly access the data from the API, instead of from the components/web page, if you don't want to provide this API, you can create service layer or directly access the database or file in the OnInitializedAsync() method. Refer to this article: Blazor Server App CRUD With Entity Framework Core In .Net 5.
i am a little overwhelmed by all the Information about Blazor Wasm and Blazor Server.
Until now, I have a 5 years old ASP.NET MVC App with a SignalR Hub running to notify my View, when something changed in the Database.
Let's be clear here. I have an existing Database Record. I present this database record to useres in an MVC View. Users are watching this View, and when an external source does an update to this record in the DB, the Partial View refreshes without reloading the whole Website.
As of now, I have a Blazor Webassembly hosted by .NET Core API and could easily switch to Blazor Server.
is this currently possible in Blazor Server/Blazor WASM... I'd prefer WASM if in anyway possible :)
Thanks in advance <3
It's definitely possible with either WASM/Server! You will have to still use a SignalR hub though to tell the client's to refresh the component. However, can be as simple as calling StateHasChanged() when the event to reload the UI is emitted from SignalR Hub. Here's an example:
...
hubConnection.On("RefreshTable", _ =>
{
// Retrieve the updated list from the DB - or utilize SignalR?
StateHasChanged();
});
...
There is a great guide on Microsoft's website to get a basic SignalR chat app running using Blazor WASM: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr-blazor?view=aspnetcore-5.0&tabs=visual-studio&pivots=webassembly#add-razor-component-code-for-chat
Currently I maintain an application that runs as a Windows service, reads messages from a message queue server, processes them and puts the result back into the message queue server. But it also contains a health monitoring component that is accessible through a web API.
It is implemented as a console app that uses Katana to self-host the health monitoring sub-system.
I'm now trying to figure out if we can move this to .NET Core and ASP.NET Core once they RTM. I know the Windows Service part cannot be ported, but I could also run the console app as a detached Docker container to basically achieve the same thing, in terms of main functionality.
But how will the health monitoring work? From what I can see the Katana project has been updated to ASP.NET 5 (which I guess is ASP.NET Core 1 before the big rename), but it does not run on the .NET Core CLR. Katana will require the full CLR. So that means Katana is out.
Does this mean that the way we build our app is impossible with .NET Core? Or does hosting the app through Kestrel not rule out the possibility of running code before the first request? With IIS the app does not live until the first request (unless you use the auto-start, but it's more of a speed-optimisation than have the app behave like an "allways-running-app") and generally the app is request-based and not continually running. Background threads in a IIS hosted app are a really bad idea.
Is this the same with Kestrel? Or will DNX start your app and keep it running until it's shutdown, much like a console app, so we can run all the background threads we want?
It follows the console app model. Katana is actually more the spiritual predecessor to kestrel. It is invoked for normal ASP.NET Core projects from the Main method with a normal method call. There are countless tutorials how to setup a server in RC1 (see Startup.cs Main method) and some for the upcoming RC2 (there is a builder for it). That would allow you to do both, your app code and your web api based monitoring, in a console app. Kestrel and DNX are not at all an application server like IIS. Kestrel is a plain HTTP server library and nothing more. You start it up and it listens from that moment on.
Nevertheless, you have to adjust your WebApi 2 and Katana based application to the new ASP.NET Core interfaces and middleware concept. But that should be easy compared to your message queuing adaption.
I'm very new to Asp.Net Web Apis(which Microsoft has made a part of MVC templates though we can use Web Api template independent of MVC)....Just a little background.
Coming back to my problem when my Web Service is called by a user then along the line of what my Web Service is serving comes a point where I have to deserialize a Json file to a generic C# collection and cache it in-memory and then the code inside one of the Controller actions(which is obviously a get method) checks for the in-memory cache and if it has that deserialized C# collection it gets it from there else its calls another method inside the controller which caches this generic collection in memory.
My question is ...is this possible to cache the stuff for a Web Api like what I described above...I'm quite familiar with Asp.Net page life cycle,caching and sessions etc. But not with Web Api....And my above explanation is just an abstract idea...not sure how to execute it, will it work? If yes then what namespaces would come in handy like System.Runtime.Caching or System.Web.Caching.
Your answers will be highly appreciated....
In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications. ASP.NET Web API doesn't have dependency in System.Web.dll so I recommend you to using System.Runtime.Caching, you can put your caching logic anywhere even in separate .dll file and use it in your ASP.NET Web API project.
I am working on an MVC4 application that uses MVC controllers and it is already in production. But a new requirement is to expose some of the functionality as a web service so it is accessible to get/post data programatically by other applications. So I am thinking of introuding WebAPI in this application and use it to provide an iterface to external world as well as use the same service by the views in my application.
My question is how can I use WebAPI in my existing controllers so that I can return Views instead of raw Json data.
Thanks