Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > Пример клиент серверного приложения


Автор: Cold_Almaty 15.2.2008, 15:06
Народ, доброе время суток.
Пожалуйста, приведите пример простого клиент серверного приложения, чтобы в качестве серверной части выступал сервис. Приложение должно запускать, скажем, блокнот.  smile

Добавлено через 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);
        }
    }
}


Нифига не работает. Пожалуйста поправьте. smile 

Автор: Cold_Almaty 19.2.2008, 11:24
Всё. Разобрался.
Правильный поиск рулит (mr. DUDA прав).

http://forum.vingrad.ru/forum/topic-176989/unread-1/hl/%25D0%25B7%25D0%25B0%25D0%25BF%25D1%2583%25D1%2581%25D0%25BA+%25D0%25BF%25D1%2580%25D0%25B8%25D0%25BB%25D0%25BE%25D0%25B6%25D0%25B5%25D0%25BD%25D0%25B8%25D1%258F/index.html


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)