Getting different instances while resolving from container

BalueBalue ILMember ✭✭

I have a specific platform service which is being registered using a platform initializer

 public class AndroidInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterInstance<IBlueToothService>(new BlueToothService_Android());
        }
    }

As you see I am registering an instance as I want it to be the same instance.
My main page VM get this service injected in its constructor:

public MainPageViewModel(INavigationService navigationService, IBlueToothService btService)
           : base(navigationService)
        {
             RefreshBlueTooothDevicesCommand = new DelegateCommand(RefreshBlueToothExecuted, RefreshBlueToothCanExecute);
            _blueToothService = btService;
            _blueToothService.DeviceAdded += BlueToothServiceDeviceAdded;
            _blueToothService.ScanFinished += () => ToggleRunningState();
        }

I am using this service also in a broadcast receiver:

 public override void OnReceive(Context context, Intent intent)
        {            
           IBlueToothService service = Xamarin.Forms.DependencyService.Get<IBlueToothService>();
            string action = intent.Action;
            if (action == BluetoothDevice.ActionFound)
            {
                BluetoothDevice newDevice = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                service.AddDevice(new Models.BluetoothDevice(newDevice.Name, newDevice.Address));
            }
            if(action == BluetoothAdapter.ActionDiscoveryFinished)
            {
                service.RaiseScanFinishedEvent();
            }
        }

I am not getting the same instance. but 2 different ones. I am pretty sure that each one of them resolves them from a different container but I cant figure our how to point all of them to the same one.

Thanks.

Best Answers

  • BalueBalue IL ✭✭
    Accepted Answer

    so the problem was I as not acceding the right container.
    instead of:

    service = Xamarin.Forms.DependencyService.Get<IBlueToothService>();
    

    it should be

    var service = ((PrismApplication) Xamarin.Forms.Application.Current).Container.Resolve();
    

    you can see the full exmaple here: https://github.com/razamit/XamarinBlueToothExplorer

    Thanks! @NMackay

Answers

  • NMackayNMackay GBInsider, University mod
    edited January 31

    @Balue

     public class AndroidInitializer : IPlatformInitializer
        {
            public void RegisterTypes(IContainerRegistry containerRegistry)
            {
                containerRegistry.RegisterSingleton<IBlueToothService>(BlueToothService_Android());
            }
        }
    

    Register your instance as a Singleton, otherwise the container will return you a fresh instance each time you inject it.

  • BalueBalue ILMember ✭✭

    Hey. What you wrote did not compile.

    I tried the following but still got the same result:

    public void RegisterTypes(IContainerRegistry containerRegistry)
            {
                //containerRegistry.RegisterInstance<IBlueToothService>(new BlueToothService_Android());
                containerRegistry.RegisterSingleton<IBlueToothService,BlueToothService_Android>();
            }
    

    it seems That containerRegistry.RegisterSingleton points to one container and IBlueToothService service = Xamarin.Forms.DependencyService.Get<IBlueToothService>(); points to a different one.

  • NMackayNMackay GBInsider, University mod

    @Balue said:
    Hey. What you wrote did not compile.

    I tried the following but still got the same result:

    public void RegisterTypes(IContainerRegistry containerRegistry)
            {
                //containerRegistry.RegisterInstance<IBlueToothService>(new BlueToothService_Android());
                containerRegistry.RegisterSingleton<IBlueToothService,BlueToothService_Android>();
            }
    

    it seems That containerRegistry.RegisterSingleton points to one container and IBlueToothService service = Xamarin.Forms.DependencyService.Get<IBlueToothService>(); points to a different one.

    Sounds like something is not set up right as we have some singleton platform specific services with no issues.

  • BalueBalue ILMember ✭✭

    @NMackay : This is exactly what I am trying to understand how can it be that the the android section has 1 instance and the shared generic section has a different one.

  • BalueBalue ILMember ✭✭

    @NMackay
    I used Unity.
    its quite a simple application. simply scans bluetooth devices around you

    I would gladly upload it to git if you could give it a look.

  • NMackayNMackay GBInsider, University mod

    @Balue said:
    @NMackay
    I used Unity.
    its quite a simple application. simply scans bluetooth devices around you

    I would gladly upload it to git if you could give it a look.

    Sure, DM me the link if you don't want to share it publicly.

  • BalueBalue ILMember ✭✭
    Accepted Answer

    so the problem was I as not acceding the right container.
    instead of:

    service = Xamarin.Forms.DependencyService.Get<IBlueToothService>();
    

    it should be

    var service = ((PrismApplication) Xamarin.Forms.Application.Current).Container.Resolve();
    

    you can see the full exmaple here: https://github.com/razamit/XamarinBlueToothExplorer

    Thanks! @NMackay

Sign In or Register to comment.