1

I have created a web api project and run as a windows service. I have added MEF to load a dll (LibOne.dll) and use it in the controller (ValueController). But unable to get the values in the imported interface.

I followed this link to implement this and added MefDependencyResolver in my web api project. How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API

in the values controller the IMyClass is returning always null. How to resolve this? Is there anything I am mising?

Here is my Web Api Controller

    [Export(typeof(ValuesController))]
    public class ValuesController : ApiController
    {
        [Import(typeof(IMyClass))]
        private IMyClass Myclass { get; set; }
        public String GetString(Int32 id)
        {
            return Myclass.GetValues();
        }
    }

SelfHosted Service to run the web api as windows service

protected override void OnStart(string[] args)
       {
           var config = new HttpSelfHostConfiguration("http://localhost:8080");
           Thread.Sleep(10000);
           MefConfig.RegisterMef(config);
           config.Routes.MapHttpRoute(
              name: "API",
              routeTemplate: "{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );

           HttpSelfHostServer server = new HttpSelfHostServer(config);
           server.OpenAsync().Wait();
       }

MEF registration

public static void RegisterMef(HttpConfiguration config)
        {
            var assemblyCatalog = new AggregateCatalog();

            assemblyCatalog.Catalogs.Add(new DirectoryCatalog(@""));
            var container = new CompositionContainer(assemblyCatalog);
            var resolver = new MefDependencyResolver(container);
            config.DependencyResolver = resolver;
        }

Exporting this class from LibOne.dll

[Export(typeof(IMyClass))]
   public class Super :IMyClass
   {
       public string GetValues()
       {
           return "My value";
       }
   }

MEFDependencyResolver:

internal class MefDependencyResolver : IDependencyResolver
    {
        private readonly CompositionContainer _container;

        /// <summary />
        /// <param name="container"></param>
        public MefDependencyResolver(CompositionContainer container)
        {
            _container = container;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>

        public IDependencyScope BeginScope()
        {
            return this;
        }

        /// <summary>
        /// Called to request a service implementation.
        /// 
        /// Here we call upon MEF to instantiate implementations of dependencies.
        /// </summary>
        /// <param name="serviceType">Type of service requested.</param>
        /// <returns>Service implementation or null.</returns>
        public object GetService(Type serviceType)
        {
            if (serviceType == null)
                throw new ArgumentNullException("serviceType");

            var name = AttributedModelServices.GetContractName(serviceType);
            var export = _container.GetExportedValueOrDefault<object>(name);
            return export;
        }

        /// <summary>
        /// Called to request service implementations.
        /// 
        /// Here we call upon MEF to instantiate implementations of dependencies.
        /// </summary>
        /// <param name="serviceType">Type of service requested.</param>
        /// <returns>Service implementations.</returns>
        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (serviceType == null)
                throw new ArgumentNullException("serviceType");

            var exports = _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
            return exports;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {

        }
    }
SGDemo
  • 129
  • 3
  • 11

0 Answers0