0

I have the error above

"Value cannot be null.\r\nParameter name: typeName"

this is the stack trace:

>     "   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly,
> StackCrawlMark& stackMark)\r\n   at System.Type.GetType(String
> typeName, Boolean throwOnError)\r\n   at
> x.QueueHelper.GetBody[T](BrokeredMessage brokeredMessage)\r\n   at
> x.QueueHelper.<>c.<ReceiveMessage>b__3_0(BrokeredMessage message)"

The code I have is as follows

I send a message like this without exceptions(this is controller action)

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Nombre,NIT,NombreRepresentanteLegal,TelefonoRepresentanteLegal,NombreContacto,TelefonoContacto,PropiedadesExtra")] Empresa empresa)
        {
            if (ModelState.IsValid)
            {
                var propiedadesList = from formvalues in Request.Form.ToDictionary()
                                      join propiedades in unitOfWork.PropiedadRepository.Get() on formvalues.Key equals propiedades.Nombre
                                      where propiedades.Entidad.Nombre == "Empresa"
                                      select new { Clave = formvalues.Key, Valor = formvalues.Value };
                XElement el = new XElement("root",propiedadesList.Select(kv => new XElement(kv.Clave, kv.Valor)));
                empresa.PropiedadesExtra = el.ToString();

                QueueHelper.SendMessage("Empresa", empresa, queueConnectionString);
                //unitOfWork.EmpresaRepository.Insert(empresa);
                //unitOfWork.Save();

                return RedirectToAction("Index");
            }

            return View(empresa);
        }

The SendMessage is like this:

 public static void SendMessage<T>(string queuName, T objeto, string connectionString)
        {
            QueueClient Client =QueueClient.CreateFromConnectionString(connectionString, "Empresa");
            BrokeredMessage message = new BrokeredMessage(objeto);
            message.ContentType = objeto.GetType().Name;
            Client.Send(new BrokeredMessage(message));
        }

Then on a console application I want to receive the messages to process them.

so I have this:

  static void Main(string[] args)
        {
            try
            {
                string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
                Console.WriteLine("Press key to continue");
                Console.ReadKey();
                QueueHelper.ReceiveMessage("Empresa", connectionString);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

and this:

public static void ReceiveMessage(string queuName, string connectionString)
    {
        QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "Empresa");

        // Configure the callback options
        OnMessageOptions options = new OnMessageOptions();
        options.AutoComplete = false;
        options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

        // Callback to handle received messages
        Client.OnMessage((message) =>
        {
            try
            {
                Empresa empresa = GetBody<Empresa>(message);
                // Process message from queue
                //Console.WriteLine("Body: " + );
                Console.WriteLine("MessageID: " + message.MessageId);

                // Remove message from queue
                message.Complete();
            }
            catch (Exception ex)
            {
                // Indicates a problem, unlock message in queue
                message.Abandon();
            }
        }, options);
    }

and the GetBody method

public static T GetBody<T>(BrokeredMessage brokeredMessage)
        {
            var ct = brokeredMessage.ContentType;
            Type bodyType = Type.GetType(ct, true);

            var stream = brokeredMessage.GetBody<Stream>();
            DataContractSerializer serializer = new DataContractSerializer(bodyType);
            XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max);
            object deserializedBody = serializer.ReadObject(reader);
            T msgBase = (T)deserializedBody;
            return msgBase;
        }

apparently the problem is in this line:

Type bodyType = Type.GetType(ct, true);

Jason Hogg - MSFT
  • 1,369
  • 9
  • 10
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

0 Answers0