- Create Remote Object
- Create Server
- Create Client
- Create Class Library project
- Create a class MyProxy
[sourcecode language="csharp" wrapline="false"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Security.Permissions;
using System.Data;
using System.Data.SqlClient;
namespace ProxyObject
{
public class MyProxy: MarshalByRefObject
{
ArrayList Re_list = new ArrayList();
/// <summary>
/// Ham khoi tao
/// </summary>
public MyProxy(){}
public bool MakeRequest(string UserName)
{
this.Re_list.Add(UserName + " - " + DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
return true;
}
public string GetInfor()
{
string re = "Hello: ";
for (int i = 0; i < this.Re_list.Count; i++)
{
re += "\n"+this.Re_list[i];
}
return re;
}
public DataSet GetEmployees()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection("Server=.\\SqlExpress;database=Northwind;Integrated Security=SSPI;");
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Employees", conn);
adp.Fill(ds, "Employee");
return ds;
}
[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
public string Shutdown()
{
Process.Start("shutdown", "/l");
return "OK";
}
}
}
[/sourcecode]
- Combine project
- Create new Window Application Project
- Add References library project
- Add new form and design as
[sourcecode language="csharp" wrapline="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using ProxyObject;
using System.Runtime.Remoting.Channels.Http;
namespace Server
{
public partial class FrmServer : Form
{
HttpChannel httpChanel;
public FrmServer()
{
InitializeComponent();
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
}
private void btnStart_Click(object sender, EventArgs e)
{
StartServer();
}
private void StopServer()
{
if (ChannelServices.GetChannel("http") != null)
ChannelServices.UnregisterChannel(httpChanel);
lstLog.Items.Add("Server stopped !");
}
private void StartServer()
{
StopServer();
int port = Convert.ToInt32(txtPort.Text);
WellKnownObjectMode mode = chkCall.Checked ? WellKnownObjectMode.SingleCall : WellKnownObjectMode.Singleton;
httpChanel = new HttpChannel(port);
ChannelServices.RegisterChannel(httpChanel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyProxy), "MyServices", mode);
lstLog.Items.Add("Server started !");
}
private void btnStop_Click(object sender, EventArgs e)
{
StopServer();
}
private void button1_Click(object sender, EventArgs e)
{
ProxyObject.MyProxy obj = new MyProxy();
obj.Shutdown();
}
}
}
[/sourcecode]
- Create new Window Application Project
- Add References library project
- Add new form and design as
[sourcecode language="csharp"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using ProxyObject;
using System.Security.Principal;
namespace Client
{
public partial class Form1 : Form
{
MyProxy obj;
public Form1()
{
InitializeComponent();
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
}
private void btnConnect_Click(object sender, EventArgs e)
{
string url = "http://" + txtServer.Text + ":" + txtPort.Text + "/MyServices";
RemotingConfiguration.RegisterWellKnownClientType(typeof(MyProxy), url);
btnGet.Enabled = true;
button1.Enabled = true;
obj = new MyProxy();
btnConnect.Enabled = false;
}
private void btnGet_Click(object sender, EventArgs e)
{
obj.MakeRequest(textBox1.Text);
rTContent.Text = obj.GetInfor();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
obj = new MyProxy();
dataGridView1.DataSource = obj.GetEmployees().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
[/sourcecode]
Test application
Chúc bạn thành công.
No comments:
Post a Comment