LocationInterceptionAspect : custom attribute for automatic resolving with Autofac ?

xavieraverbouch's Avatar


25 Feb, 2018 12:53 PM

I'm currently setting up a new MVVM WPF project and I want to use autofac for my viewmodels injection and I would like to create a Postsharp custom attribute to automatically resolve dependencies when this custom attribute is used, like in this example here : autofac postsharp

this example doesn't work on MVVM WPF because there is no Dependency Resolver like this example (MVC)

How can I achieve the same for MVVM with WPF ?

If I knew how to do this, I could also just use postsharp with a global composition container, or a global service locator, but I couldn't figure out for now how to make it work
My services inherit from Postsharp IService
then I put Export Attribute of type Iservice on AuthenticationViewModel constructor
I have a IAuthenticationService derivating from IService and I've created a [ServiceAspect]

 AspectServiceInjector.Initialize(new TypeCatalog(typeof(IAuthenticationService)));
            AuthenticationViewModel viewModel = new AuthenticationViewModel(???????????);

like in http://doc.postsharp.net/global-service-container

"??????" how to pass my IService here ? in the documentation there is thsis note:
User code has no control over the time when and the thread on which an aspect is initialized. Therefore, using ThreadStaticAttribute to make the container local to the current thread is not a reliable approach.

in my authentificationviewmodel class :

    public class AuthenticationViewModel : IViewModel, INotifyPropertyChanged

and also my custom service derived from IService

public interface IAuthenticationService: IService

then I've tried : AspectServiceLocator with public static container called with :

     AspectServiceLocator.Initialize(new TypeCatalog(typeof(IAuthenticationService)));
            AuthenticationViewModel viewModel = new AuthenticationViewModel(AspectServiceLocator.GetService<IAuthenticationService>().Value);

but I get exception :

  Message=Aucune exportation correspondant à la contrainte 
    ContractName    Formula.UI.Data.IAuthenticationService
    RequiredTypeIdentity    Formula.UI.Data.IAuthenticationService n'a été trouvée.
  Arborescence des appels de procédure :
   à System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
   à System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality)
   à System.ComponentModel.Composition.Hosting.ExportProvider.GetExportCore[T](String contractName)
   à System.ComponentModel.Composition.Hosting.ExportProvider.GetExport[T](String contractName)
   à System.ComponentModel.Composition.Hosting.ExportProvider.GetExport[T]()
   à Formula.UI.Data.AspectServiceLocator.GetServiceImpl[T]() dans C:\work\Chemlynx\FormulaNET\Formula.WPF\Data\AutofacResolveAttribute.cs :ligne 40
   à System.Lazy`1.CreateValue()
  1. 1 Posted by xavieraverbouch on 25 Feb, 2018 03:53 PM

    xavieraverbouch's Avatar

    resolved: I used PostSharp.Patterns.Utilities.ServiceLocator.GetService and it worked directly

     AuthenticationViewModel viewModel = new AuthenticationViewModel(ServiceLocator.GetService<IAuthenticationService>());
  2. Support Staff 2 Posted by PostSharp Techn... on 26 Feb, 2018 09:26 AM

    PostSharp Technologies's Avatar


    thank you for the update. We are going to close this request as we believe it was solved.

    Our system hasn't found any active maintenance subscription. If that is correct and you'd need further support for our product, you can either renew your subscription
    at https://www.postsharp.net/purchase/upgrades-and-renewals or use our non-commercial support via https://stackoverflow.com/ by adding a PostSharp tag to your question.

    If you do have an active maintenance subscription, please send use the number which is in the beginning of you license key, divided by a dash from the rest of the key, so we can figure out why we don't see it.

    For details, please visit https://www.postsharp.net/support/policies.

    Best regards,

  3. PostSharp Technologies closed this discussion on 26 Feb, 2018 09:26 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac