Azure / Azure/azure-functions-host

Resolving interfaces with contravariant type parameter fails

Open
#7,794 7 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

Hello.

I've been playing around with the azure-functions-host repo on my local machine(dev branch) and noticed some weird dependency registration issue. Upon further investigation, it would appear that the WebHost uses a version(or a copy) of the [DryIoC](https://github.com/dadhi/DryIoc) code. I've raised an issue in the DryIoC library(https://github.com/dadhi/DryIoc/issues/432) and it was resolved and the fix was released.

Would it be possible to incorporate that fix in the azure-functions-host as well?

Here is a simplified snippet of what I'm trying to do in my Azure Function(v3, c#):
```csharp
public class Settings
{
public string Address {get; set;}
}

public class ExtraSettings : Settings
{
public string Key {get; set;}
}

public interface IValidator
{
public bool IsValid(T settings);
}

public abstract class AbstractValidator : IValidator
{
public bool IsValid(T settings)
{
return true;
}
}

public class SettingsValidator : AbstractValidator
{

}

// somewhere in Startup.cs

services.AddSingleton, SettingsValidator>(); // this throws
```
This is the error message that I get in logs:
`
Microsoft.Azure.WebJobs.Script.WebHost: Registering implementation type SettingsValidator is not assignable to service type IValidator.
`

The same issue is present when developing a function locally with [azure-functions-core-tools](https://github.com/Azure/azure-functions-core-tools).

Contributor guide

Open the contributing guide

Research direction

Start with the WebHost dependency-registration path and the Startup.cs registration shown in the report, then compare the embedded DryIoC code with the resolved DryIoC issue 432 and its released fix. Done means the contravariant IValidator registration succeeds in Azure Functions and locally through azure-functions-core-tools without the assignability error.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, 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.