Azure / Azure/azure-functions-host

WebHost service provider fails to handle covariant types

Open
#7,027 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

#### Check for a solution in the Azure portal
For issues in production, please check for a solution to common issues in the Azure portal before opening a bug. In the Azure portal, navigate to your function app, select `Diagnose and solve problems` from the left, and view relevant dashboards before opening your issue.

#### Investigative information

Please provide the following:

- Timestamp: N/A
- Function App version: N/A
- Function App name: N/A
- Function name(s) (as appropriate): N/A
- Invocation ID: N/A
- Region: N/A

#### Repro steps

1. Create a new AzF in Visual Studio (any trigger).
2. Create a Startup class with the associated `FunctionsStartup` attribute.
3. Add a service registration for a concrete implementation of a covariant interface.
4. Start the function in the emulator.

#### Expected behavior

The service should start without errors.

#### Actual behavior

An exception is logged in the console:
> A host error has occurred during startup operation
> Microsoft.Azure.WebJobs.Script.WebHost: Registering implementation type IFactory is not assignable to service type Factory.

#### Known workarounds

No known workarounds for this problem; covariant type implementations are considered by the host to be unassignable to the interface.

#### Related information

A simple example of this problem:

```csharp
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;

[assembly: FunctionsStartup(typeof(CovarianceTest.Startup))]

namespace CovarianceTest
{
public interface IFactory
{
T Create();
}

public class Factory : IFactory
{
public T Create()
{
throw new NotImplementedException();
}
}

public interface IClient
{
}

public class Client : IClient
{
}

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient, Factory>();
}
}
}

Contributor guide

Open the contributing guide

Research direction

Start with the repro's FunctionsStartup.Configure entry point and the builder.Services.AddTransient registration, then run the function in the emulator to observe the host validation error. Trace where the host checks assignability for the covariant IFactory and Factory types; done means the function starts without the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.