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

Named services cyclic dependency #172

Open
MitchRazga opened this issue Feb 16, 2024 · 0 comments
Open

Named services cyclic dependency #172

MitchRazga opened this issue Feb 16, 2024 · 0 comments

Comments

@MitchRazga
Copy link

Hi,
I've been playing around with the new named services feature and thought it could useful for resolving relative URIs into absolute URIs.

Before named services this would have been difficult to achieve since having a Uri depend on another Uri understandably causes a cyclic dependency.
However, now that a specific registration can be injected by name this seems like this should be possible but still causes the cyclic dependency. Is this by design?

I've recreated a basic repro. Please see attached.
JabNamedServices.zip

[ServiceProvider]
[Singleton<Uri>(Factory = "BaseUriFactory", Name = "Base")]
[Singleton<Uri>(Factory = "FullUriFactory", Name = "Full")]
internal partial class Services
{
	static Uri BaseUriFactory() => new(@"C:\");

	static Uri FullUriFactory([FromNamedServices("Base")] Uri baseUri)
	{
		Uri relativeUri = new(@"Test\", UriKind.Relative);
		return new(baseUri, relativeUri);
	}
}

As a workaround I've found I can just inject IServiceProvider for a runtime resolution which is still pretty useful.

static Uri FullUriFactory(IServiceProvider serviceProvider)
{
	Uri baseUri = serviceProvider.GetRequiredKeyedService<Uri>("Base");
	Uri relativeUri = new(@"Test\", UriKind.Relative);
	return new(baseUri, relativeUri);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant