Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TryGetInstance to GetInstance #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public StructureMapServiceProvider(IContainer container)

public object GetService(Type serviceType)
{
if (serviceType.IsGenericEnumerable())
// TryGetInstance doesn't resolve instances of concrete types
try
{
// Ideally we'd like to call TryGetInstance here as well,
// but StructureMap does't like it for some weird reason.
return GetRequiredService(serviceType);
return Container.GetInstance(serviceType);
}
catch (StructureMapConfigurationException)
{
return null;
}

return Container.TryGetInstance(serviceType);
}

public object GetRequiredService(Type serviceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public void can_start_and_tear_down_a_scope_from_the_provider()
Assert.IsType<BlueWidget>(provider.GetRequiredService(typeof(IWidget)));
}

[Fact]
public void get_service_without_registered_types_return_null()
{
var root = new Container();

var provider = new StructureMapServiceProvider(root);
Assert.Same(provider.Container, root);
Assert.Null(provider.GetService(typeof(IWidget)));
}

public interface IWidget { }

public class BlueWidget : IWidget { }
Expand Down