【问题标题】:.NET remoting exception ArgumentOutOfRange.NET 远程处理异常 ArgumentOutOfRange
【发布时间】:2013-05-02 11:41:11
【问题描述】:

我有 .Net 智能卡,我正在使用 c# 构建卡应用程序和主机应用程序。 实际上,智能卡在 .Net 远程处理中充当服务器。

在智能卡中,我有远程对象(Myclass)并调用远程方法:

public int AddPerson (string name,string age)

为这个对象添加一些信息。

public class Myclass : MarshalByRefObject
{


private string Text1;
private string Text2;
private int NoOfperson = 0 ;
private person[] List = new person [6];


    public void setText1(string t)
    { Text1= t; }
    public void setText2(string t)
    { Text2= t }        

    public string getText1 ()
    { return Text1; }
    public string getText1 ()
    { return Text1; }

    public int AddPerson (string name,string age)
    {
        person OBJ = new person ();
        OBJ.Name =  name;
        OBJ.Age =  age;
       List[NoOfperson] = OBJ;
        NoOfperson ++;
        return NoOfperson - 1 ; //Index of current person           
        return 1 ; 
    }
      public PersonStruct getPesrson(int index)
    {
        return PersonStruct.convertToStruct(List[index]);

    }
}

人物类:

public class person
{
    private string name;
    private string age;

    public string Age
    {
        get { return age; }
        set { age = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }}

人员结构:

  public struct PersonStruct
    {

    public string name;
    public string age;

    public static PersonStruct convertToStruct(person OBJ)
    {
        PersonStruct tmp = new PersonStruct ();            
        tmp.name = OBJ.Name;
        tmp.age = OBJ.Age;
        return tmp;




    }

为了检索 person 数组的每个元素,我首先将其转换为 struct,因为在尝试序列化对象时出现了一些问题。无论如何,这是方法:

public PersonStruct getPesrson(int index)

虽然它们已成功存储在智能卡中,但此方法会引发问题。我得到了带有这些详细信息的 ArgumentOutOfRange 异常:

Message
Index and length must refer to a location within the string.


TargetSite:
 Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage
, System.Runtime.Remoting.Messaging.IMessage)

ParamName:
length

Source:
mscorlib

StackTrace:
Server stack trace:
   at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length,
Boolean fAlwaysCopy)
   at System.String.Substring(Int32 startIndex, Int32 length)
   at SmartCard.Runtime.Remoting.a.A(Type , String& , IMessage )
   at SmartCard.Runtime.Remoting.a.a(Type , String& , IMessage )
   at SmartCard.Runtime.Remoting.a.a(Type , String& , IMessage )
   at SmartCard.Runtime.Remoting.a.A(Type , Byte[] , IMessage )
   at SmartCard.Runtime.Remoting.a.A(Type , Stream , IMessage )
   at SmartCard.Runtime.Remoting.Channels.APDU.APDUClientFormatterSink.SyncProce
ssMessage(IMessage msg)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
   at medicalrecordApp.medicalrecord.getExamination(Int32 index)
   at MyCompany.MyClientApp.MyClient.Main() in C:\Documents and Settings\User\My
 Documents\Visual Studio 2008\Projects\Client2\Client2\MyClient.cs:line 36

【问题讨论】:

  • 您提供的代码与异常无关。请发布一些相关代码并指出哪一行执行失败。
  • 我更新了我的问题
  • @hum - 序列化对象时有什么问题?是否添加了 Serializable 属性?
  • 我在这里陈述我的问题,你可以检查一下:stackoverflow.com/questions/15233119/…,我使用智能卡,所以我没有 .NET 的全部功能
  • 仅供参考,您的 PersonStruct 块缺少最后一个 }

标签: c# exception remoting


【解决方案1】:

听起来您正在使用 SubString 和字符串中不存在的索引。在使用该索引之前,您需要确保您的字符串有足够的字符。

如果这是一个新项目,请查看WCF 而不是远程处理,您可能会发现它更合适。微软发布了下图:

【讨论】:

  • 我没有看到 OP 创建 ArrayList 我看到它使用 Person 类型的数组。 .NET Remoting 还有什么问题?如果 OP 正在寻找性能 .NET Remoting 是要走的路。如果它是那么糟糕,微软会用 WCF 代替它来进行进程间通信。您会看到 WCF 并不适合所有情况。
  • 代码已经更新,所以我会更新我的答案。以前,代码有一个名为 Arraylist 的类。我当然不认为 WCF 适合一切,远非如此。但是,我确实认为 WCF 取代了远程处理。
  • 对我来说肯定不行,但每个人都有权有不同的看法。
  • 这是我从微软那里了解到的。您是否认为远程处理仍然有用,因为它比 WCF 更快?还是您有其他使用它的原因?
  • .net 远程处理集成在 CLR 的深处,我认为完全删除它并不容易,就像我说的 .net 远程处理被 clr 本身用于进程间沟通。我已经广泛使用 .net 远程处理,并且我知道它对于新手来说可能很复杂,但是一旦你掌握了它,在某些情况下很难选择它而不是 WCF,但是 WCF 提供的不仅仅是进程间通信,它还提供对低耦合合约(SOAP、REST...)的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
相关资源
最近更新 更多