Народ, доброе время суток. Пожалуйста, приведите пример простого клиент серверного приложения, чтобы в качестве серверной части выступал сервис. Приложение должно запускать, скажем, блокнот. Добавлено через 11 минут и 51 секундуСделал три блока: клиент, сервер, dll. клиент: | Код | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels.Http; using RemoteRegObject;
namespace RemoteRunAppClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnStart_Click(object sender, EventArgs e) { HttpChannel c = new HttpChannel(); ChannelServices.RegisterChannel(c);
object remoteObject = Activator.GetObject(typeof(RemoteRegObject.RemoteRegObj), "http://localhost:32469/RemoteAppStart.soap");
RemoteRegObj simple = (RemoteRegObj)remoteObject; simple.runApp(); }
private void btnClose_Click(object sender, EventArgs e) { this.Close(); }
} }
|
сервер: | Код | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp ; using System.Runtime.Remoting.Channels.Http; using RemoteRegObject;
namespace RemoteRunAppServ { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnClose_Click(object sender, EventArgs e) { this.Close(); }
private void Form1_Load(object sender, EventArgs e) { HttpChannel c = new HttpChannel(32469); ChannelServices.RegisterChannel(c);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteRegObject.RemoteRegObj), "RemoteAppStart.soap", WellKnownObjectMode.SingleCall); } } }
|
DLL: | Код | using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Data; using System.IO; using Microsoft.VisualBasic;
namespace RemoteRegObject { public class RemoteRegObj:MarshalByRefObject { public RemoteRegObj() { } public void runApp() { Process notepad = new Process(); notepad.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe"; notepad.Start(); } } }
|
Вот так всё работает. Но мне надо, чтобы сервер был реализован в виде сервиса. Код сервиса: | Код | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Configuration.Install; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels.Http; using RemoteRegObject;
namespace RemoteRunAppService { [RunInstallerAttribute(true)] public class MyProjectInstaller : Installer { private ServiceInstaller serviceInstaller1; private ServiceProcessInstaller processInstaller;
public MyProjectInstaller() { // Instantiate installers for process and services. processInstaller = new ServiceProcessInstaller(); serviceInstaller1 = new ServiceInstaller();
// The services run under the system account. processInstaller.Account = ServiceAccount.LocalSystem;
// The services are started manually. serviceInstaller1.StartType = ServiceStartMode.Manual;
// ServiceName must equal those on ServiceBase derived classes. serviceInstaller1.ServiceName = "RegApp";
// Add installers to collection. Order is not important. Installers.Add(serviceInstaller1); Installers.Add(processInstaller); } }
public partial class SimpleObjServer : ServiceBase { public SimpleObjServer() { InitializeComponent(); } public HttpChannel c;
protected override void OnStart(string[] args) { // TODO: Add code here to start your service. c = new HttpChannel(32469); ChannelServices.RegisterChannel(c);
RemotingConfiguration.RegisterWellKnownServiceType( typeof(RemoteRegObject.RemoteRegObj), "RemoteAppStart.soap", WellKnownObjectMode.Singleton ); }
protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. ChannelServices.UnregisterChannel(c); } } }
|
Нифига не работает. Пожалуйста поправьте.
|