2017-09-06 1 views
0

Je place des intercepteurs d'Unity dans mon projet WCF et j'obtiens un System.StackOverflowException quand j'appelle une méthode de mon service. Cela vient des intercepteurs que j'ai mis en place mais je ne sais pas comment le résoudre.Obtention de System.StackOverflowException avec l'intercepteur Unity dans WCF

FYI: Je vérifie si j'avais un circular dependancy ou a non-terminating recursion

J'utilise v4 Unity et Unity.WCF v3.0

Voici le code

l'usine pour le service Web

public class WcfServiceFactory : UnityServiceHostFactory 
{ 
    protected override void ConfigureContainer(IUnityContainer container) 
    { 
     // register all your components with the container here 
     List<string> lstNomAssemblyToLoad = new List<string>(); 
     List<Assembly> lstAssemblyToLoad; 

     lstNomAssemblyToLoad.Add("Service"); 
     lstNomAssemblyToLoad.Add("Interfaces"); 
     lstNomAssemblyToLoad.Add("Bll"); 
     lstNomAssemblyToLoad.Add("Dal"); 

     // Loading specific interface from unity.config 
     container = container.LoadConfiguration(); 

     // Resolving interfaces without explicit declaration 
     lstAssemblyToLoad = AppDomain.CurrentDomain.GetAssemblies().Where(a => lstNomAssemblyToLoad.Contains(a.GetName().Name)).ToList(); 
     container.AddNewExtension<Interception>(); 
     container.RegisterTypes(AllClasses.FromAssemblies(lstAssemblyToLoad), 
           WithMappings.FromAllInterfaces, 
           WithName.Default, 
           WithLifetime.ContainerControlled, 
           getInjectionMembers: c => new InjectionMember[] 
           { 
            new Interceptor<VirtualMethodInterceptor>(), 
            new InterceptionBehavior<ExceptionInterceptionBehavior>() 
           }, 
           overwriteExistingMappings: true); 

    } 
} 

Mon InterceptionBehavior

public class ExceptionInterceptionBehavior : IInterceptionBehavior 
{ 
    public IEnumerable<Type> GetRequiredInterfaces() 
    { 
     return Type.EmptyTypes; 
    } 

    public bool WillExecute 
    { 
     get { return true; } 
    } 

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) 
    { 
     var result = getNext()(input, getNext); 
     return result; 
    } 
} 

Des idées pour lesquelles je reçois An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll chaque fois que j'appelle une méthode de mon service Web?

La trace

enter image description here

Si vous avez besoin de plus amples informations s'il vous plaît ne pas hésiter à demander.

Merci pour l'aide


MISE À JOUR

Si je supprime les intercepteurs, bien sûr, je n'ai pas exception plus:

container.RegisterTypes(AllClasses.FromAssemblies(lstAssemblyToLoad), 
           WithMappings.FromAllInterfaces, 
           WithName.Default, 
           WithLifetime.ContainerControlled, 
           null, 
           overwriteExistingMappings: true); 

Mais si je change juste WithName.Default à WithName.TypeName et garder les intercepteurs, je ne reçois pas d'exception ... Je ne comprends pas pourquoi ...

container.RegisterTypes(AllClasses.FromAssemblies(lstAssemblyToLoad), 
           WithMappings.FromAllInterfaces, 
           WithName.TypeName, 
           WithLifetime.ContainerControlled, 
           getInjectionMembers: c => new InjectionMember[] 
           { 
            new Interceptor<VirtualMethodInterceptor>(), 
            new InterceptionBehavior<ExceptionInterceptionBehavior>() 
           }, 
           overwriteExistingMappings: true); 
+0

pouvez-vous partager la trace de pile d'exception? – Dhawalk

+0

Mis à jour la réponse avec une capture d'écran de ce que je reçois –

+0

Je ne pense pas que ce soit la pile complète. Si vous descendez plus loin, est-ce que cela commence par votre code? – Dhawalk

Répondre

0

J'ai trouvé une solution en mettant à getName WithName.TypeName ou t => t.GetType().Name mais maintenant je suis face à un problème de cartographie :)

Cependant, je ne sais toujours pas pourquoi cela résout le StackOverflowException. Si quelqu'un le sait, je lui donnerai un cookie.

EDIT Avec mon collègue, nous avons examiné et trouvé cette solution Registration by convention and interception causes ResolutionFailedException

donc nous ajoutons un nouveau filtre Type

public static class TypeFiltres 
{ 
    public static IEnumerable<Type> WithMatchingInterface(this IEnumerable<Type> types) 
    { 
     return types.Where(type => 
      type.GetTypeInfo().GetInterface("I" + type.Name) != null); 
    } 
} 

et changé la configuration dans l'usine de WCF comme celui-ci

container.RegisterTypes(AllClasses.FromAssemblies(lstAssemblyToLoad).WithMatchingInterface(), 
           WithMappings.FromMatchingInterface, 
           WithName.Default, 
           WithLifetime.ContainerControlled, 
           getInjectionMembers: c => new InjectionMember[] 
           { 
            new Interceptor<InterfaceInterceptor>(), 
            new InterceptionBehavior<ExceptionInterceptionBehavior>() 
           });