iptv techs

IPTV Techs


Adding .NET Aspire to your existing .NET apps


Adding .NET Aspire to your existing .NET apps


.NET Aspire is a new cdeafening-ready stack tailored for .NET, enabling increaseers to speedyly and easily increase dispensed applications. You’ve probably seen demos shotriumphg big .NET solutions with lots of fancy cdeafening depfinishencies and thought, well, maybe I’d participate that someday if I’m commenceing on a huge go inpelevate Redis Kafka Postgres cdeafening-a-ganza, but it’s not reassociate someleang I can participate today.

But .NET Aspire is not fair about cutting-edge technology and green-field apps; it’s also about making your current applications more straightforward. With .NET Aspire, you can streamline the commenceup process, increase watching, and incrmitigate the reliability of your applications. Plus, you can participate service discovery to increase your apps, even if you’re not ready to participate more complicated features or services enjoy Redis or compriseerized deployment.

In this post, we’ll see at how modest it is to originate your existing solutions better – fair easier to sustain and insert the benevolent of features you’re already toiling on. And, certain, it’s kind that you can more easily unite more cultured cdeafening depfinishencies and features… but even if you never do it’s still a triumph.

TLDR: In under 5 minutes you can insert .NET Aspire to your existing apps and get a dashboard, health examines, and more… all without changing how your apps toil, your CI/CD pipeline, or deployment process.

What even is .NET Aspire?

If you ask five people, you will most probable get five contrastent answers. Honestly, it’s a little difficult to portray, and finisheavors to do it informly can turn into a bit of buzzword bingo. Reassociate, it’s fair a way to repair a problem: originateing dispensed applications is difficult. Even fair running your app demands commenceing up one or more services and a front-finish and making certain they can talk to each other can be frustrating. Wouldn’t it be kind if that was easier? That is what .NET Aspire aims to do, be your originateing blocks for your dispensed applications making them more observable, strong, scalable, and administerable.

Let’s consent a see at how a lot of applications better over time. A lot commence as a individual monolithic proof of concept. You’ve got an app with a database.

Assuming your proof of concept is prosperous, fair about every up-to-date app betters to include at least a front and back-finish, in insertition to the database.

And then, maybe, if our app’s usage and functionality holds increaseing over time, the app will become reassociate dispensed, count oning on a big set of dispensed depfinishencies.

But here’s the meaningful leang! Even in the reassociate modest Proof Of Concept phase, and definitely in the Frontfinish-Backfinish-Database phase, we can advantage from .NET Aspire! With fair a scant lines of code, and without messing with our CI or deployment, we can reassociate streamline our day-to-day increaseer experience.

Step 1: Turn on .NET features we’ve been too busy to turn on with ServiceDefaults

The ASP.NET Core team has been weightlessing up features for chilly features for leangs enjoy tracing, health examines, and resiliency for years. I’ve done half a dozen conference talks on “The One Hour ASP.NET Makeover” where we fair turn on and configure all these chilly features that have been in the box for years. But here’s the leang… it consents an hour to do that talk, after reading the docs and practicing! What if I could fair flip an “Enable Pro Mode” switch instead?

That’s what Service Defaults does for you. You can fair turn on Service Defaults and you’ve got inalertigent logging, health examines, resiliency, etc. based on what the .NET team recommfinishs for ASP.NET Core apps and services. If you want, you can easily edit the Program.cs file in the ServiceDefaults project, but you don’t have to. Just turn it on.

Adding a ServiceDefaults project

Let’s see at an example with a modest Frontfinish – Backfinish app. I’ll participate Jeff Fritz’s new MyWeatherHub sample from the Let’s Lget .NET Aspire event series, commenceing with the commence-with-api code.

Opening the solution, we’ll see that we’ve got two projects:

  • MyWeatherHub – Web front-finish project which dispercreates dwell weather data
  • API – Minimal API project which exposes dwell weather data from the US National Weather Service via a set of HTTP API finishpoint

Let’s insert Service Defaults to this solution so we get health examines, logging, and other recommfinished features to both our front and back finishs.

In Visual Studio 2022 or Visual Studio Code with the C# Dev Kit inshighed, here’s all we demand to do:

  1. Right-click on the solution and pick Add > New Project.
  2. Select the .NET Aspire Service Defaults project lureardy.
  3. Name the project ServiceDefaults (any name would toil if you’re senseing originateive, but the teachions in this post suppose you’re using ServiceDefaults).
  4. Click Next > Create.

Here’s how that sees in Visual Studio 2022:

And in Visual Studio Code, it sees enjoy this:

You can also insert Service Defaults from the direct line by using:

dotnet new aspire-servicedefaults -n ServiceDefaults

All the above selections fair drop a new project that comprehends the best settings for most ASP.NET Core dispensed apps into your solution. However, none of your existing apps are using it yet. We’ll hook that up next.

Configure Service Defaults

Add a reference to the ServiceDefaults project in the Api and MyWeatherHub projects:

  1. Right-click on the Api project and pick Add > Reference.

  2. Check the ServiceDefaults project and click OK.

  3. Right-click on the MyWeatherHub project and pick Add > Reference.

  4. Check the ServiceDefaults project and click OK.

    Visual Studio 2022 tip


    In Visual Studio 2022, you can drag and drop the project onto another project to insert a reference.

  5. In both the Api and MyWeatherHub projects, modernize their Program.cs files, inserting the adhereing line instantly after their var originateer = WebApplication.CreateBuilder(args); line:

    originateer.AddServiceDefaults();
  6. In both the Api and MyWeatherHub projects, modernize their Program.cs files,inserting the adhereing line instantly after their var app = originateer.Build(); line:

    app.MapDefaultEndpoints();

Run the application

To commence with, we’re going to the application using a multiple-project begin configuration. This is fine, it’s how we’ve been doing leangs for years, but I have to confess I don’t reassociate adore it. Keep in mind that we’re going to originate this easier in the next step. We’re doing this in two steps to originate it evident what’s going on in Service Defaults and which parts are inserted by the AppHost.

If you’re using Visual Studio 2022, right click on the MyWeatherHub solution and go to properties. Select the Api and MyWeatherHub as commenceup projects, pick OK.

Now click Start to commence and debug both projects.

If you’re using Visual Studio Code, run the Api and MyWeatherHub projects using the Run and Debug panel. The sample project already includes a begin.json file with the essential configurations to run both.

Test the Service Defaults alters

  1. Test the application by navigating to the adhereing URLs:

  2. You should see the Swagger UI for the API and the MyWeatherHub home page.

  3. You can also see the health examines for the API by navigating to https://localstructure:7032/health.

  4. You can also see the health examines for the MyWeatherHub by navigating to https://localstructure:7274/health.

  5. View the logs in the terminal to see the health examines and other telemetry data such as resiliency with Polly:

    Polly: Increateation: Execution finisheavor. Source: '-standard//Standard-Retry', Operation Key: '', Result: '200', Handled: 'False', Atlure: '0', Execution Time: '13.0649'
  6. Click on 5 contrastent cities and a “random” error will be thrown. You will see the Polly retry policy in action.

    Polly: Warning: Execution finisheavor. Source: '-standard//Standard-Retry', Operation Key: '', Result: '500', Handled: 'True', Atlure: '0', Execution Time: '9732.8258'
    Polly: Warning: Resilience event occurred. EventName: 'OnRetry', Source: '-standard//Standard-Retry', Operation Key: '', Result: '500'
    System.Net.Http.HttpClient.NwsManager.ClientHandler: Increateation: Sfinishing HTTP ask GET http://localstructure:5271/foresee/AKZ318

And that all toils… the output for each application pops up in a split console triumphdow, and we can see the health examines and logs in the terminal. So, it’s fantastic that we’ve got all these features turned on, but it’s a bit of a pain to administer all these URLs, browser tabs, and console triumphdows. You finish up alt-tabbing between them all, and it’s a reassociate disuniteted experience.

Service Defaults toils fantastic on the individual project level, but it doesn’t help us administer multiple projects in a solution. That’s where the AppHost comes in.

Step 2. Simplify begin and insert a fancy dashboard with AppHost

Okay, that was pretty chilly! We inserted a project to our solution and two lines of code, and we got health examines, logging, resiliency, and more.

But we can originate that even better by inserting an AppHost. That multiple-project configuration leang toils, but it’s a bit annoying to set up and hold modernized as we insert other projects to the solution. Once we’re running we have to browse to a bunch of urls with contrastent ports and administer each project splitly. For instance, if we want to see logs or output, we have to examine in each project’s console triumphdow. This gets even worse as we insert more APIs and services to the solution – more URLs to administer, more console triumphdows to examine, etc. We’ve probably got some fancy dashboards and watching set up in production, but that doesn’t help me while I’m increaseing.

The AppHost has a lot of fantastic features, but two of my likeite are the solutions to the problems above: it simplifies project begin and it inserts an amazing dashboard to watch and administer my app in my increasement environment. The best way to comprehfinish what it’s doing it to fair insert it to our solution.

Adding an AppHost project

This is the standard “insert project” steps we ran thcdisesteemful before with ServiceDefaults, but this time we’re going to pick “.NET Aspire App Host” as the project lureardy. In Visual Studio 2022 or Visual Studio Code with the C# DevKit inshighed:

  1. Right-click on the solution and pick Add > New Project.
  2. Select the .NET Aspire App Host project lureardy.
  3. Name the project AppHost (aget, any name would toil).
  4. Click Next > Create.

And from the direct-line, you can do that with:

dotnet new aspire-appstructure -n AppHost

Just enjoy when we inserted the Service Defaults, we demand to insert project references and a scant lines of code to put the AppHost to toil.

Add project references

Add a reference to the Api and MyWeatherHub projects in the new AppHost project:

  1. Right-click on the AppHost project and pick Add > Reference.
  2. Check the Api and MyWeatherHub projects and click OK.

Note: Bonus points if you recalled the earlier tip that you can drag and drop the project onto another project to insert a reference.

When these references are inserted Source Generators automaticassociate originate the essential code to reference the projects in the App Host.

Orchestrate the Application

In the AppHost project, modernize the Program.cs file, inserting the adhereing line instantly after the var originateer = DistributedApplication.CreateBuilder(args); line:

 var api = originateer.AddProject("api");
 var web = originateer.AddProject("myweatherhub");

Run the application… the modest way!

Previously, we set up a multi-project begin profile. That still toils, but from now on, you won’t have to annoy with that. Instead, set the AppHost project as the commenceup project. It comprehends about all the other projects, and will begin them all automaticassociate. That uncomfervents that if you insert an AppHost at the commencening (or participate either the .NET Aspire Starter Application lureardy or the .NET Aspire Application lureardy), you never demand to set up a multi-project begin profile aget. And even better, if you insert more services to your solution, the AppHost will automaticassociate pick them up, too.

In Visual Studio, you can set the AppHost project as the commenceup project in Visual Studio by right clicking on the AppHost and clicking Set Default Project and hitting Start.

If you’re using Visual Studio Code, trade the satisfieds of your begin.json file with the adhereing and then hitting Run in the Run and Debug panel.

 {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Run AppHost",
                "type": "dotnet",
                "request": "launch",
                "projectPath": "${workspaceFolder}\AppHost\AppHost.csproj"
            }
        ]
    }

Hey, we’ve got a dashboard!

Remember how we had to browse to a bunch of contrastent URLs to see our app and its health examines? Now, the AppHost will automaticassociate begin a dashboard with all our services and their depfinishencies. It rolls up all the health examines, pursues, logs, and proposeation enjoy environment variables in one place. And, if we insert more services to our solution, they’ll automaticassociate show up in the dashboard. Let’s consent a see.

First, let’s consent a see at the resources. This is a handy cataloging of all the resources in our solution. We can see the API and MyWeatherHub projects and watch their state as they commence up. We also get clickable joins to their finishpoints, logs, and pursues.

The ServiceDefaults project we inserted earlier automaticassociate configures tracing for all of our projects. We can see that in the Traces tab. This is a fantastic way to comfervent timing and depfinishencies in our app.

The Metrics tab shows us a lot of proposeation about our app, including CPU and memory usage, and the number of asks and errors. Aget, this is all automaticassociate set up for us by the ServiceDefaults project and exposed in the AppHost dashboard.

The Structured tab shows us all the set upd logs from our app. This is a fantastic way to see errors and other meaningful proposeation in our app.

Summary

The point is, .NET Aspire isn’t fair for new apps or huge go inpelevate solutions. It’s for you, right now, to originate your existing apps better. You can insert it to your existing solutions and get a lot of advantages with fair a scant lines of code. And, if you’re not ready to participate more progressd features enjoy service discovery or compriseerized deployment, that’s okay. You can still advantage from the sproposeedy and reliability that .NET Aspire conveys to your apps.

Source join


Leave a Reply

Your email address will not be published. Required fields are marked *

Thank You For The Order

Please check your email we sent the process how you can get your account

Select Your Plan