0

Using Unity, I am currently receiving this error message when attempting to resolve a type:

The current type, Onboard.UI.Services.IMessagingService, is an interface and cannot be constructed

Below, is the code found in my App.xaml.cs file that is throwing this error.

UnityContainer
    .RegisterType<IMessagingService, SessionMessagingService>("SessionMessageService")

var sessionStateNotifactionService =
    UnityContainer
        .Resolve <SessionStateNotificationService>("SessionMessageService");

I am trying to .RegisterType<> on two types that share the same interface IMessageService. If I remove the attempt at trying to register a named type, e.g. the ("SessionMessageService") parameter, everything works well; however, I no longer have a way to distinguish between which implemenation of the interface I wish to use.

The error is thrown on the call to .Resolve as the constructor for SessionStateNotificationService looks like

// ... class

private IMessagingService _messagingService;

public SessionStateNotificationService(IMessagingService messagingService)
{
    // ...

The attempt here is to be more generic about what implementations I can offer to the SessionStateNotificationService.

There are a number of questions regarding this error on StackOverflow, namely:

Exception is: InvalidOperationException - The current type, is an interface and cannot be constructed. Are you missing a type mapping?

However, that question arises out of that OP doing some odd things and the accepted answer isn't applicable to the above case (which really attempts to follow the Unity documentation).

What am I missing?

Community
  • 1
  • 1
Thomas
  • 6,291
  • 6
  • 40
  • 69
  • how are you registering your other `IMessagingService`? – Ric Oct 15 '15 at 12:13
  • @Ric the same way, with a different name, e.g. `("BrowseMessageService")`. The implementation (type) is different. So it looks like `.RegisterType("BrowseMessagService");` However, even if I remove that registration, it still fails. – Thomas Oct 15 '15 at 12:14
  • 1
    you need to `Resolve` using the interface from what I've seen and done `UnityContainer.Resolve("namehere")` – Ric Oct 15 '15 at 12:16
  • I gave that a try and still received the error. I meant to mention that in my question. I am absolutely baffled. – Thomas Oct 15 '15 at 12:21
  • Ok, have you tried this: `UnityContainer.Resolve(new InjectionConstructor( new ResolvedParameter("SessionMessageService")));` – Ric Oct 15 '15 at 12:25
  • @Ric I get a compiler error stating "Argument 2: Cannot convert from 'Microsoft.Practices.Unity.InjectionConstructor` to 'Microsoft.Practices.Unity.ResolverOverride' – Thomas Oct 15 '15 at 12:30

1 Answers1

2

Assuming multiple registrations:

UnityContainer
    .RegisterType<IMessagingService, SessionMessagingService>("SessionMessageService")

UnityContainer
    .RegisterType<IMessagingService, BrowseMessagingService>("BrowseMessagingService")

Ensure that SessionStateNotificationService is registered and that the named dependency exists:

UnityContainer.
     RegisterType<SessionStateNotificationService>(
        new InjectionConstructor(new ResolvedParameter<IMessagingService>("SessionMessageService")));

now resolve:

UnityContainer.Resolve<SessionStateNotificationService>(); // uses the SessionMessageService
Ric
  • 12,855
  • 3
  • 30
  • 36