C# : Make communication between assemblies using run-time interop.

Example of making communication between assemblies using run-time interop and information.

I have written the application which contains lots of assemblies and I can do communicate between these assemblies using run time remoting very easily. It just required to have client and server XML file which specifies the protocols. I dont know why you dont want to use it but any way if you dont use remoting then there is no straight method to make the commumication between assemplies as the context will be totally different. What you need to do is use Process class and run the assembly A and pass the function or something similar to call the particular function with the arguments specified.

The main point is the name of config file should be in this format: "yourappName.exe.cfg"

Client side XML configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <client>
                <wellknown
                    type="Give your common dll and assemblie names"
                    url="ipc://example/myviewer.rem"
                />
            </client>
            <channels>
                <channel
                    type="System.Runtime.Remoting.Channels.Ipc.IpcClientChannel, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                    portName="myport"
                    secure="True"
                    useDefaultCredentials="True">

                    <serverProviders>
                        <formatter ref="binary" />
                        <provider
                            type="System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        />
                    </serverProviders>
                </channel>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

Server side XML configuration file:

<?xml version="1.0" encoding="utf-8"?>
<configuration> 
  <system.runtime.remoting>
    <application>    
      <service>
        <wellknown mode="SingleCall" type="Common asseblies" objectUri="myviewer.rem" />
      </service>
      <channels>
        <channel type="System.Runtime.Remoting.Channels.Ipc.IpcServerChannel, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" portName="sudowin" secure="True" tokenImpersonationLevel="Impersonation" authorizedGroup="Everyone">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full" />
            <provider type="System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          </serverProviders>
        </channel>
        <channel type="System.Runtime.Remoting.Channels.Ipc.IpcClientChannel, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" portName="sudowin" secure="True" useDefaultCredentials="True">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full" />
            <provider type="System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          </serverProviders>
        </channel>
      </channels>
    </application>
  </system.runtime.remoting> 
</configuration>

To start communication you will need to configure remoting:

               // get path to the actual exe
                Uri uri = new Uri(
                    Assembly.GetExecutingAssembly().GetName().CodeBase);
                                               
                // configure remoting channels and objects
                //Reads the configuration file and configures the remoting infrastructure.
                //Configure(String) is obsolete. Please use Configure(String,Boolean) instead.
                //Second argument is "true" to ensuring the security
                //Needed to set the client and server chanels and ports
                //               
                RemotingConfiguration.Configure(uri.LocalPath + ".config", true);               
                //
                // get the server object that is used to elevate
                // privleges and act as a backend store for
                // caching credentials
                // get an array of the registered well known client urls
                //Well-known object types can be either single call or singleton.
                //If an object type is single call, then a new instance of it is created each time a call from the
                //client comes in. All calls to a singleton object are handled by one instance of that object.
                //

                WellKnownClientTypeEntry[] wkts =
                    RemotingConfiguration.GetRegisteredWellKnownClientTypes();

Now instantiate the object using Activator and then you can start communication between assemblies.

        obj = Activator.GetObject(typeof("common namespace"),
                        wkts[x].ObjectUrl) as "common namespace";

        Now you can call another assembly function like obj.anotherFunction() and also pass the arguments.

Enjoy!!!!


By Perry    Popularity  (1915 Views)