0

Is it possible to interact with Sitecore's DI container from the SPE command line? I was able to do this:

PS master:\>[Sitecore.DependencyInjection.ServiceLocator]::IsConfigured
True

But when I try to get a registered class, I get a strange error:

PS master:\>[Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider::GetService([Microsoft.Extensions.Logging.ILogger])

 Method invocation failed because [Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope] does not contain a method named 'GetService'.
 At line:1 char:1
 + [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider::GetSe ...
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

ServiceProviderEngineScope does have a GetService method, which makes me thing this error is due to some other syntax issue.

1 Answer 1

0

Ah, had a small syntax error. The second :: should have been a .:

 PS master:\>[Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Microsoft.Extensions.Logging.ILogger])

 Microsoft.Extensions.Logging.Logger`1[System.Object]

Get-Member shows I've indeed loaded the object:

PS master:\>[Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Microsoft.Extensions.Logging.ILogger])| Get-Member
 
 
TypeName: Microsoft.Extensions.Logging.Logger`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
 
Name        MemberType Definition
----        ---------- ----------
BeginScope  Method     System.IDisposable ILogger.BeginScope[TState](TState state)
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
IsEnabled   Method     bool ILogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
Log         Method     void ILogger.Log[TState](Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception exception, System.Func[...
ToString    Method     string ToString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.