Injecting WCF service proxy

Topics: Prism v4 - Silverlight 4
Sep 12, 2012 at 6:28 PM

I currently have a Silverlight application using Prism and MEF. I connect to several WCF services in this application but use a "controller" class to hold the instantiated client-generated service proxy objects.

What I want to do is inject the service references into this controller (like I currently do with the IEventAggregator). I'm unsure how to do this. Do I need to make a wrapper class that implements the service contract interface, and holds a reference to the service proxy object?

What I do now:

/// <summary>
/// WCF client used to communitcate with the data WCF service
/// </summary>
private DataClient _dataClient;  // DataClient is the client generated object from a service reference

/// <summary>
/// Region manager for the application
/// </summary>
private IRegionManager _manager;

/// <summary>
/// Application event aggregator
/// </summary>
private IEventAggregator _eventAggregator;

/// <summary>
/// Constructor. Initializes the statistics controller, hooks up all services, and initializes all commands.
/// </summary>
/// <param name="manager"></param>
[ImportingConstructor]
public ZOpportunityController(IRegionManager manager, IEventAggregator events)
{
    _manager = manager;
    _eventAggregator = events;

    //hookup a WCF service used to retrive GP data
    _dataClient = new ZellerGpDataClient();  
    _dataClient.OpenCompleted += new EventHandler<AsyncCompletedEventArgs>(_dataClient_openCompleted);
    _dataClient.GetCustomersCompleted += new EventHandler<GetCustomersCompletedEventArgs>(_dataClient_GetCustomersCompleted);
    _dataClient.OpenAsync();
}

As you can see here, I connect to a service in my controller constructor, but I'd like to just inject a new object into the controller like I do with IRegionManager and IEventAggregator, that is already wired for asynchronous communication to the service.

Can someone help me achieve this?

Developer
Sep 12, 2012 at 8:06 PM

Hi,

I believe you could find the following blog post from Guido Maliandi interesting:

There you could find a sample and information on how to wrap the service functionality in a shared service, which is registered in the container through a common interface mapping, defined in a common project. This way you could consume this service using this interface, for example in your controller class.

Also for another example of this approach (although this uses Unity as the container) you could check the "WCFServiceWrapperSample" also located in Guido Maliandi's skydrive account.

I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami

Sep 12, 2012 at 8:25 PM

Thank you so much...this is exactly what I was looking for.