none
MEF2 RegistrationBuilder.ForTypesMatching not exporting the parts after updating the system with KB2840642v2 RRS feed

  • Question

  • We are using MEF2 RegistrationBuilder.ForTypesMatching for exporting the types from the class library. I have attached the sample code below. This is working fine before updating the system with the Microsoft update KB2840642v2. But is not exporting any parts after updated the system with  the update KB2840642v2. One thing we noticed is that it is not working when I put this code in Global.asax.cs. But the code is working fine on a console application. The same issue is there on .net 4.5 and .net 4.5.1. Tested the same result on Visual Studio 2012 and Visual Studio 2013.

    Looking for a fix or workaround for this.

    using HandlerModule;
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Registration;
    using System.Reflection;
    using System.Web.Hosting;

    namespace WebApplication1
    {
        public class Global : System.Web.HttpApplication
        {
            private const string TaskServices = "HandlerModule.dll";

            protected void Application_Start(object sender, EventArgs e)
            {

                var reg = new RegistrationBuilder();

                string path = HostingEnvironment.MapPath("~/bin");

                AggregateCatalog mAggrCatalog = new AggregateCatalog();

                mAggrCatalog.Catalogs.Add(new DirectoryCatalog(path, TaskServices, reg));


                reg.ForTypesMatching(t => IsDescendentOf(t, typeof(IRequestHandler<,>)))
                            .Export(builder => builder.AsContractType(typeof(IRequestHandler<,>)));


                //context.ForTypesMatching(t => t.GetInterface(typeof(IRequestHandler<,>).Name) != null)
                //            .ExportInterfaces());


                var container = new CompositionContainer(mAggrCatalog, CompositionOptions.DisableSilentRejection);

                container.ComposeExportedValue(mAggrCatalog);
            }

            private static bool IsGenericDescendentOf(TypeInfo openType, TypeInfo baseType)
            {
                if (openType.BaseType == null)
                {
                    return false;
                }
                if (openType.BaseType == baseType.AsType())
                {
                    return true;
                }
                foreach (Type type in openType.ImplementedInterfaces)
                {
                    if (type.IsConstructedGenericType && (type.GetGenericTypeDefinition() == baseType.AsType()))
                    {
                        return true;
                    }
                }
                return IsGenericDescendentOf(IntrospectionExtensions.GetTypeInfo(openType.BaseType), baseType);
            }

            private static bool IsDescendentOf(Type type, Type baseType)
            {
                if (((type == baseType) || (type == typeof(object))) || (type == null))
                {
                    return false;
                }
                TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);
                TypeInfo info2 = IntrospectionExtensions.GetTypeInfo(baseType);
                if (typeInfo.IsGenericTypeDefinition)
                {
                    return IsGenericDescendentOf(typeInfo, info2);
                }
                return info2.IsAssignableFrom(typeInfo);
            }
        }
    }

    #The classes I'm exporting are on the HandlerModule class library project. Code below

    namespace HandlerModule
    {
        public interface IRequestHandler<TRequest, TResponse>
        {
            TResponse Handle(TRequest request);
        }
    }

    namespace HandlerModule
    {
        public class GetOrderRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
            where TRequest: class
            where TResponse: class
        {

            public GetOrderRequestHandler()
            {
            }

            public TResponse Handle(TRequest pRequest)
            {
                    return "MyREsponse" as TResponse;
            }
        }
    }

    Thursday, November 21, 2013 5:19 AM

Answers

All replies

  • We are using MEF2 RegistrationBuilder.ForTypesMatching for exporting the types from the class library. I have attached the sample code below. This is working fine before updating the system with the Microsoft update KB2840642v2. But is not exporting any parts after updated the system with  the update KB2840642v2. One thing we noticed is that it is not working when I put this code in Global.asax.cs. But the code is working fine on a console application. The same issue is there on .net 4.5 and .net 4.5.1. Tested the same result on Visual Studio 2012 and Visual Studio 2013.

    Looking for a fix or workaround for this.


    using HandlerModule;
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Registration;
    using System.Reflection;
    using System.Web.Hosting;

    namespace WebApplication1
    {
        public class Global : System.Web.HttpApplication
        {
            private const string TaskServices = "HandlerModule.dll";

            protected void Application_Start(object sender, EventArgs e)
            {

                var reg = new RegistrationBuilder();

                string path = HostingEnvironment.MapPath("~/bin");

                AggregateCatalog mAggrCatalog = new AggregateCatalog();

                mAggrCatalog.Catalogs.Add(new DirectoryCatalog(path, TaskServices, reg));


                reg.ForTypesMatching(t => IsDescendentOf(t, typeof(IRequestHandler<,>)))
                            .Export(builder => builder.AsContractType(typeof(IRequestHandler<,>)));


                //context.ForTypesMatching(t => t.GetInterface(typeof(IRequestHandler<,>).Name) != null)
                //            .ExportInterfaces());


                var container = new CompositionContainer(mAggrCatalog, CompositionOptions.DisableSilentRejection);

                container.ComposeExportedValue(mAggrCatalog);
            }

            private static bool IsGenericDescendentOf(TypeInfo openType, TypeInfo baseType)
            {
                if (openType.BaseType == null)
                {
                    return false;
                }
                if (openType.BaseType == baseType.AsType())
                {
                    return true;
                }
                foreach (Type type in openType.ImplementedInterfaces)
                {
                    if (type.IsConstructedGenericType && (type.GetGenericTypeDefinition() == baseType.AsType()))
                    {
                        return true;
                    }
                }
                return IsGenericDescendentOf(IntrospectionExtensions.GetTypeInfo(openType.BaseType), baseType);
            }

            private static bool IsDescendentOf(Type type, Type baseType)
            {
                if (((type == baseType) || (type == typeof(object))) || (type == null))
                {
                    return false;
                }
                TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);
                TypeInfo info2 = IntrospectionExtensions.GetTypeInfo(baseType);
                if (typeInfo.IsGenericTypeDefinition)
                {
                    return IsGenericDescendentOf(typeInfo, info2);
                }
                return info2.IsAssignableFrom(typeInfo);
            }
        }
    }


    #The classes I'm exporting are on the HandlerModule class library project. Code below

    namespace HandlerModule
    {
        public interface IRequestHandler<TRequest, TResponse>
        {
            TResponse Handle(TRequest request);
        }
    }


    namespace HandlerModule
    {
        public class GetOrderRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
            where TRequest: class
            where TResponse: class
        {

            public GetOrderRequestHandler()
            {
            }

            public TResponse Handle(TRequest pRequest)
            {
                    return "MyREsponse" as TResponse;
            }
        }
    }

     

     

    Thursday, November 21, 2013 6:09 AM
  • Hi Noushad,

    There are many similar cases where customer’s app doesn’t work after installing this KB. You could see these pages below:

    http://connect.microsoft.com/VisualStudio/feedback/details/802418/visual-studio-2012-database-project-does-not-open-after-installation-of-security-update-for-microsoft-net-framework-4-5-kb2840642v2.

    http://social.technet.microsoft.com/Forums/lync/en-US/e1d2c1b0-166e-416d-a49f-4b9419394c79/lync-connectivity-analyzer-crashes-on-startup?forum=lyncdeploy.

    http://social.technet.microsoft.com/Forums/lync/en-US/b567c9d0-2dd9-406e-848c-1c99ad123787/lync-network-analyzer-3264-unable-to-start?forum=ocsclients.

    The solution is to uninstall this KB, and wait for the update later.

    Hope useful to you.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 1:52 AM
    Moderator
  • I installed visual studio 2013 ultimate and updated my system. The update KB2840642v2 now not available on the system. It looks like this KB is removed by some newer update or Visual Studio 2013 installation. The same issue is still there.  Which new update override the old KB KB2840642v2 ?

    Our application is on production and breaking because of this.

    The sample application to reproduce this issue is shared at my skydrive . The issue is on WebApplication1

    https://skydrive.live.com/redir?resid=1C187FBADD7CAC72%21217 

    Could somebody  from Microsoft .Net Framework team suggest a solution for this. 

     

     

    Friday, November 22, 2013 6:36 PM
  • Please don't uninstall the patch. This will leave you open for attack.

    I suspect this is occurring because you do not have a targetFramework value of "4.5" set on the httpRuntime in your web.config. See this page for more information:

    http://blogs.msdn.com/b/webdev/archive/2012/11/19/all-about-httpruntime-targetframework.aspx


    Base Class Library Team (BCL) | My Blog: http://davesbox.com


    Tuesday, December 3, 2013 5:34 PM
    Moderator