读书人

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

发布时间: 2012-01-07 21:41:56 作者: rapoo

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();
}
}


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

[解决办法]
错误提示信息?
------解决方案--------------------


把服务端注册信道的代码改为:

C# code
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();            serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;            IDictionary props = new Hashtable();            props["port"] = 8001;            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);            ChannelServices.RegisterChannel(channel, false);
[解决办法]
呵呵,我知道,你这个例子,是服务器和客户端互相调用,和那个广播的例子(remoting和delgate综合)类似
你的问题是由于安全过高所致的,.NET Framework1.0,和1.1为了安全提高了远程运用delgate的安全。 可以在代码和配置文件中修改,但是当初我怎么修改的我忘记了。你可以查找一下。

读书人网 >Web Service

热点推荐