首要工作是要把Glue4net安装下windows service中,下载http://www.ikende.com/files/file/Glue4net.rar 解压后运行install.bat 这样Glue4net就安装到windows server中.这个时候你要在服务中启动Glue4net,之后windows启动就会自动加载.
Glue4net可以挂DLL和代码文件,如果功能比较简单那直接写个.cs或.vb文件即可,下面就通过c#写一个简单的Socket Tcp服务.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; namespace Glue4Net.SocketServer { public class Server:Glue4Net.IAppModule { private Socket mListenSocket; private IList<Socket> mClients = new List<Socket>(); private bool IsAccepting = false; private void BeginReceive(Socket e) { SocketAsyncEventArgs saea = new SocketAsyncEventArgs(); saea.Completed += OnReceiveCompleted; saea.SetBuffer(new byte[1024], 0, 1024); saea.UserToken = e; if (!e.ReceiveAsync(saea)) { OnReceiveCompleted(this, saea); } } private void SendData(Socket e, byte[] data, int offset, int count) { e.Send(data, offset, count, SocketFlags.None); } private void OnReceiveCompleted(object sender, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success && e.BytesTransferred > 0) { SendData((Socket)e.UserToken, e.Buffer, 0, e.BytesTransferred); BeginReceive((Socket)e.UserToken); } else { mClients.Remove((Socket)e.UserToken); Log.Error("{0} disposed!", e.RemoteEndPoint); } } private void OnAccept(object state) { while (IsAccepting) { try { Socket socket = mListenSocket.Accept(); Log.Info("{0} connected!", socket.RemoteEndPoint); mClients.Add(socket); BeginReceive(socket); } catch (Exception e_) { Log.Info("Socket Server Accept error {0}",e_.Message); } } } public void Load() { try { mListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mListenSocket.Bind(new IPEndPoint(IPAddress.Any, 10024)); mListenSocket.Listen(10); IsAccepting = true; System.Threading.ThreadPool.QueueUserWorkItem(OnAccept); Log.Info("Socket Server Listen @{0}", 10024); Log.Info("Load Socket Server Success!"); } catch (Exception e_) { Log.Error("Load Socket Server Error {0}", e_.Message); } } public IEventLog Log { get; set; } public string Name { get { return "Socket Server"; } } private void DisposedSocket(Socket e) { try { if (e != null) { mListenSocket.Close(); mListenSocket.Shutdown(SocketShutdown.Both); } } catch { } } public void UnLoad() { try { IsAccepting = false; DisposedSocket(mListenSocket); foreach (Socket client in mClients) { DisposedSocket(client); } Log.Info("Unload Socket Server Success!"); } catch (Exception e_) { Log.Error("Unload Socket Server Error {0}", e_.Message); } } } }
继承Glue4Net.IAppModule是容器加载运行的规则,需要在Load方法中实现启动运行的工作,然后在UnLoad方法里实现服务关闭的工作.功能完成后把代码保存到任何目录下,这里是保存到H:\SocketServer;以上是一个非常简单的Socket TCP服务,把接收的信息回应给发送端.
为了保证Glue4Net能加载,必须保让代码目录下存在Glue4Net.Dll这个文件,因为它是模块加载的入口点.把程序加载到Glue4Net是非常简单的事情,只需要在domains.config中添加一个配置即可.
<add name="socketserver" compiler="true" path="H:\SocketServer\" updateWatch="true" watchFilter="*.cs|*.config"/>
添加一个socketserver的应用名,然后目录指向之前存放代码的目录,确认配置信息无误后保存.当文件保后无须对Glue4Net进行重启服务即可加载完成.针对以上编写的代码部署完成后,你只需要telent 127.0.0.1 10024 即可连接到相应的服务中.
通过Glue4Net非常简单就能把程序添加到windows service中运行,而这个部署是基于热加载.当添加或修改某些应用的时候,是完全不会影响其他应用的正常工作.
https://github.com/IKende/IKendeLib/tree/master/Glue4Net