/// <summary>
/// A composite implementation of <see cref="IServiceProvider"/> that is created
/// with two other <see cref="IServiceProvider"/> implementations, and resolves instances
/// by trying with the first and if didn't succeed then trying with the second
/// </summary>
public class OrServiceProvider : IServiceProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="OrServiceProvider"/> class.
/// </summary>
/// <param name="primaryServiceProvider">
/// The primary service provider to get services from.
/// </param>
/// <param name="secondaryServiceProvider">
/// The secondary service provider to get services from in case the primary can't provide them.
/// </param>
public OrServiceProvider( IServiceProvider primaryServiceProvider, IServiceProvider secondaryServiceProvider)
{
_primaryServiceProvider = Guard.CheckNullArgument ( primaryServiceProvider, nameof( primaryServiceProvider) ) ;
_secondaryServiceProvider = Guard.CheckNullArgument ( secondaryServiceProvider, nameof( secondaryServiceProvider) ) ;
}
/// <inheritdoc/>
public object GetService( Type serviceType)
{
return _primaryServiceProvider.GetService ( serviceType) ?? _secondaryServiceProvider.GetService ( serviceType) ;
}
private readonly IServiceProvider _primaryServiceProvider;
private readonly IServiceProvider _secondaryServiceProvider;
}
Ly8vIDxzdW1tYXJ5PgovLy8gQSBjb21wb3NpdGUgaW1wbGVtZW50YXRpb24gb2YgPHNlZSBjcmVmPSJJU2VydmljZVByb3ZpZGVyIi8+IHRoYXQgaXMgY3JlYXRlZAovLy8gd2l0aCB0d28gb3RoZXIgPHNlZSBjcmVmPSJJU2VydmljZVByb3ZpZGVyIi8+IGltcGxlbWVudGF0aW9ucywgYW5kIHJlc29sdmVzIGluc3RhbmNlcwovLy8gYnkgdHJ5aW5nIHdpdGggdGhlIGZpcnN0IGFuZCBpZiBkaWRuJ3Qgc3VjY2VlZCB0aGVuIHRyeWluZyB3aXRoIHRoZSBzZWNvbmQKLy8vIDwvc3VtbWFyeT4KcHVibGljIGNsYXNzIE9yU2VydmljZVByb3ZpZGVyIDogSVNlcnZpY2VQcm92aWRlcgp7CiAgICAvLy8gPHN1bW1hcnk+CiAgICAvLy8gSW5pdGlhbGl6ZXMgYSBuZXcgaW5zdG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m4gX3ByaW1hcnlTZXJ2aWNlUHJvdmlkZXIuR2V0U2VydmljZShzZXJ2aWNlVHlwZSkgPz8gX3NlY29uZGFyeVNlcnZpY2VQcm92aWRlci5HZXRTZXJ2aWNlKHNlcnZpY2VUeXBlKTsKICAgIH0KCiAgICBwcml2YXRlIHJlYWRvbmx5IElTZXJ2aWNlUHJvdmlkZXIgX3ByaW1hcnlTZXJ2aWNlUHJvdmlkZXI7CiAgICBwcml2YXRlIHJlYWRvbmx5IElTZXJ2aWNlUHJvdmlkZXIgX3NlY29uZGFyeVNlcnZpY2VQcm92aWRlcjsKfQ==
compilation info
prog.cs(6,34): error CS0246: The type or namespace name `IServiceProvider' could not be found. Are you missing `System' using directive?
prog.cs(17,30): error CS0246: The type or namespace name `IServiceProvider' could not be found. Are you missing `System' using directive?
prog.cs(17,71): error CS0246: The type or namespace name `IServiceProvider' could not be found. Are you missing `System' using directive?
prog.cs(24,30): error CS0246: The type or namespace name `Type' could not be found. Are you missing `System' using directive?
prog.cs(29,22): error CS0246: The type or namespace name `IServiceProvider' could not be found. Are you missing `System' using directive?
prog.cs(30,22): error CS0246: The type or namespace name `IServiceProvider' could not be found. Are you missing `System' using directive?
Compilation failed: 6 error(s), 0 warnings
stdout