Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: UWP + Shared scope

$
0
0
Hi All,
I may be doing some stupid here as Shared do not seems to be working for me at all. Multiple instances created. Here it is:

Define interfaces:
namespace TestMef
{
    public interface IMainPageViewController
    {
        void DoWork();
    }
}

namespace TestMef
{
    public interface IRepoA
    {
        void DoWorkA();
    }
}

namespace TestMef
{
    public interface IRepoB
    {
        void DoWorkB();
    }
}

namespace TestMef
{
    public interface IWorker
    {
        void DoStuff();
    }
}
Create implementation
using System.Composition;
using System.Diagnostics;

namespace TestMef
{
    [Export(typeof(IRepoA)), Shared]
    public class RepoA : IRepoA
    {
        public RepoA()
        {
            Debug.WriteLine("Created RepoA");
        }

        public void DoWorkA()
        {
            Debug.WriteLine("DoWorkA");
        }
    }
}

using System.Composition;
using System.Diagnostics;

namespace TestMef
{
    [Export(typeof(IRepoB)), Shared]
    public class RepoB : IRepoB
    {
        public RepoB()
        {
            Debug.WriteLine("Created RepoB");
        }

        public void DoWorkB()
        {
            Debug.WriteLine("DoWorkB");
        }
    }
}

using System.Composition;
using System.Composition.Hosting;
using System.Diagnostics;
using System.Reflection;

namespace TestMef
{
    [Export(typeof(IWorker))]
    public class Worker : IWorker
    {
        [Import]
        public IRepoA RepoA { get; set; }

        [Import]
        public IRepoB RepoB { get; set; }

        public Worker()
        {
            Debug.WriteLine("Created Worker");
            var configuration = new ContainerConfiguration().WithAssembly(typeof(App).GetTypeInfo().Assembly);
            using (var container = configuration.CreateContainer())
            {
                container.SatisfyImports(this);
            }
        }

        public void DoStuff()
        {
            Debug.WriteLine("DoStuff");
        }
    }
}

using System.Composition;
using System.Composition.Hosting;
using System.Diagnostics;
using System.Reflection;

namespace TestMef
{
    [Export(typeof(IMainPageViewController))]
    public class MainPageViewController : IMainPageViewController
    {
        [Import]
        public IRepoA RepoA { get; set; }

        [Import]
        public IRepoB RepoB { get; set; }

        [Import]
        public IWorker IWorker { get; set; }

        public MainPageViewController()
        {
            Debug.WriteLine("Created MainPageViewController");
            var configuration = new ContainerConfiguration().WithAssembly(typeof(App).GetTypeInfo().Assembly);
            using (var container = configuration.CreateContainer())
            {
                container.SatisfyImports(this);
            }
        }

        public void DoWork()
        {
            Debug.WriteLine("DoWork");
        }
    }
}
Add this to main page class
using System.Composition;
using System.Composition.Hosting;
using System.Reflection;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;


namespace TestMef
{
    public sealed partial class MainPage : Page
    {
        [Import]
        public IMainPageViewController MainPageViewController { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var configuration = new ContainerConfiguration().WithAssembly(typeof(App).GetTypeInfo().Assembly);
            using (var container = configuration.CreateContainer())
            {
                container.SatisfyImports(this);
            }
        }
    }
}
But the output not exactly what I expect

'TestMef.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'Anonymously Hosted DynamicMethods Assembly'.
Created MainPageViewController
Created RepoA
Created RepoB
Created Worker
Created RepoA
Created RepoB
Created RepoA
Created RepoB
Created Worker
Created RepoA
Created RepoB
The program '[9420] TestMef.exe' has exited with code 0 (0x0).


WTF is going on ?

Is there any good tutorial for this version of MEF for UWP.

I am using version 1.0.30

Many thanks.

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>