Skip to main content

Install the .NET Framework agent with Web API and Owin

The .NET agent supports analysis of Web API applications that are self-hosted with the Open Web Interface for .NET (OWIN). The Web Api can be deployed as a command line application and Windows service.

Note

Web API applications hosted in the IIS integrated pipeline using the SystemWeb HttpModule and those deployed with an OWIN Host are not supported.

  1. Install the .NET agent with .NET Framework agent installer for Windows.

  2. Set the environment variables, depending on how you deploy your Web API hosted by OWIN.

    • Deployed as a command line application: Set these environment variables before running the command line application that is being used to self-host OWIN:

      Environment variable

      Value

      COR_ENABLE_PROFILING

      1

      COR_PROFILER

      {EFEB8EE0-6D39-4347-A5FE-4D0C88BC5BC1}

      COR_PROFILER_PATH_32

      C:\Program Files\Contrast\dotnet\runtimes\win-x86\native\ContrastProfiler.dll

      COR_PROFILER_PATH_64

      C:\Program Files\Contrast\dotnet\runtimes\win-x64\native\ContrastProfiler.dll

      Note

      COR_PROFILER_PATH_32 / COR_PROFILER_PATH_64 must match the installation directory chosen during the install of the .NET Framework agent.

    • Deployed as a Windows service:

      Install the service that contains the Web API application. Note the name of the service.

      Under the service's registry key, create a REG_MULTI_SZ value called Environment. If there is already an Environment value, add the new values below the existing values.

      Set the required environment variables. Each environment variable key/value pair must be separated by a new line. Environment variables that are unique for each service can be set under that service's registry key. The service's registry key can be found at: HKLM\SYSTEM\CurrentControlSet\Services\YourServiceName.

      Environment variable

      Value

      COR_ENABLE_PROFILING

      1

      COR_PROFILER

      {EFEB8EE0-6D39-4347-A5FE-4D0C88BC5BC1}

      COR_PROFILER_PATH_32

      C:\Program Files\Contrast\dotnet\runtimes\win-x86\native\ContrastProfiler.dll

      COR_PROFILER_PATH_64

      C:\Program Files\Contrast\dotnet\runtimes\win-x64\native\ContrastProfiler.dll

      CONTRAST_CONFIG_PATH

      C:\ProgramData\contrast\dotnet\contrast_security.yaml

      Note

      COR_PROFILER_PATH_32 / COR_PROFILER_PATH_64 must match the installation directory chosen during the install of the .NET Framework agent.

  3. Restart the service so that new values are loaded. This PowerShell script can be used to set the required environment variables:

    param (
        # Name of the service that it was given at installation.
        [Parameter(Mandatory=$true)]
        [string]
        $ServiceName,
    
        # Path to the 64-bit Contrast profiler DLL.
        # Defaults to: "C:\Program Files\Contrast\dotnet\runtimes\win-x64\native\ContrastProfiler.dll"
        [string]
        $ProfilerPath64 = "C:\Program Files\Contrast\dotnet\runtimes\win-x64\native\ContrastProfiler.dll",
    
        # Path to the 32-bit Contrast profiler DLL.
        # Defaults to: "C:\Program Files\Contrast\dotnet\runtimes\win-x86\native\ContrastProfiler.dll"
        [string]
        $ProfilerPath32 = "C:\Program Files\Contrast\dotnet\runtimes\win-x86\native\ContrastProfiler.dll",
    
        # Path to the Contrast agent configuration YAML file.
        # Defaults to: "C:\ProgramData\contrast\dotnet\contrast_security.yaml"
        [string]
        $ConfigYamlPath = "C:\ProgramData\contrast\dotnet\contrast_security.yaml"
    )
    
    if (-Not (Test-Path -Path $ProfilerPath64 -PathType Leaf)) {
        Write-Host "Cannot find 64-bit profiler DLL at path `"$ProfilerPath64`"."
        exit 1
    }
    
    if (-Not (Test-Path -Path $ConfigYamlPath -PathType Leaf)) {
        Write-Host "Cannot find configuration YAML file at path `"$ConfigYamlPath`"."
        exit 1
    }
    
    if (-Not (Test-Path -Path $ProfilerPath32 -PathType Leaf)) {
        Write-Host "Cannot find 32-bit profiler DLL at path `"$ProfilerPath32`"."
        exit 1
    }
    
    # Check if there is a service with the specified name installed.
    $service = Get-Service -Name $ServiceName -ErrorAction Ignore
    
    if ($null -Eq $service) {
        Write-Host "The service `"$ServiceName`" was not found."
        exit 2
    }
    
    # Create value for multiline registry string.
    $values = @(
        "COR_ENABLE_PROFILING=1",
        "COR_PROFILER={EFEB8EE0-6D39-4347-A5FE-4D0C88BC5BC1}",
        "COR_PROFILER_PATH_64=$ProfilerPath64",
        "COR_PROFILER_PATH_32=$ProfilerPath32",
        "CONTRAST_CONFIG_PATH=$ConfigYamlPath"
    )
    
    $registryKey = "HKLM:\SYSTEM\CurrentControlSet\Services\$ServiceName"
    
    # Check if the Environment value already exists.
    $environmentValue = Get-ItemProperty -Path $registryKey -Name "Environment" -ErrorAction Ignore
    
    if ($null -Ne $environmentValue) {
        # Add the Contrast environment variables to the existing variables.
        $existingValues = [System.Collections.ArrayList]@($environmentValue.Environment)
        foreach ($item in $values) {
            $idx = $existingValues.Add($item)
        }
        $values = $existingValues
    }
    
    # Set the environment variables for the service.
    Set-ItemProperty -Path $registryKey -Type MultiString -Name "Environment" -Value $values
    
    # Restart the service so it picks up the new environment variables.
    Restart-Service -Name $ServiceName