MVVM Light SimpleIoC не может найти экземпляр
Я использую WPF MVVM Light SimpleIoC для реализации доступа к моим сервисам. У меня нет проблем с регистрацией и использованием моей виртуальной машины, но когда я регистрирую службу, я не могу использовать ее.
Вот мой код:
public interface IDeviceDataAccessService
{
List<Models.Device> GetDevices();
bool InsertDevice(ref Models.Device device, int userId);
bool RemoveDevice(Models.Device device);
}
Реализация:
public class DeviceDataAccessService : DataAccesBase, IDeviceDataAccessService
{
public DeviceDataAccessService(string connectionString) : base(connectionString)
{
}
...
}
ViewModelLocator
SimpleIoc.Default.Register<IDeviceDataAccessService, DeviceDataAccessService>();
И использовать в коде
SimpleIoc.Default.GetInstance<IDeviceDataAccessService>();
Когда я меняюсь IDeviceDataAccessService
для TestViewModel у меня нет проблем. Но я не могу использовать свой сервис.
Message = "Exception has been thrown by the target of an invocation."
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
arguments, Signature sig, Boolean constructor)\r\n
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,
Object[] parameters, Object[] arguments)\r\n
at System.Delegate.DynamicInvokeImpl(Object[] args)\r\n
at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String
key, Boolean cache) in C:\\Users\\lbugn\\Documents\\MVVMLight\\GalaSoft.MvvmLight\\GalaSoft.MvvmLight.Extras (PCL)\\Ioc\\SimpleIoc.cs:line 622\r\n
at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstance[TService]() in C:\\Users\\lbugn\\Documents\\MVVMLight\\GalaSoft.MvvmLight\\GalaSoft.MvvmLight.Extras (PCL)\\Ioc\\SimpleIoc.cs:line 1059\r\n
at LabDesk.ViewModel.MainViewModel..ctor() in C:\\Users\\kzrostek\\Documents\\Git repo\\labdesk\\LabDESK\\LabDesk\\ViewModel\\MainViewModel.cs:line 141"
InnerException
{"Type not found in cache: System.String."}
1 ответ
Решение
Я забыл, как вы регистрируете строку подключения, но я думаю, что ваша ошибка говорит о том, что конструктор deviceaccesservice:
public DeviceDataAccessService(string connectionString)
Ожидает строку подключения в качестве параметра. Этого нет, чтобы это так... крушение. Я думаю, что вам нужно что-то похожее на:
SimpleIoc.Default.Register<IDeviceDataAccessService>(() => {
return new DeviceDataAccessService("Whatever connectionstring should be");
});