Question

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 the service object into the controller like I do with IRegionManager and IEventAggregator.

EDIT: This blog post was really what I was trying to accomplish.

Was it helpful?

Solution

Derive a client from ClientBase<TChannel> and make it implement your service contract. The implementation of the contract look similar to this:

public class MyClient : ClientBase<IMyService>, IMyService
{
  void IMyService.DoSomething(Foo bar)
  {
    this.Channel.DoSomething(bar);
  }
}

Now you can just register that implementation with MEF or any other container and inject it via the constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top