×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

I will show my code below:

本文发表在 rolia.net 枫下论坛The remoting object has one function:

private int m_nCount;
public string GetCount()
{
System.Threading.Thread.Sleep(500);
m_nCount++;
return m_nCount.ToString();
}

The Server is a sample remoting server and it should be fine.
The problem is in the client:

1. I have a client application like this
:
HttpChannel chan = new HttpChannel();
ChannelServices.RegisterChannel(chan);

RemotingConfiguration.RegisterWellKnownClientType(
typeof(Remoting.RemotingObject),
"http://localhost:8080/RemoteFunction");

RemotingObject obj = new RemotingObject();
Console.WriteLine(obj.GetCount());//run 10 times...
Console.WriteLine(obj.GetCount());
...

ChannelServices.UnregisterChannel(chan);

If I run two client application at almost same time, the result is like this:
instance 1: 1 2 4 6 8 10 ...
instance 2: 3 5 7 9 ...

both instance can run as expected, and they finished in almost same time ( 5s).

2. I put the remoting client into a aspx page, using almost exact same code:
RemotingConfiguration.RegisterWellKnownClientType(
typeof(Remoting.RemotingObject),
"http://localhost:8080/RemoteFunction");
obj = new RemotingObject();
this.Label1.Text=obj.GetCount(); //run 10 times.
this.Label1.Text=obj.GetCount();
...

The page is working, but if I access this page twice in almost same time, the first request will work, but the second request will return a "500 Internal Server Error".更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 这里有对 .Net Remoting 比较了解的吗?我刚开始用,有个问题想请教一下。
    • 什么问题?会不会先看看
      • I will show my code below:
        本文发表在 rolia.net 枫下论坛The remoting object has one function:

        private int m_nCount;
        public string GetCount()
        {
        System.Threading.Thread.Sleep(500);
        m_nCount++;
        return m_nCount.ToString();
        }

        The Server is a sample remoting server and it should be fine.
        The problem is in the client:

        1. I have a client application like this
        :
        HttpChannel chan = new HttpChannel();
        ChannelServices.RegisterChannel(chan);

        RemotingConfiguration.RegisterWellKnownClientType(
        typeof(Remoting.RemotingObject),
        "http://localhost:8080/RemoteFunction");

        RemotingObject obj = new RemotingObject();
        Console.WriteLine(obj.GetCount());//run 10 times...
        Console.WriteLine(obj.GetCount());
        ...

        ChannelServices.UnregisterChannel(chan);

        If I run two client application at almost same time, the result is like this:
        instance 1: 1 2 4 6 8 10 ...
        instance 2: 3 5 7 9 ...

        both instance can run as expected, and they finished in almost same time ( 5s).

        2. I put the remoting client into a aspx page, using almost exact same code:
        RemotingConfiguration.RegisterWellKnownClientType(
        typeof(Remoting.RemotingObject),
        "http://localhost:8080/RemoteFunction");
        obj = new RemotingObject();
        this.Label1.Text=obj.GetCount(); //run 10 times.
        this.Label1.Text=obj.GetCount();
        ...

        The page is working, but if I access this page twice in almost same time, the first request will work, but the second request will return a "500 Internal Server Error".更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • 我试着猜想一下:在Romoting中连续两次注册端口会抛出异常,你说的情况(2次快速访问页面)等于连续2次执行Client端的代码,这样...
          第一次的代码因为Sleeping的原因还没有执行UnregisterChannel()函数,第二次点击又要执行一次RegisterChannel()函数,这样会抛出异常.

          我一般是设置一个判断,如果注册的channel数目==0则注册端口,否则直接执行剩下的remoting语句,如下:

          if (ChannelServices.RegisteredChannels.Length == 0)
          {
          HttpChannel hChannel = new HttpChannel();
          ChannelServices.RegisterChannel(hChannel, true);
          }

          可能是这个原因,你试试,这是我的第一反应,我也没根据你的情况作试验
          • 多谢多谢。真的是这个问题。不过不知道为什么application 中就没问题。我现在还是照猫画虎,对Remoting 里的细节都不了解。
            • 你说的第一种情况,每一个单独的进程remoting会单独的分配一个channel,虽然你的2个程序完全一样,但它们是2个完全独立的进程,同时运行windows会分配不同的channel号给这2个进程。所以不会抛出异常
              但是ASP.NET不一样,它实际上是一个进程,你刷新2次页面实际上是在2次执行同一个程序里的函数,这样remoting看到channel已经被注册过一次就会抛出异常

              还有你的Server端应该用的是Singleton模式吧,我看到2个程序同时运行打印出来的数字前后关联变化。这种模式下无论有几个client连接Server,在Server端实际上只有一个线程在运行,返回的变量也是同一个变量,也就会造成不同的Client看到的不是相同的结果
              • 对,是Singleton,我本来是在测Singleton和singlecall的区别。server肯定只有一个Remoting Object, 不过说到single thread,我觉得还有疑问。
                运行一个client application, 耗时5秒。
                几乎同时运行3个client application, 3个几乎同时结束,各耗时6秒。如果是单线程,应该是15秒左右阿。应该是3个线程访问共同的数据。
          • 正确的做法是什么呢?是不是干脆就不UnregisterChannel?反正以后还要用。
          • 还有一个问题,ChannelServices.RegisteredChannels.Length>0恐怕不能断定我的channel已经registed吧,比如其它application创建其它channel... 看来有必要写个函数来查channel名。
          • 找到原因,http://msdn2.microsoft.com/en-us/library/ms223155.aspx, channel 是基于app level的。多谢了。
            • No Problem... ^_^