title: Error serializing C# string using protobuf-net
body: <p>I would like to write a C# client to send a string via protobuf-net to a TCP server (implemented).  However, when I try to serialize the string using protobuf-net, I'm getting a <code>TypeInitializerException</code> "The type initializer for 'Singleton' threw an exception."</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static void TelnetConnect(string host, int port) {
      ...
      Message msg = new Message("This is a test.");
      byte[] sentbytes = msg.Serialize();
      ...
  }
  
  [ProtoContract]
  public abstract class MessageI {
      public byte[] Serialize() {
          byte[] result;
          using (var stream = new MemoryStream()) {
              Serializer.Serialize(stream, this);  //THIS LINE THROWS EXCEPTION
              result = stream.ToArray();
          }
          return result;
      }
  }
  
  [ProtoContract]
  public class Message : MessageI {
      [ProtoMember(1)]
      public string str { get; set; }
  
      public Message(string s) {
          this.str = s;
      }
  }
  </code></pre>
  
  <p>I've tried a number of approaches advised by this site and others, without success.  I'm using C# on Visual Studio 2010.</p>
  
  <p>Thanks, I very much appreciate your help.</p>
  
  <p>UPDATE: The stack trace is:</p>
  
  <pre><code>   at ProtoBuf.Meta.RuntimeTypeModel.get_Default()
     at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)
     at PingNorbertServer.MessageI.Serialize() in C:\Users\RS88517\Documents\Visual Studio 2010\Projects\PingNorbertServer\PingNorbertServer\NorbertClient.cs:line 37
     at PingNorbertServer.NorbertClient.TelnetConnect(String host, Int32 port) in C:\Users\RS88517\Documents\Visual Studio 2010\Projects\PingNorbertServer\PingNorbertServer\NorbertClient.cs:line 23
     at PingNorbertServer.NorbertClient.Main(String[] args) in C:\Users\RS88517\Documents\Visual Studio 2010\Projects\PingNorbertServer\PingNorbertServer\NorbertClient.cs:line 14
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>