Skip to content

Consider adding AllowRejection to formalize rejection #178

@KirillOsenkov

Description

@KirillOsenkov

Specifying AllowDefault on an import allows us to still instantiate the part being composed if that import is not found.

It would be useful to express the current semantics of rejection if AllowDefault is missing, but without causing an error in the composition. This would formalize the implicit expectation that the part will get rejected if the import is missing.

The big problem with rejection is it can have unforeseen consequences as it's transitive. Also having errors in the composition is not great.

The Roslyn team has a usage scenario that goes like this:

We have [Export] components with required imports, knowing that in some scenarios the import will not be available and the linked export will silently also not be part of the catalog. We are using this to simulate a behavior where a MEF export in package A behaves as though it was provided by package B, where package A is always available but package B is optional. By implementing the export in package A with a required import from package B, the export becomes fully-conditioned on B.

Currently this scenario causes by-design errors in the composition. So hosts that rely on composition errors for diagnostics and investigation of real failures now get to either filter the by-design errors or manually filter the types that will get rejected in their composition.

I'm guessing one thing we can do is add

[Import(AllowRejection = true)]

or

[Import(RejectIfMissing = true)]

or

[Import(CausesRejectionIfMissing = true)]

and don't report an error in the composition if the part is rejected.

See some discussion here:
dotnet/roslyn#42121

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions