# Self Hosted

## .NET Core

Wisej 3 .NET Core applications are self-hosted by default being standard ASP.NET Core applications. ASP.NET Core apps are built to an executable that you can simply start and will start hosting your application. **You don't need to use Wisej.HostService.exe**.

:point\_right: If you are deploying on a [Linux ](https://docs.wisej.com/deployment/targets/linux-macos)distribution or on [MacOS](https://docs.wisej.com/deployment/targets/linux-macos), make sure [libgdiplus ](https://www.mono-project.com/docs/gui/libgdiplus/)is installed.

{% hint style="success" %}
Starting with Wisej.NET 4, the use of `libgdiplus` is no longer necessary. This change is due to the development of a new managed System.Drawing library, which has been created in conjunction with [ImageSharp](https://sixlabors.com/products/imagesharp/).
{% endhint %}

### Basic Steps

1. Run the [publishing tool](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-6.0) to deploy to a folder.

<div align="left"><img src="https://1993823814-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIuSWPmpejGB_3Up-Wy%2Fuploads%2FVvtorkeA1wV4pNEQWKMp%2Fimage.png?alt=media&#x26;token=e99a7bde-90bf-4b35-bc22-2526eb62c5ca" alt=""></div>

That's it. When you run YourApplication.exe you can use the **--urls** command line argument to [configure the application's endpoints](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/?view=aspnetcore-6.0).

### Service

Wisej 3 ASP.NET Core apps can be deployed as a [Windows service](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0) quite easily:

1. Add the [WindowsServices](https://www.nuget.org/packages/Microsoft.Extensions.Hosting.WindowsServices) nuget to your project.
2. Add builder.Host.UseWindowsServices() to Startup.cs or Startup.vb (see code snippet below).
3. Register the service using "[sc create](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/sc-create)"
4. Now you can start the service like any other Windows service. For any additional configuration  option refer to the Microsoft documentation.

{% tabs %}
{% tab title="C#" %}

```csharp
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
  Args = args,
  WebRootPath = "./",

    // Add this:
  ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});

// And this
builder.Host.UseWindowsService();

...
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
' Add UseWindowsService() and UseContentRoot like shown below
Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureWebHostDefaults(
    Sub(builder)
        builder.UseWebRoot("./")
        builder.UseStartup(Of Startup)()
        builder.UseContentRoot(If(WindowsServiceHelpers.IsWindowsService(), AppContext.BaseDirectory, Nothing))
    End Sub).Build().Run()

```

{% endtab %}
{% endtabs %}

### Security

When deploying on .NET Core, you are unable to use the `web.config` file to configure the `System.Web.HttpForbiddenHandler`, which is typically used to block the downloading of potentially sensitive `.json` files. As a result, these `.json` files could become accessible for downloading, which might pose a security vulnerability.

Since ASP.NET Core relies on middleware modules instead of using traditional HttpHandlers and HttpModules, to secure specific files or directories, you need to modify the configuration in the `Startup.cs` file. Adjusting the middleware settings within `Startup.cs` allows you to implement security measures to protect sensitive files or directories effectively.

Below is an example of how to block access to specific files or directories in ASP.NET Core, based on your security requirements. You can customize the middleware configuration in the `Startup.cs` file to meet your particular security needs.

{% code title="Conditional UseFileServer" %}

```csharp
// app.UseFileServer();
app.UseWhen(
 cx => !cx.Request.Path.Value.EndsWith(".json", StringComparison.OrdinalIgnoreCase), 
 app => app.UseFileServer()
);
```

{% endcode %}

## .NET Framework

You can run a Wisej application (including Wisej 3 on .NET 4.8) as a self hosting process or service. All you need to do is deploy the [Wisej.HostService](https://github.com/iceteagroup/wisej-extensions/tree/2.5/HostService) executable in the root folder of the deployment directory.

### Basic Steps

1. Create the application directory
2. Copy **Wisej.HostService.exe** to the application directory
3. Create the **/bin** directory in the application's directory
4. Copy all the assemblies from your local /bin to the server's **/bin** (no need to copy xml and pdb files)
5. Copy the **/Themes** folder if you have custom themes or mixins
6. Copy the **/Images** folder if you have images that need to be served as URLs
7. Copy the **/App\_Data** folder if your application uses it
8. Copy [Web.config](#web.config)
9. Copy Default.html (and other html files you use sub-applications)
10. Copy Default.json (and other json configuration files if you use sub-applications)
11. Copy favicon.ico.

{% hint style="info" %}
Wisej.HostService is currently only available in GitHub - you need to download the source code and compile the executable.
{% endhint %}

Once the deployment directory is ready, you can either register Wisej.HostService.exe as a Windows service, or run it directly as a normal process.

{% hint style="danger" %}
Self Hosting uses the classic pipeline instead of the integrated pipeline in web.config. If the classic configuration is not already present in your web.config, you need to add it. See the [web.config ](#web.config)section below to see what to add.
{% endhint %}

These are the supported startup arguments:

|                                                   |                                                                                                                              |
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| (none) or **-start**                              | Starts the host process for the current Wisej application: where the process is located.                                     |
| **-stop**                                         | Stops the process for the current Wisej application                                                                          |
| **-p**:{port} or **-port**:{port}                 | Changes the port. The default is 8080.                                                                                       |
| **-d**:{domain name} or **-domain**:{domain name} | Limits the domain: i.e. \* = all, or localhost to accept local connections only. The default is \*.                          |
| **-i** or **-install**                            | Installs Wisej.HostService as a Windows service for the current Wisej application.                                           |
| **-u** or **-uninstall**                          | Uninstalls Wisej.HostService from the Windows services.                                                                      |
| **-n**:{name} or **-name**:{name}                 | Changes the name of the service, otherwise Wisej sets the name to “Wisej.HostService: “ + {Name of Wisej Application Folder} |

{% hint style="warning" %}
Listening on port 80 may be restricted by the OS unless the process is started as Administrator.
{% endhint %}

### Service

Register the executable to run as a service:

```shell
>Wisej.HostService -i -p:8181
```

You can start the same service multiple times listening on different ports.

```shell
>Wisej.HostService -i -p:8181
>Wisej.HostService -i -p:8182
>Wisej.HostService -i -p:8183
```

Used in conjunction with a load balancer like NGINX, you can have multiple processes of the same application handle their share of the sessions.

### Process

Run Wisej.HostService.exe as any other process. You can configure Windows to start the process at startup or use Windows Task Scheduler or any other means to start the process.

```shell
>Wisej.HostService -d:localhost
```

Starts the process listening for connections to localhost on port 8080 (the default).

### Web.Config Classic

If your web.config file doesn't include the \<system.web> section (classic pipeline), add it as shown in the example below. All it needs is the definition of the Wisej **httpModule**. under **\<system.web>** and to turn the validation of the integrated mode in **\<system.webServer>**, otherwise IIS will throw in an error.

```xml
<configuration>
...
  <system.web>
    <compilation debug="false" />
    <httpRuntime targetFramework="4.8" maxRequestLength="1048576"/>
    <httpModules>
      <add name="Wisej" type="Wisej.Core.HttpModule, Wisej.Framework"/>
    </httpModules>
  </system.web>
...
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
...
```

Once the web.config file includes these settings it can be used with IIS, self hosting, standalone, and Ultidev/Cassini deployments.
