Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Code cast InvalidCastException when it "Shouldn't"

Code cast InvalidCastException when it "Shouldn't"

Scheduled Pinned Locked Moved C#
csharpvisual-studiodebuggingsalesjson
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evilfish2000
    wrote on last edited by
    #1

    I am working with EFT solutions in the LSRetail LSOne POS, and one of my customers has an issue that I cannot figure out why happens. I have a class that has the following declaration:

    public class MyEFTService : EFTBase, IEFTService, IMyInterface
    {
    //Code omitted
    }

    The IMyInterface interface is one I have added so I can execute some added functionality to the class from another location. I do that like this:

    public IPosTransaction RunTask()
    {
    var service = Services.EFTService(entry);
    var dxcpcEft = (IMyInterface)service;

    //rest of code omitted
    

    }

    The Services.EFTService(entry) call returns the instance of MyEFTService created during startup, which I then cast to my IMyInterface and I can then do the calls I need to. This works on my machine, on my testers machine, and any co-workers machine. But it's here my customer has an issue. When we try to cast the instance to the IMyInterface interface on his machine we get an InvalidCastException. In debug, if I look at the service variable and browse through the object information, I can see variables that only my version of EFTBase has and I then know that it does have the IMyInterface as well. We also tried to execute service is IMyInterface in the immediate debug window of visual studio and it returns true. So, as far as I can see, the service variable DOES have the IMyInterface and visual studios Immediate debug window confirms it. We also tried to run service.GetType(), and that too returns information related to MyEFTService class:

    {Name = "EFTService" FullName = "LSOne.Services.EFT.EFTService"}
    Assembly: {LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
    AssemblyQualifiedName: "LSOne.Services.EFT.MyEFTService, LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    Attributes: Public | BeforeFieldInit
    BaseType: {Name = "EFTBase" FullName = "LSOne.Services.EFT.Common.EFTBase"}
    ContainsGenericParameters: false
    CustomAttributes: Count = 0
    DeclaredConstructors: {System.Reflection.ConstructorInfo[1]}
    DeclaredEvents: {System.Reflection.EventInfo[0]}
    DeclaredFields: {System.Reflection.FieldInfo[15]}
    DeclaredMembers: {System.Reflection.MemberInfo[75]}
    DeclaredMethods: {System.Reflection.MethodInfo[52]}
    DeclaredNestedTypes: {System.Reflection.TypeInfo.d__23}
    DeclaredProperties: {System.Reflection.PropertyInfo[3]}
    DeclaringM

    OriginalGriffO realJSOPR 2 Replies Last reply
    0
    • E Evilfish2000

      I am working with EFT solutions in the LSRetail LSOne POS, and one of my customers has an issue that I cannot figure out why happens. I have a class that has the following declaration:

      public class MyEFTService : EFTBase, IEFTService, IMyInterface
      {
      //Code omitted
      }

      The IMyInterface interface is one I have added so I can execute some added functionality to the class from another location. I do that like this:

      public IPosTransaction RunTask()
      {
      var service = Services.EFTService(entry);
      var dxcpcEft = (IMyInterface)service;

      //rest of code omitted
      

      }

      The Services.EFTService(entry) call returns the instance of MyEFTService created during startup, which I then cast to my IMyInterface and I can then do the calls I need to. This works on my machine, on my testers machine, and any co-workers machine. But it's here my customer has an issue. When we try to cast the instance to the IMyInterface interface on his machine we get an InvalidCastException. In debug, if I look at the service variable and browse through the object information, I can see variables that only my version of EFTBase has and I then know that it does have the IMyInterface as well. We also tried to execute service is IMyInterface in the immediate debug window of visual studio and it returns true. So, as far as I can see, the service variable DOES have the IMyInterface and visual studios Immediate debug window confirms it. We also tried to run service.GetType(), and that too returns information related to MyEFTService class:

      {Name = "EFTService" FullName = "LSOne.Services.EFT.EFTService"}
      Assembly: {LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
      AssemblyQualifiedName: "LSOne.Services.EFT.MyEFTService, LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      Attributes: Public | BeforeFieldInit
      BaseType: {Name = "EFTBase" FullName = "LSOne.Services.EFT.Common.EFTBase"}
      ContainsGenericParameters: false
      CustomAttributes: Count = 0
      DeclaredConstructors: {System.Reflection.ConstructorInfo[1]}
      DeclaredEvents: {System.Reflection.EventInfo[0]}
      DeclaredFields: {System.Reflection.FieldInfo[15]}
      DeclaredMembers: {System.Reflection.MemberInfo[75]}
      DeclaredMethods: {System.Reflection.MethodInfo[52]}
      DeclaredNestedTypes: {System.Reflection.TypeInfo.d__23}
      DeclaredProperties: {System.Reflection.PropertyInfo[3]}
      DeclaringM

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I'd start by checking if the customer has all the right dependencies - if the interface or class is declared in a separate assembly (and it probably should be) then a version mismatch could cause the problem.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      E 1 Reply Last reply
      0
      • E Evilfish2000

        I am working with EFT solutions in the LSRetail LSOne POS, and one of my customers has an issue that I cannot figure out why happens. I have a class that has the following declaration:

        public class MyEFTService : EFTBase, IEFTService, IMyInterface
        {
        //Code omitted
        }

        The IMyInterface interface is one I have added so I can execute some added functionality to the class from another location. I do that like this:

        public IPosTransaction RunTask()
        {
        var service = Services.EFTService(entry);
        var dxcpcEft = (IMyInterface)service;

        //rest of code omitted
        

        }

        The Services.EFTService(entry) call returns the instance of MyEFTService created during startup, which I then cast to my IMyInterface and I can then do the calls I need to. This works on my machine, on my testers machine, and any co-workers machine. But it's here my customer has an issue. When we try to cast the instance to the IMyInterface interface on his machine we get an InvalidCastException. In debug, if I look at the service variable and browse through the object information, I can see variables that only my version of EFTBase has and I then know that it does have the IMyInterface as well. We also tried to execute service is IMyInterface in the immediate debug window of visual studio and it returns true. So, as far as I can see, the service variable DOES have the IMyInterface and visual studios Immediate debug window confirms it. We also tried to run service.GetType(), and that too returns information related to MyEFTService class:

        {Name = "EFTService" FullName = "LSOne.Services.EFT.EFTService"}
        Assembly: {LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
        AssemblyQualifiedName: "LSOne.Services.EFT.MyEFTService, LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        Attributes: Public | BeforeFieldInit
        BaseType: {Name = "EFTBase" FullName = "LSOne.Services.EFT.Common.EFTBase"}
        ContainsGenericParameters: false
        CustomAttributes: Count = 0
        DeclaredConstructors: {System.Reflection.ConstructorInfo[1]}
        DeclaredEvents: {System.Reflection.EventInfo[0]}
        DeclaredFields: {System.Reflection.FieldInfo[15]}
        DeclaredMembers: {System.Reflection.MemberInfo[75]}
        DeclaredMethods: {System.Reflection.MethodInfo[52]}
        DeclaredNestedTypes: {System.Reflection.TypeInfo.d__23}
        DeclaredProperties: {System.Reflection.PropertyInfo[3]}
        DeclaringM

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3
        1. Ensure than their machine is up to date on their Windows updates. 1) Have the customer uninstall it (and all older versions), and reinstall it.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          I'd start by checking if the customer has all the right dependencies - if the interface or class is declared in a separate assembly (and it probably should be) then a version mismatch could cause the problem.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          E Offline
          E Offline
          Evilfish2000
          wrote on last edited by
          #4

          I thought I did confirm the versions, but it does look like there is a version mismatch with at least some files. I will update when I have more information.

          OriginalGriffO L 2 Replies Last reply
          0
          • E Evilfish2000

            I thought I did confirm the versions, but it does look like there is a version mismatch with at least some files. I will update when I have more information.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            :thumbsup:

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            E 1 Reply Last reply
            0
            • E Evilfish2000

              I thought I did confirm the versions, but it does look like there is a version mismatch with at least some files. I will update when I have more information.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Easy way to prevent it in the future; make a hash from all that you build. Do not confirm the versions, confirm the resulting hash.

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                :thumbsup:

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                E Offline
                E Offline
                Evilfish2000
                wrote on last edited by
                #7

                Upon further research, it became clear that the versions of the POS system did not match the expected version. We transferred the source code over to the customer so he could build the files with his version and to great care in deleting all other versions of the affected DLL. We still get the same results. I also found the version on my machine that they actually got from me and ran a test on my machine, and it works without problems. So now to rule out a problem on the customer's end, he is currently sending me the entire software catalogue with my file changes to see if it crashes on my machine. If it fails, it's something in his project. If it does not fail... then I actually don't know. I will keep you posted.

                OriginalGriffO 1 Reply Last reply
                0
                • E Evilfish2000

                  Upon further research, it became clear that the versions of the POS system did not match the expected version. We transferred the source code over to the customer so he could build the files with his version and to great care in deleting all other versions of the affected DLL. We still get the same results. I also found the version on my machine that they actually got from me and ran a test on my machine, and it works without problems. So now to rule out a problem on the customer's end, he is currently sending me the entire software catalogue with my file changes to see if it crashes on my machine. If it fails, it's something in his project. If it does not fail... then I actually don't know. I will keep you posted.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Sound like you're going to need a site visit with a copy of VS ... I hate those. I once spent a week sitting quietly in a library waiting for software to go wrong, only to find on the Friday it was a hardware problem: they had a laminator they used to seal new user badges which spat so much noise back down the mains lead it fritzed out the PSU. Still, it was on expenses, and the hotel bar was good!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups