Words on my addictions: .NET, Philosophy, Music

19 March 2007

.NET Remoting binary config

Filed under: C# — stefanprodan @ 10:05 pm

Remoting service class:

public class Service : MarshalByRefObject, IClient
{
public long Ping()
{
return DateTime.Now.ToBinary();
}

public override object InitializeLifetimeService()
{
return null;
}
}

IClient Interface:

public interface IClient
{
long Ping();
}

Server config:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<system.runtime.remoting>
<application name=”AlephServer” >
<service>
<wellknown mode=”Singleton” type=”AlephServerCore.Service, AlephServerCore” objectUri=”AlephServer” />
</service>
<channels>
<channel name=”Primary” ref=”http” port=”8080″>
<serverProviders>
<formatter ref=”binary” typeFilterLevel=”Full” />
</serverProviders>
</channel>
<channel name=”Secundary” ref=”http” port=”8090″>
<serverProviders>
<formatter ref=”binary” typeFilterLevel=”Full” />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

Server code:

System.Runtime.Remoting.RemotingConfiguration.Configure(“AlephServer.exe.config”, false);

Client config:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<system.runtime.remoting>
<application name = “AlephClient”>
<client>
<wellknown type=”AlephServerCore.IClient, AlephServerCore” url=”http://localhost:8080/AlephServer”/&gt;
</client>
<channels>
<channel ref=”http” port=”0″>
<clientProviders>
<formatter ref=”binary” />
</clientProviders>
<serverProviders>
<formatter ref=”binary” typeFilterLevel=”Full” />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

Client code:

RemotingConfiguration.Configure(“AlephClient.exe.config”, false);
WellKnownClientTypeEntry[] entrys = RemotingConfiguration.GetRegisteredWellKnownClientTypes();
Type clientType = typeof(IClient);
IClient proxy = (IClient)Activator.GetObject(clientType, entrys[0].ObjectUrl);
Console.WriteLine(proxy.Ping());

PS: There are here 3 projects : AlephServerCore.dll, AlephServer.exe and AlephClient.exe, all you need to do to make calls from the client to the server is to add in the IClient interface methods and implement them in Service class. Because the typeFilterLevel is set to Full it will allow you to send your own objects(classes or structs) to the server as method parameters. The binary serialization will make communication faster compared to web services soap serialization, and there is no limitation for the types used. The Service class is hosted on the server as a singleton so be wise and use ReadWriteLocks in your code.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: