Клиент-cервер на .NET Remoting - C#
Формулировка задачи:
Надо было создать не сложное клиент серверное приложение, решила сначала почитать, повторить примеры и встряла. Вот собственно пример (осторожно много букв):
Сделала отдельным приложением библиотеку RemoteHello, построила решение RemoteHello.dll готов.
Создала сервер, отдельное консольное приложение, подключила ссылки RemoteHello.dll и System.Runtime.Remote.
Построила решение.
Создала отдельным приложением клиент, подключила ссылки RemoteHello.dll и System.Runtime.Remote.
Запускаю сервер, запускаю клиент ошибка
Понимаю что секрет кроется где-то здесь, но чего именно он от меня хочет не понимаю.
Подскажите, пожалуйста, чего не так.
namespace Wrox.ProfessionalCSharp {
using System;
/// <summary>
/// Краткое описание Class1
/// </summary>
public class Hello: System.MarshalByRefObject {
public Hello() {
Console.WriteLine("Constructor called");
}
~Hello() {
Console.WriteLine("Destructor called");
}
public string Greeting(string name) {
Console.WriteLine("Greeting called");
return "Hello, " + name;
}
}
}using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox.ProfessionalCSharp {
/// <summary>
/// Краткое описание Class1
/// </summary>
public class HelloServer {
public static void Main(string[] args) {
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("hit to exit");
System.Console.ReadLine();
}
}
}using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox.ProfessionalCSharp {
/// <summary>
/// Краткое описание Class1.
/// </summary>
public class HelloClient {
public static void Main(string[] args) {
ChannelServices.RegisterChannel(new TcpClientChannel());
Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");
if (obj == null) {
Console.WriteLine("could not locate server"); return;
}
for (int i = 0; i < 5; i++) {
Console.WriteLine(obj.Greeting("Christian"));
}
}
}
}namespace Wrox
{
using System;
public class Hello
{
public Hello()
{
Console.WriteLine("Constructor called");
}
~Hello()
{
Console.WriteLine("Destructor called");
}
public string Greeting(string name)
{
Console.WriteLine("Greeting called");
return "Hello, " + name;
}
}
}using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox
{
public class HelloServer
{
public static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("hit to exit");
System.Console.ReadLine();
}
}
}using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox
{
public class HelloClient
{
public static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");
if (obj == null)
{
Console.WriteLine("could not locate server"); return;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(obj.Greeting("Christian"));
}
}
}
}Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");
Решение задачи: «Клиент-cервер на .NET Remoting»
textual
Листинг программы
namespace Client
{
public partial class Form1 : Form
{
private readonly Singleton _sington;
public readonly Cache _cache;
private readonly Singlecall _singlecall;
public Form1()
{
InitializeComponent();
RemotingConfiguration.Configure("Client.exe.config", false);
int idClient = Math.Abs(DateTime.Now.GetHashCode() / 100000);
Text = idClient.ToString();
try
{
_sington = new Singleton();
_singlecall = new Singlecall();
_cache = new Cache();
_cache.SetIdClient(idClient);
}
catch (WebException)
{
MessageBox.Show("Сервер недоступен");
return;
}
}