elastic communication是基於c#開發支持.net和mono的通信組件(簡稱EC),EC的主要目的簡化mono和.net下的通信開發難度,經過EC能夠很是快速地開發基於mono和.net的通信交互應用。EC抽取的基礎的通信協議默認支持protobuf,msgpack的數據對象進行通信交互,開發者不能夠根據本身的制要制訂更多的序列化方式支持;EC不牢牢支持簡單的對像傳輸,還提供了控制器,方法控制器和更方便的遠程接口調用功能。 git
如下是ES但願實現的最終目標 github
藉助於Xamarin實現不一樣移動端的實現,因爲這一塊我的工做緣由還沒細化實現,經過開源相關代碼可讓感興趣的人更好進一步去完成相關功能。 c#
EC實現通信功能是很是方便的事情,下面通信簡單的幾種場景來介紹一下EC在通信上的應用。 session
HelloWord app
namespace HelloWord.Server { [Controller] public class Program { static void Main(string[] args) { ECServer.Open(); System.Threading.Thread.Sleep(-1); } public string HelloWord(ISession session,Hello e) { return string.Format("hello {0} [say time:{1}]", e.Name, DateTime.Now); } } [MessageID(0x1)] [ProtoContract] public class Hello { [ProtoMember(1)] public string Name { get; set; } } }
private EC.ProtoSyncClient mClient = new ProtoSyncClient("127.0.0.1"); mClient.Send<string>(new Hello { Name=textBox1.Text })
遠程方法訪問 asp.net
EC支持遠程方法調用,若是用過wcf那對這功能感受應該不會陌生,而EC也是經過接口的方式來定義遠程調用行爲;wcf同一方法重載須要從新定義名稱,而ec則支持同一方法多個重載版本。爲了知足更復雜的須要,EC的遠程調用一樣支持out和ref參數。 ide
namespace Remoting.Service { public interface IUserService { User Register(string name, string email); } }
[SOAService(typeof(Service.IUserService))] class Program : IUserService { static void Main(string[] args) { ECServer.Open(); System.Threading.Thread.Sleep(-1); } public Service.User Register(string name, string email) { User user = new User(); user.EMail = email; user.Name = name; user.CreateTime = DateTime.Now; return user; } }
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ProtoClient mClient = new ProtoClient("192.168.7.111"); private IUserService UserService; private void Form1_Load(object sender, EventArgs e) { UserService = mClient.CreateInstance<IUserService>(); } private void cmdRegister_Click(object sender, EventArgs e) { User user= UserService.Register(txtName.Text, txtEMail.Text); txtCreateTime.Text = user.CreateTime.ToString(); } }
數據訪問示例 ui
數據訪問應該是最多見的一種應用場,如下是定義一個簡單的數據查詢示例。 spa
[EC.Controller] public class Program { static void Main(string[] args) { DBContext.SetConnectionDriver<SqliteDriver>(DB.DB1); DBContext.SetConnectionString(DB.DB1, "Data Source=northwindEF.db;Pooling=true;FailIfMissing=false;"); ECServer.Open(); System.Threading.Thread.Sleep(-1); } public IList<Employee> OnEmployeeSearch(ISession session, EmployeeSearch e) { return new Expression().List<Models.Employees, Employee>(); } public IList<Customer> OnCustomerSearch(ISession session, CustomerSearch e) { return new Expression().List<Models.Customers, Customer>(); } public IList<Order> OnOrderSearch(ISession session, OrderSearch e) { Expression exp = new Expression(); if (e.CustomerID != null) exp &= Models.Orders.customerID == e.CustomerID; if (e.EmployeeID > 0) exp &= Models.Orders.employeeID == e.EmployeeID; return exp.List<Models.Orders, Order>(); } public IList<OrderDetail> GetOrderDetail(ISession session, GetDetail e) { Expression exp = Models.OrderDetails.orderID == e.OrderID; JoinTable jt = Models.OrderDetails.productID.InnerJoin(Models.Products.productID); jt.Select("OrderDetails.*", Models.Products.productName.Name); return exp.List<OrderDetail>(jt); } }
private ProtoSyncClient mClient = new ProtoSyncClient("127.0.0.1"); private void FrmMain_Load(object sender, EventArgs e) { cbEmployees.Items.Add(new Employee()); foreach (Employee item in mClient.Send<IList<Employee>>(new EmployeeSearch())) { cbEmployees.Items.Add(item); } cbCustomers.Items.Add(new Customer()); foreach (Customer item in mClient.Send<IList<Customer>>(new CustomerSearch())) { cbCustomers.Items.Add(item); } } private void cmdSearch_Click(object sender, EventArgs e) { OrderSearch os = new OrderSearch(); if (cbCustomers.SelectedItem != null) os.CustomerID = ((Customer)cbCustomers.SelectedItem).CustomerID; if (cbEmployees.SelectedItem != null) os.EmployeeID = ((Employee)cbEmployees.SelectedItem).EmployeeID; gdOrder.DataSource = mClient.Send<IList<Order>>(os); } private void gdOrder_SelectionChanged(object sender, EventArgs e) { if (gdOrder.SelectedRows.Count > 0) { Order order = (Order)gdOrder.SelectedRows[0].DataBoundItem; GetDetail getdetail = new GetDetail(); getdetail.OrderID = order.OrderID; gdDetail.DataSource = mClient.Send<IList<OrderDetail>>(getdetail); } }
AppModule .net
AppModule相似於asp.net的httpModule,它能夠在EC服務中載實始化的時候進行加載,經過AppModule能夠實現消息處理,處理日誌,全局信息定義等相關主要功能。
public class FilterModel : IAppModel { public string Name { get { return "Filter"; } } private System.Threading.Timer mTimer; public void Init(IApplication application) { //application.Filters.Add(new LoginFilter()); application.Disconnected += (o, e) => { "{0} disposed applicaion event".Log4Info(e.Session.Channel.EndPoint); }; application.Connected += (o, e) => { "{0} connect applicaion event".Log4Info(e.ChannelConnectArgs.Channel.EndPoint); }; application.SendCompleted += (o, e) => { "{0} send completed applicaion event".Log4Info(e.Session.Channel.EndPoint); }; application.MethodProcess += (o, e) => { //application e.Application["Path"] = @"c:\"; //sexxion e.Session["folder"] = "aaa"; }; application.Error += (o, e) => { "{0} channel error {1}".Log4Error(e.Info.Channel.EndPoint, e.Info.Error.Message); }; mTimer = new System.Threading.Timer(o => { application.Server.Send(new User { Name = Guid.NewGuid().ToString("N"),CreateTime = DateTime.Now }, application.Server.GetOnlines()); }, null, 1000, 1000); } public string Command(string cmd) { throw new NotImplementedException(); } }
這個特性在EC上是提供比較有用的功能,通信filter能夠對調用方法加入權限,日誌,攔載等邏輯功能。
public class AdminFilter : FilterAttribute { public override void Execute(IMethodContext context) { "admin filter ->{0}".Log4Debug(context.Handler); base.Execute(context); } } [Controller] public class Controller { [SkipFilter(typeof(LoginFilter))] [ThreadPool] public User Regisetr(ISession session, User user) { user.CreateTime = DateTime.Now; "Register invoke[Name:{0} Email:{1}]".Log4Debug(user.Name, user.EMail); return user; } [AdminFilter] public IList<User> Search(ISession session, Query query) { "Search invoke".Log4Debug(); List<User> users = new List<User>(); users.Add(new User()); users.Add(new User()); return users; } }
https://github.com/IKende/ec/
https://github.com/IKende/ec/tree/master/Samples