0

Hi I working through exercises from TCP/IP Sockets in C# and experiencing a failure which I'm not sure how to approach. The example below is the client that uses a custom dispatcher. When I try to load the assembly that contains the implementation the exception below is throw.

namespace TcpEchoServerThreadFactory
{
    class TcpEchoServerThreadFactory
    {
        static void Main(string[] args)
        {
            if (args.Length != 3)
                throw new ArgumentException("Parameter(s): [optional properties]" + 
                    "<port> <protocol> <dispatcher>");

            int port = Int32.Parse(args[0]);
            string protocolName = args[1];
            string dispatcherName = args[2];

            TcpListener listener = new TcpListener(IPAddress.Any, port);
            listener.Start();
            ILogger logger = new ConsoleLogger();

            System.Runtime.Remoting.ObjectHandle objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", protocolName + "ProtocolFactory");
            IProtocolFactory protocolFactory = (IProtocolFactory)objHandle.Unwrap();
            objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", dispatcherName + "Dispatcher");
            IDispatcher dispatcher = (IDispatcher)objHandle.Unwrap();

            dispatcher.startDispatching(listener, logger, protocolFactory);
        }
    }
}

Unhandled Exception: System.TypeLoadException: Could not load type 'EchoProtocolFactory' from assembly 'TcpEchoThreadServerLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=
null'.
   at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Ob
ject[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName)
   at TcpEchoServerThreadFactory.TcpEchoServerThreadFactory.Main(String[] args) in C:\microsoft_press\DemoProjects\NonBlocking\TcpEchoServerThreadFactory\TcpEchoServerThreadFact
ory.cs:line 25
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • 2
    Does the TypeLoadException have an InnerException set? It's usually the case of a secondary exception thrown during the static constructor. – Avner Shahar-Kashtan Jun 19 '16 at 08:58
  • I dumped all the exceptions from the heap and only the TypeLoadException was there: Exception object: 02109900 Exception type: System.TypeLoadException Message: Could not load type 'EchoProtocolFactory' from assembly 'TcpEchoThreadServerLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. – dcrearer Jun 19 '16 at 09:01
  • Check out the suggestions [here](http://stackoverflow.com/questions/16086178/what-could-be-causing-a-system-typeloadexception?rq=1) – Avner Shahar-Kashtan Jun 19 '16 at 09:03
  • It simply is telling you that "TcpEchoThreadServerLib" is not a valid type name. Looks highly likely, it appears to be missing a namespace name. – Hans Passant Jun 19 '16 at 13:18
  • @HansPassant Thx… specifying the type with a the namespace resolved the failure. – dcrearer Jun 19 '16 at 14:53

1 Answers1

0

As recommended by Hans Passan I qualified the type with the namespace, now the code works as expected

  string ns = "TcpEchoThreadServerLibrary.";

            TcpListener listener = new TcpListener(IPAddress.Any, port);
            listener.Start();
            ILogger logger = new ConsoleLogger();

            System.Runtime.Remoting.ObjectHandle objHandle =
                Activator.CreateInstance("TcpEchoThreadServerLib", ns + protocolName + "ProtocolFactory");
            IProtocolFactory protocolFactory = (IProtocolFactory)objHandle.Unwrap();

            objHandle = Activator.CreateInstance("TcpEchoThreadServerLib", ns + dispatcherName + "Dispatcher");
            IDispatcher dispatcher = (IDispatcher)objHandle.Unwrap();

            dispatcher.startDispatching(listener, logger, protocolFactory);
dcrearer
  • 1,972
  • 4
  • 24
  • 48