我有上面的错误
"值不能为空。\ r \ n \ nParameter name:typeName"
这是堆栈跟踪:
> " 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)"
我的代码如下
我发送这样的消息没有例外(这是控制器动作)
[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);
}
SendMessage是这样的:
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));
}
然后在控制台应用程序上,我希望收到处理它们的消息。
所以我有这个:
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;
}
}
和此:
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);
}
和GetBody方法
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;
}
显然问题在于这一行:
键入bodyType = Type.GetType(ct,true);