当前位置: 代码迷 >> Web Service >> remoting出错,大家看看是什么有关问题
  详细解决方案

remoting出错,大家看看是什么有关问题

热度:231   发布时间:2016-05-02 03:02:05.0
remoting出错,大家看看是什么问题?
远程对象:
public class SimpleMath : MarshalByRefObject ,ISimpleMath.ISimpleMath
  {
  public int Add(int n1, int n2)
  {
  Console.WriteLine("SimpleMath.Add() executing on thread {0}",Thread.CurrentThread.GetHashCode());
  Thread.Sleep(5000);
  return n1 + n2;
  }

  private delegate int BinaryOperatorDelegate(int n1, int n2);

  public void AsyncAdd(int n1, int n2, IClientCallback.IClientCallback callback)
  {
  Console.WriteLine("SimpleMath.AsyncAdd() executing on thread {0}",Thread.CurrentThread.GetHashCode());
  BinaryOperatorDelegate binOp = new BinaryOperatorDelegate(Add);
  binOp.BeginInvoke(n1, n2, new AsyncCallback(DoClientCallback), callback);
  }

  private void DoClientCallback(IAsyncResult ar)
  {
  Console.WriteLine("DoClientCallback executing on thread {0}",Thread.CurrentThread.GetHashCode());

  AsyncResult asyncResult = (AsyncResult)ar;
  BinaryOperatorDelegate binOp = (BinaryOperatorDelegate)asyncResult.AsyncDelegate; //取得异步委托
  int result = binOp.EndInvoke(ar); //取得运算结果

  IClientCallback.IClientCallback callback = (IClientCallback.IClientCallback)ar.AsyncState; //取得回调对象
  callback.ResultCallback(result); //调用接口方法
  }
  }


客户端:
public class ClientCallbackSink : MarshalByRefObject,IClientCallback.IClientCallback
  {
  public void ResultCallback(int result)
  {
  Thread.Sleep(1000);
  Console.WriteLine("ResultCallback executing on thread {0}",Thread.CurrentThread.GetHashCode());
  Console.WriteLine("Add result is {0}",result);

  Thread.Sleep(1000);
  }
  }
 class Program
  {
  static void Main(string[] args)
  {
  Console.WriteLine("Main executing on thread {0}",Thread.CurrentThread.GetHashCode());

  HttpChannel channel = new HttpChannel(0);
  ChannelServices.RegisterChannel(channel, false);

  ISimpleMath.ISimpleMath simpleMath = (ISimpleMath.ISimpleMath)Activator.GetObject(typeof(ISimpleMath.ISimpleMath), "http://localhost:8001/SimpleMath.soap");
  ClientCallbackSink callback = new ClientCallbackSink();
  simpleMath.AsyncAdd(5, 2, callback);

  Console.WriteLine("Press enter to end...");
  Console.ReadLine();
  }
  }

服务端:
class Program
  {
  static void Main(string[] args)
  {
  Console.WriteLine("Main executing on thread {0}", Thread.CurrentThread.GetHashCode());

  HttpServerChannel channel = new HttpServerChannel(8001);
  ChannelServices.RegisterChannel(channel, false);

  RemotingConfiguration.RegisterWellKnownServiceType(typeof(SimpleMath.SimpleMath), "SimpleMath.soap", WellKnownObjectMode.SingleCall);

  Console.WriteLine("Press enter to exit...");
  Console.ReadLine();
  }
  }


还有些接口代码没贴,主要是个异步操作的小例子,运行出错,大家帮忙看看是可能是啥问题?

------解决方案--------------------
错误提示信息?
------解决方案--------------------
  相关解决方案