139 lines
4.2 KiB
C#
139 lines
4.2 KiB
C#
|
using System;
|
||
|
using System.Configuration;
|
||
|
using System.Runtime.Remoting;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Runtime.Remoting.Channels;
|
||
|
using System.Runtime.Remoting.Channels.Tcp;
|
||
|
|
||
|
namespace Ease.SvcHost
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Summary description for RemotingHost.
|
||
|
/// </summary>
|
||
|
public class RemotingHost
|
||
|
{
|
||
|
private HostConfig _config;
|
||
|
private List<IChannelReceiver> _channels = new List<IChannelReceiver>();
|
||
|
|
||
|
private string _status = "";
|
||
|
public string Status
|
||
|
{
|
||
|
get { return _status; }
|
||
|
}
|
||
|
|
||
|
private void Log(string format, params object[] args)
|
||
|
{
|
||
|
if (_status.Length > 0)
|
||
|
_status += Environment.NewLine;
|
||
|
|
||
|
_status += string.Format(format, args);
|
||
|
}
|
||
|
|
||
|
public RemotingHost()
|
||
|
{
|
||
|
_config = HostConfig.GetConfig();
|
||
|
if (_config == null)
|
||
|
throw new ConfigurationErrorsException("ServiceHost configuration missing.");
|
||
|
}
|
||
|
|
||
|
private void InvokeMethod(HostConfig.MethodNode node)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Log("Invoking method: " + node.Method.DeclaringType.FullName + "." + node.Method.Name + "()");
|
||
|
node.Method.Invoke(null, null);
|
||
|
Log("Method Executed.");
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Log("Error: {0}", e.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Start()
|
||
|
{
|
||
|
_status = "";
|
||
|
|
||
|
if (_config.Port != 0)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
TcpServerChannel channel = new TcpServerChannel("RemotingListener", _config.Port);
|
||
|
ChannelServices.RegisterChannel(channel, false);
|
||
|
Log("Tcp channel registered on port {0}.", _config.Port);
|
||
|
Log("Custome Errors: {0}", _config.ErrorModes);
|
||
|
Log("");
|
||
|
|
||
|
_channels.Add(channel);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Log("Error Registering Channel: {0}.", e.Message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Turn off custom errors (comment if required)
|
||
|
RemotingConfiguration.CustomErrorsMode = _config.ErrorModes;
|
||
|
|
||
|
//container assemblies
|
||
|
foreach (HostConfig.ContainersNode containers in _config.Containers)
|
||
|
{
|
||
|
Log("Loading Containers Assembly: {0}", containers.Assembly.FullName);
|
||
|
|
||
|
// initializers
|
||
|
foreach (HostConfig.MethodNode node in containers.Initializers)
|
||
|
{
|
||
|
InvokeMethod(node);
|
||
|
}
|
||
|
|
||
|
Log("Activation Mode: {0}", containers.ActivationMode);
|
||
|
Log("");
|
||
|
Log("Start-Assembly >>");
|
||
|
|
||
|
foreach (Type type in containers.Types)
|
||
|
{
|
||
|
WellKnownServiceTypeEntry entry = new WellKnownServiceTypeEntry(type, type.FullName, containers.ActivationMode);
|
||
|
RemotingConfiguration.RegisterWellKnownServiceType(type, type.FullName, containers.ActivationMode);
|
||
|
Log("Service -> {0}", type.FullName);
|
||
|
}
|
||
|
|
||
|
Log("<< End-Assembly");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Stop()
|
||
|
{
|
||
|
_status = "";
|
||
|
|
||
|
if (_channels.Count > 0)
|
||
|
{
|
||
|
Log("Stopping channels ...");
|
||
|
foreach (IChannelReceiver channel in _channels)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
channel.StopListening(null);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Log("Error stopping channel: {0}. {1}", channel.ChannelName, e.Message);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
foreach (HostConfig.ContainersNode assembly in _config.Containers)
|
||
|
{
|
||
|
if (assembly.Finalizers.Count > 0)
|
||
|
{
|
||
|
Log("{0} Finalizers ...", assembly.Assembly.GetName().Name);
|
||
|
foreach (HostConfig.MethodNode node in assembly.Finalizers)
|
||
|
{
|
||
|
InvokeMethod(node);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Log("Host Stopped.");
|
||
|
}
|
||
|
}
|
||
|
}
|