# Perigee With .NET Hosting

Here's the full setup guide for a Perigee Application with included API hosting, CORS, and swagger.  You can download the completed result here:

{% file src="<https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FUV1IsSKbnrln51a9U6Dg%2FPerigeeAPI.zip?alt=media&token=833820fe-be59-4723-a39c-9b2e9ac740ff>" %}

### 1- Create an ASP.NET Core Web API project

Using Visual Studio we are going to create a new project. We want to select <mark style="color:red;">**"ASP.NET Core Web API".**</mark>

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FvTvlD6vJPfCYlavs194G%2Fimage.png?alt=media&#x26;token=9cb2c0f8-6799-465b-bed8-b9c3cd550fbb" alt=""><figcaption></figcaption></figure>

### 2- Configure the application as needed

When you get to configuration, feel free to override these settings as desired. An example configuration of this is shown below.

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FUGFZgyoZlSIOU0iH9rHm%2Fimage.png?alt=media&#x26;token=939fb1c2-a44e-400d-9aa2-94b9d6fb56bf" alt=""><figcaption></figcaption></figure>

### 3 - Setup Program.cs

After opening <mark style="color:red;">**Program.cs**</mark> - the template created a project for us which looks like this:&#x20;

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FBDBUFGw760sLh3zQ8sY3%2Fimage.png?alt=media&#x26;token=653237b3-41c5-4935-927d-de08f1851dc9" alt=""><figcaption></figcaption></figure>

We're going to add a few things to this basic setup to get it fully configured for Perigee.&#x20;

* We're going to add the <mark style="color:blue;">`ThreadRegistry`</mark> instance to the builder host services.
* We need to use <mark style="color:blue;">`Serilog`</mark>.
* Let's add CORS for cross origin.
* Instead of running the application in synchronous mode we capture the asynchronous task handle.&#x20;
* Pass it to Perigee.

#### The "new" Program.cs

```csharp
using Perigee;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddSingleton(ThreadRegistry.Instance);
builder.Host.UseSerilog();

//Controllers
builder.Services.AddControllers();

//SwaggerGen - https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer().AddSwaggerGen();

//Build / configure
var app = builder.Build();
if (app.Environment.IsDevelopment()) app.UseSwagger().UseSwaggerUI();

// HTTPS/Auth/Controllers/Cors
app.UseHttpsRedirection().UseAuthorization();
app.MapControllers();
app.UseCors(o => o.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseSerilogRequestLogging();

//Run async!
var taskRunner = app.RunAsync();

//Pass the taskRunner to Perigee initialization
PerigeeApplication.ApplicationNoInit("Hosted Perigee App", (c) => {

    //Add and configure away

    //Sample running config
    c.AddRecurring("Running", (c, l) => { l.LogInformation("I'm running"); });

}, HostTask: taskRunner);

/*********************************************************************/
// TODO:
// Using the package manager console, or the Nuget GUI: install-package Perigee
// Add AppSettings.json and set the "Copy To Output Directory" in the Properties inspector to "Copy Always"
/*********************************************************************/
```

### 4 - Installation

We need to install Perigee as a dependency.&#x20;

Either use the Nuget GUI or the Package Manager Console and type **`install-package Perigee`**

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FaqZ2ZeVNG0ZfJy29b98t%2FInstallPackage.gif?alt=media&#x26;token=24ab90c5-9540-4401-ba25-dd20462d384e" alt=""><figcaption></figcaption></figure>

### 5- AppSettings.json

{% file src="<https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FADoAIhf1F12gpgDVuCc9%2Fappsettings.json?alt=media&token=05f271fd-3676-450d-9ae8-7d53b7f6204e>" %}

The <mark style="color:red;">**`AppSettings.json`**</mark> file is slightly different when using .NET hosting, as we want to turn off the debug level logging for <mark style="color:blue;">`Serilog`</mark> and <mark style="color:blue;">`AspNetcore`</mark>.&#x20;

```json
{
  "ConnectionStrings": {

  },
  "AppSettings": {

  },
  "Perigee": { "HideConsole": false },
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "WriteTo": [
      { "Name": "Console" }
    ]
  },
  "AllowedHosts": "*"
}
```

#### File / Copy Settings

Depending on the template and .NET version you selected the appsettings.json file may or may not exist. Feel free to download and drop in the one provided if it is not automatically included. \
&#x20;  As always, make sure this is set to **"Copy Always" in the Property Inspector**.

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2F4KktEF0FeqLPd3i4YBbo%2Fimage.png?alt=media&#x26;token=c980a3d8-7bef-4df7-bbe3-38070eafee0e" alt=""><figcaption></figcaption></figure>

### 6- License file

Please copy your license file into the project root and set to "Copy Always"

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FEdKzSEBOhGHA5IGNVHJD%2Fimage.png?alt=media&#x26;token=c4971d00-76c1-4d3e-b438-fe358beaa964" alt=""><figcaption></figcaption></figure>

### Running the application

After clicking "Run" - You should see Swagger in the default browser, and the Perigee application console window with .NET hosting running.&#x20;

<figure><img src="https://2203366127-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FloJjRj49SSQfSrp7vuw9%2Fuploads%2FnPIKdxvAZWyPzEXxX0Jo%2Fimage.png?alt=media&#x26;token=fdbe3d54-0edc-4a5f-b6c6-73e5cab72be5" alt=""><figcaption></figcaption></figure>
