WPF using PRISM and WCF - how to implement multiple views that connect to WCF services?

Topics: Prism v4 - WPF 4
Jul 17, 2012 at 4:39 PM

I am implementing a WPF client application in MVVM architecture using PRISM (I am new to PRISM).

The WPF application has 3 main regions :

  • Left Region
  • Right Region
  • Side Region

The left region has a view with a 'Fetch Button', that when pressed should connect to a 'FETCH' WCF service, fetch items, disconnect from the service and present the items in the view.

The right region has a view with no buttons. This view should connect every 10 minutes to the 'POLL' WCF service, fetch items, disconnect from the service and present the items in the view.

The side region has a view with no buttons. This view should connect to the 'NOTIFICATIONS' WCF service, and stay connected to it during the whole application life cycle. It should receive callbacks from the service, that should be presented in the Side view.

Here is an image showing what it should look like:

Questions :

  1. What should I pass on to the constructor of the 'FETCH MODULE' that includes the 'Fetch View' ? should it be the service itself or should it be some channel factory ? I think it should be some kind of channel factory, because the connection to the 'Fetch WCF Service' needs to be initiated every time the user pressed the 'fetch' button.

  2. Same question goes for the 'POLL MODULE'.

  3. Regarding the 'NOTIFICATIONS MODULE' - should I pass a Singleton instance of the 'Notification Service Client' ? (since the application needs to open only one client to the service, and it should stay connected all the time)

  4. In order to avoid the 'Notifications' client faulting because of a timeout, I need to implement some sort of 'heartbeat', that will call a stub method on the 'Notifications' WCFservice, so that it doesn't timeout. Where should I place the code for the heartbeat ? should it be in the 'NOTIFICATIONS MODULE' ? who should initiate it ? (I believe it should run in some sort of background thread)

  5. I am currently using MEF, and I don't really know what I am doing (I decorated the views with [Export] and the [ViewModel] setter with [Import] etc, but I still don't understand how I control who gets loaded and when). In addition - I don't understand how the Dependency Injection works in MEF. What are the differences between using 'MEF Bootstrapper' and 'Unity Bootstrapper' ?

  6. I have seen some people use MEF and just use [Import] and [Export] attributes, and some people that inherit their modules from 'IModule' and implement 'InitializeModule' with EventAggreagator, Container and RegionManager. What are the differences between these approaches, and when should you use each approach ?

  7. Do you recommend using the 'Add Service Reference...' or to manually create an 'IChannelFactory' ? (PROs \ CONs)

  8. Should the service clients be part of the Modules or part of the Infrastructure ?

  9. I have searched a lot on the internet for samples of WPF+PRISM+WCF and could not find any good samples I can use, so I can understand how to inject the services into my view-models. Can anyone offer any examples \ sample links ?

Following 4 questions are a tough cookie:

  1. All 3 WCF services I have mentioned use the same 'Username+Password' authentication method, which means I need to pop-up a 'Login' window when the application loads. What is the best practice for doing such a thing in PRISM and MVVM ? Should the 'Login' window replace the 'Shell' window ? (I don't think so, so what should it be ?)

  2. When the user enters the username+password - Should I try to connect to just the first service, and if it succeeds - disconnect from it and close the 'Login' view (meaning, I assume that the connection will succeed with the other services).

  3. Where could I store the username+password the user has entered, so that the application can keep on reconnecting to the services without requesting the user to provide the credentials each time ?

  4. What do you advise should be the approach I take if for some reason - one of the modules tries to connect to one of the WCF services, and doesn't succeed because of credentials ? Should I raise an event that forces all other modules to disconnect from their services, and then pop-up the 'Login' view ?

Sorry for the long post ...

Jul 18, 2012 at 6:44 PM


Take into account that, as far as I know, there is no official guidance regarding WCF Services in Prism. However, I will try to answer your questions within the scope of Prism the best I can:

  1. In my opinion, which of the two services (the “Fetch WCF Service” or the “channel factory” service) you should pass will depend mostly on your personal preferences and requirements. For example, if in your implementation you initiate the “Fetch WCF Service” each time you need to fetch items and you will be doing this through a channel factory, then it make sense to inject the channel factory in the module’s constructor.

  2. Same as above.

  3. In this case, it makes sense to pass a singleton instance of the “Notification Service Client.” However, I believe this too will depend mostly on your personal preferences and requirements. For example, you may want to make the code in each module “consistent” with the other ones (for example, passing always a “channel factory”) or you may want to implement some logic to reinitialize the service in case of failure, etc.

  4. As a possible approach, I believe that initializing a background thread in the “Notifications Module” to implement this “heart beat” could be reasonable since this is related to the notifications logic only and the module knows the service. However, take into account that Prism does not provide guidance about background threads either and that, once the initialization of a module is completed, the module instance is garbage collected unless a strong reference is created (hence, you will need to think how to reference and control this background thread in the module).

  5. The most important difference between them is that the MEFBootstrapper is prepared to be used with MEF and the UnityBootstrapper is prepared to be used with Unity (you cannot use the UnityBootstrapper with MEF and backwards.)

    Generally speaking and based on my understanding, when you ask MEF to resolve a class, it will check it in order to find any [Import] or [ImportingConstructor] attributes that need to be satisfied. To do this, MEF checks the exported classes in order to find the required ones and compose them. To compose those classes that are required by the first one, MEF check them in order to find any [Import] or [ImportingConstructors] attributes and so on. After resolving this “chain” of dependencies MEF returns the corresponding instance of the class.

    This is a very simplified explanation of how MEF inject dependencies. If you are interested in this topic, I believe you could find better support about it in the MEF site:

    Also, you can find more information about MEF and Unity in the following chapter of the Prism documentation:

  6. I am sorry but I don't understand to which "approaches" you are referring. In Prism, a module must always inherit from IModule and its Initialize method will always run.

  7. Again, Prism does not provide guidance about this topic and I believe this will depend mostly of your personal preferences and the requirements of your scenario.

  8. In my opinion, the implementation of the services should be part of the Modules, while the interfaces implemented by those services (which will be used to register them in the container) should be in the Infrastructure project. However, this will depend on how you want to distribute the logic of your application.

  9. I believe you could find the following sample by Guido Maliandi interesting, where WCF services are included in a Prism application. Although the application is developed in Silverlight, it can provide useful pointers on how to use WCF in Prism:

    Also, if you want a more complex "real life" scenario of WCF services in Prism, you can check the Microsoft Media Platform Video Editor which was built using Prism v2

  10. Also, I believe you could find the following approach for an authentication scenario (also by Guido Maliandi) containing a login screen useful too :

  11. How you should handle this will depend mostly on your personal preferences and requirements. Personally, I would check the connection to all services before accepting the user; or even better, authorize the user through other means (for example, and authorization service).

  12. As a possible approach, you could simply store the user and password in a shared service which could be accessed when needing to connect to the WCF services. For more information regarding shared services you could check the following chapter of the Prism documentation:

  13. What you mentioned seems to be a valid approach for this case. Also you could show an error message or handle each module separately (if a module could not connect to the service, only that module is affected and the others could keep working.) However, I believe this is more of a design decision and will depend on the requirements of your scenario.

Take into account that most of the questions are highly related to design decision which, in my opinion, does not have a correct “approach,” but depend mostly of the requirements on the application, and the personal choices.

I hope you find this useful,

Damian Cherubini

Jul 18, 2012 at 8:33 PM

Thanks so much.

Your help is much appreciated...

I have come accross so many problems trying to use 'async WCF clients' in my WPF PRISM app, that need to supply a username+password credentials.

It just seems like PRISM was not meant for such stuff...