0

I have .Net smart card and I am using c# to build the card application and the host application. Actually the smart card acts as a server in .Net remoting.

In smart card, I have remote object (Myclass) and I call remote method :

public int AddPerson (string name,string age)

to add some information to this object.

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]);

    }
}

person class:

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;




    }

To retrieve each element of person array, I firstly convert it to struct because there are some problem when I try to serialize the object. Anyway this is the method :

public PersonStruct getPesrson(int index)

The problem is raised in this method, although they are stored successfully in the smart card. I got ArgumentOutOfRange Exception with these details:

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
Charles
  • 50,943
  • 13
  • 104
  • 142
hum.
  • 25
  • 7
  • The code you provide has nothing to do with the exception. Please post some relevant code and indicate on which line execution fails. – John Willemse May 02 '13 at 11:46
  • @hum - what problems do have when serializing an object? Have you added the Serializable attribute? – Joe Ratzer May 02 '13 at 13:18
  • I state my problem here you can check it :http://stackoverflow.com/questions/15233119/net-smartcard-serialize-deserialize-the-remote-object-the-input-stream-is-no , I use smart card thats why i do not have full features of .NET – hum. May 02 '13 at 14:02
  • FYI, your PersonStruct block is missing a final } – NH. Jun 08 '17 at 19:24

1 Answers1

2

It sounds like you are using SubString with an index that doesn't exist in the string. You'd need to make sure your string has enough characters before using that index.

If this is a new project have a look at WCF instead of remoting, you might find it more suitable. Microsoft has published the following diagram:

WCF

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • I did not see the OP creating an ArrayList I saw it using an array of Person type. Also what is the problem with .NET Remoting? If the OP is looking for performance .NET Remoting is the way to go. If it was that bad microsoft would have replaced it with WCF for interprocess communication. You see WCF does not fit everything. – MeTitus May 02 '13 at 12:17
  • The code has been updated so I will update my answer. Previously, the code had a class called Arraylist. I certainly don't think WCF fits everything, far from it. However, I do think WCF replaces remoting. – Joe Ratzer May 02 '13 at 13:10
  • It surely does not for me, but everyone is entitled to have a different view. – MeTitus May 02 '13 at 13:15
  • That's what I understood from Microsoft. Do you think remoting is still useful because it is faster than WCF? Or do you have other reasons for using it? – Joe Ratzer May 02 '13 at 13:17
  • .net remoting is integrated deep in the CLR, I don't think it would be easy for them to remove it completely and like I said .net remoting is used by the clr itself for inter-process communication. I've used .net remoting extensively and I do understand that it can be complex for new comers, but once you master it, it becomes difficult to choose it over WCF for some scenarios, but then again WCF provides much more than interprocess communication, it also provides support for low coupling contracts(SOAP, REST...). – MeTitus May 02 '13 at 13:39
  • As for speed, I don't think there should be any doubts about that, .NET remoting is close to raw TCP/IP, and AFAIK, WCF is an abstract container for .net, asp.net webservices, which means another stack to deal with, but these are just my two cents. – MeTitus May 02 '13 at 13:40
  • As for raw performance I could not find anything stating that .net remoting is faster although I would believe that to be the case. – MeTitus May 02 '13 at 13:50
  • Microsoft won't "remove it", I just thought they were recommending using WCF. When you say WCF is "for asp.net webservices" that isn't quite right. You can use "close to the metal" TCP and named pipes with WCF. I've never heard remoting was quicker, but I'd be interested to know if it was faster. – Joe Ratzer May 02 '13 at 13:52
  • Ok I know this problem is caused by substring, but as the stack trace shows the method that throws this exception (HandleReturnMessage) is not accessible so how to check the length? – hum. May 02 '13 at 14:07
  • Maybe it was my a wrong assumption that.net remoting was faster, but I recall reading something about that, when WCF came out. – MeTitus May 02 '13 at 14:18
  • I come up with this solution, and i dont know if it will fix it. As the method that throws this exception is invoked while passing the message from the server (card) to my host application. In my case, the message is the person struct. The problem is cauesd by substring, so if I change the name and age attrs to be byte array. Do you think it may solve the problem? now i test it about 40 tims and the exception dose not appear yet. Btw, before this edition , the exception appears suddenly and in unexpected way. – hum. May 02 '13 at 14:18