.NET远程处理异常ArgumentOutOfRange

时间:2013-05-02 11:41:12

标签: c# exception remoting

我有.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; }
    }}

PersonStruct:

  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

1 个答案:

答案 0 :(得分:2)

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

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

WCF

相关问题