Remote Call of many platform interfaces for EC applications

Source: Internet
Author: User

In traditional network application programming, messages are received for processing and then responded, but in. net 3.0 and later provided a network interaction system called based on business interfaces. based on WCF.. Net program can call the service remotely through the interface. since WCF is a system provided by MS, other platforms supported by mono and xamarin are not well supported. however, with the rapid development of Mono and xamarin, the Service System Based on the Service Interface Remote Call should provide more platforms with convenient communication; to solve these problems, the EC component provides a set of Remote Call methods based on service interfaces. with mono and xamarin, some functions can be simply applied to Linux, Android, WP, and IOS.

The EC can describe a remote communication operation only by Using Interfaces. Its original system principle is similar to that of WCF, because it is designed for the functions supported by mono. therefore, it provides support for multiple platforms. the following describes how to use EC to construct multiple remote interface calling functions.

Interface Definition

No specification is required for describing service interfaces in EC. However, because protobuf is used to implement basic RPC communication, some user-defined objects must be described through protobuf. the following is the definition of the sample interface and object structure.

    public interface IUserService   {        User Register(string name, string email);        void GetRegisteTime(string name, out DateTime time);    }    [ProtoContract]    [EC.MessageID(0x0001)]    public class User    {        [ProtoMember(1)]        public string Name { get; set; }        [ProtoMember(2)]        public string Email { get; set; }        [ProtoMember(3)]        public DateTime CreateTime { get; set; }    }

The above defines a simple user service interface, including the functions to register and obtain the user registration time. EC does not closely provide support for common parameter members, but also provides support for out parameters.

Service implementation

As mentioned in the previous article, if EC enables a network service, it can be enabled without any configuration, including the existing remote interface calling function. you only need to add a soaserver to the class that implements the remote interface and then load and run it automatically when the EC starts. the Code is as follows:

    [EC.Remoting.SOAService(typeof(IUserService))]   class Program:IUserService    {        static void Main(string[] args)        {            EC.ECServer.Open();            System.Threading.Thread.Sleep(-1);        }       public User Register(string name, string email)        {            Console.WriteLine("register name:{0}\t email:{1}", name, email);            User user = new User();            user.Name = name;            user.Email = email;            user.CreateTime = DateTime.Now;            return user;        }       public  void GetRegisteTime(string name, out DateTime time)        {                      time = DateTime.Now;            Console.WriteLine("get registe time:{0}", time);        }    }

The above code is not very simple, just implement the interface, then the service will naturally load and process.

Client

The EC also provides a simple function library to process remote interface calls. You only need to reference the interface and custom parameter types. you do not need to call the interface. the component constructs a proxy Based on the interface to simplify the complexity of client calls.

        public partial class Form1 : Form   {        public Form1()        {            InitializeComponent();        }        private ProtoClient mChannel = new ProtoClient("127.0.0.1", 10034);        private IUserService UserService;        private void Form1_Load(object sender, EventArgs e)        {            UserService = mChannel.CreateInstance<IUserService>();        }        private void btnRegister_Click(object sender, EventArgs e)        {          btnRegister.Text="Register(+" + UserService.Register(txtName.Text,txtEMail.Text).CreateTime+")";        }        private void button2_Click(object sender, EventArgs e)        {            DateTime dt;            UserService.GetRegisteTime("test",out dt);            button2.Text="GetTime(" + dt+")";        }    }

Directly construct a protoclient client object, and then create a proxy operation interface through the client object. The object does not require the validity of the relational connection, the interface proxy determines whether to create a connection during the call based on the connection conditions.

Android

EC also provides the xamarin function library, but it does not. the. NET platform provides protobuf and msgpack support in the same way. Therefore, there are some differences in usage. Because protobuf only supports protobuf, an object such as servicechannel is provided in xamarin to code the original protoclient, although the names are different, the functions are the same.

 [Activity (Label = "EC.InterfaceProxy", MainLauncher = true, Icon = "@drawable/icon")]public class MainActivity : Activity{private EC.ServiceChannel mChannel = new ServiceChannel("10.0.2.2",10034);private IUserService UserService;protected override void OnCreate (Bundle bundle){base.OnCreate (bundle);ServiceChannel.Register (typeof(MainActivity).Assembly);UserService = mChannel.CreateInstance<IUserService> ();// Set our view from the "main" layout resourceSetContentView (Resource.Layout.Main);EditText name = this.FindViewById<EditText> (Resource.Id.txtName);EditText email = this.FindViewById<EditText> (Resource.Id.txtEMail);Button register = this.FindViewById<Button> (Resource.Id.cmdRegister);register.Click += delegate {register.Text ="Register(" + UserService.Register(name.Text,email.Text).CreateTime+")";};Button gettime = this.FindViewById<Button> (Resource.Id.cmdGetTime);gettime.Click += delegate {DateTime dt;UserService.GetRegisteTime ("test", out dt);gettime.Text = "GETTIME(" + dt + ")";};// Get our button from the layout resource,// and attach an event to it}}

The overall call is the same, but one thing to note is that servicechannel needs to be called on mobile platforms. the register function registers user-defined message types to components. It mainly ensures that protobuf can solve user-defined types during RPC processing.


Summary

Because EC is implemented based on the standard fuse library and is also compatible with mono, the communication functions written through EC can run under Windows and Linux. if you have a mobile platform, xamarin can be easily applied to Android, IOS, and WP mobile systems.

Download complete example


Remote Call of many platform interfaces for EC applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.