72054

How do I pass a service to another plugin?

Question:

I have a plugin that I will instantiate at runtime and I want to pass it a WCF service from the application host. The application host is responsible for creating the connection to the service. The reason for this is that a single service can be used by multiple plugins, but the plugins should only know about its interface since there may be several implementation of IMyPluginServices. For instance, the Run method of the plugin instance would be:

public void Run(IMyPluginServices services) { services.DoSomething(); }

The problem I am running into is that I don't know how to create a service of type IMyPluginServices and pass it to the Run function. The service reference generated by VS 2010 doesn't seem to create an object of type IMyPluginServices that I can pass to it. Any help would be greatly appreciated. Thanks.

Answer1:

When you add a service reference in VS 2010 for a service it generates an interface named IMyService which contains methods for each OperationContract in your service. It also generates a concrete class named MyServiceClient, which can be constructed and then used to invoke your service.

Now, the problem that you're running into, I believe, is that MyServiceClient is a subclass of ClientBase<IMyService>, and does not implement the generated IMyService interface (which is a real pain).

To get around this problem I ended up making a new interface:

public interface IMyServiceClient : IMyService, IDisposable, ICommunicationObject { }

(Note: IDisposable and ICommunicationObject are only required if you want your module to be able to detect/react to faulted channels and other such things).

I then extend MyServiceClient with a partial class (in the assembly that contains my WCF Service reference):

public partial class MyServiceClient : IMyServiceClient { }

Now in my modules I can accept an IMyServiceClient instead of an IMyService, and still execute all of the methods that I need to. The application in control of the modules can still create instances of MyServiceClient as it always did.

The beauty of this is that your new interface and partial class don't need any actual code - the definitions suffice to get the job done.

Recommend

  • C# foreach - Is collection computed with each iteration? [duplicate]
  • What is the JSF behaviour, if you bind the same backing bean property to two input fields in the sam
  • Call task's updateProgress
  • Request response issues in biztalk
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Clear fused location provider's location for testing
  • Cast between interfaces whose interface signatures are same
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Join two tables and save into third-sql
  • Trying to switch camera back to front but getting exception
  • How to model a transition system with SPIN
  • ActionScript 2 vs ActionScript 3 performance
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • Suggestions to manage Login/Logout transitions
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?