Wrapping the Channel

As you've seen with the default .NET Remoting channels, you don't have to manually create and register HTTPClientChannel and HTTPServerChannel but can instead use the combination in the form of HTTPChannel. This isn't strictly needed for compatibility with the .NET Remoting Framework, but it does provide more comfort for the developers using this channel. An additional feature you might want to implement is the default assignment of a formatter to this channel. I'm now going to show you how to do this to create an SMTPChannel class.

First, the combined channel has to extend BaseChannelWithProperties and implement IChannelSender and IChannelReceiver. Nevertheless, there won't be too much logic in SMTPChannel, as it will delegate most of its work to either SMTPClientChannel or SMTPServerChannel.

To check if an application wants to act as a server for .NET Remoting requests via SMTP, you have to introduce another attribute that can be used in the configuration file: isServer. When this is set to "yes", the SMTPChannel will create an SMTPServerChannel as well; otherwise it will only create an SMTPClientChannel.

The SMTPChannel has to implement a different constructor that allows the framework to pass both an IClientChannelSinkProvider and an IServerChannelSinkProvider object to it. It will then check whether either of these is null and create a default SOAP formatter in this case.

All other methods that have to be implemented to support the specified interfaces will just forward their calls to the respective client or server channel. This is shown in Listing 10-7.

Listing 10-7: The SMTPChannel

start example
 using System; using System.Collections; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System.Runtime.Remoting.Messaging; namespace SMTPChannel {    public class SMTPChannel: BaseChannelWithProperties,       IChannelSender, IChannelReceiver    {       SMTPClientChannel _clientchannel;       SMTPServerChannel _serverchannel;       String _name;    public SMTPChannel (IDictionary properties,       IClientChannelSinkProvider clientSinkProvider,       IServerChannelSinkProvider serverSinkProvider)    {      if (clientSinkProvider == null)    {      clientSinkProvider = new SoapClientFormatterSinkProvider();    }    // create the client channel    _clientchannel = new SMTPClientChannel(properties, clientSinkProvider);    if ((properties["isServer"] != null) &&       ((String) properties["isServer"] == "yes" ))    {       if (serverSinkProvider == null)       {          serverSinkProvider = new SoapServerFormatterSinkProvider();       }       // create the server channel       _serverchannel = new SMTPServerChannel( properties,              serverSinkProvider);    }    _name = (String) properties["name"]; } public IMessageSink CreateMessageSink(string url,    object remoteChannelData, out string objectURI) {    return _clientchannel.CreateMessageSink(url,       remoteChannelData, out objectURI); } public string Parse(string url, out string objectURI) {    return _clientchannel.Parse(url, out objectURI); } public string ChannelName {    get    {       return _name;    } } public int ChannelPriority {    get    {       return 0;    } } public void StartListening(object data) {    if (_serverchannel != null)    {       _serverchannel.StartListening(data);    } } public void StopListening(object data) {    if (_serverchannel != null)    {       _serverchannel.StopListening(data);    } } public string[] GetUrlsForUri(string objectURI) {    if (_serverchannel != null)    {       return _serverchannel.GetUrlsForUri(objectURI);    }    else    {       return null;    } } public object ChannelData {    get    {       if (_serverchannel != null )       {          return _serverchannel.ChannelData;       }       else       {          return null;       }     }   }  } } 
end example

Advanced  .NET Remoting C# Edition
Advanced .NET Remoting (C# Edition)
ISBN: 1590590252
EAN: 2147483647
Year: 2002
Pages: 91
Authors: Ingo Rammer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net