我有一个消息处理器,我想用一个已知模式的包装器来获取一块json,但是它具有一个动态对象的属性,如下所示:
public class NotificationDetails
{
[JsonProperty(PropertyName = "id")]
public string NotificationID { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateSent { get; set; }
public string TemplateUrl { get; set; }
public dynamic Model { get; set; }
}
如您所见,最后一个属性是动态的。通知将具有不同的模型模式,因此我希望它只是作为嵌套对象存储。
那就是说,当我尝试通过
创建对象时client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, collectionId), item)
我收到以下错误消息:
“MyClass.CreateNotification(NotificationDetails)”的最佳重载方法匹配有一些无效的参数
我以为我可以向这些文档扔东西。我究竟做错了什么?我应该为此Model属性使用除dynamic之外的其他内容吗?
UPDATE 我发现这是关于我如何在DocumentClient返回的任务上调用Wait()方法。一旦我恢复到异步等待策略,它就开始正常工作了。
答案 0 :(得分:0)
根据你的描述。我已经测试了你的代码,它的工作原理如下。你可以参考我的所作所为:
public static void CreateCosmosDocument()
{
DocumentClient client = new DocumentClient(new Uri("https://xxxxx/"), "C2y6yDjf5/R+ob0N8A7Cgv30VRDJxxxxM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", new ConnectionPolicy { EnableEndpointDiscovery = false });
TestEntity testEntity = new TestEntity { x = 11, y = 11, name = "wakaka", dynam = "hello dynam" };
var createdItem = client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("ToDoList", "Items"), new NotificationDetails { DateCreated=DateTime.Now, DateSent=DateTime.Now, TemplateUrl="www.baidu.com", Model= testEntity });
}
NotificationDetails类:
public class NotificationDetails
{
[JsonProperty(PropertyName = "id")]
public string NotificationID { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateSent { get; set; }
public string TemplateUrl { get; set; }
public dynamic Model { get; set; }
}
作为嵌套对象的TestEntity类:
class TestEntity
{
public ObjectId _id { get; set; }
public string name { get; set; }
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
public dynamic dynam { get; set; }
}
结果截图:
如果错误仍然存在,您最好与我们分享更详细的代码,以便进一步研究。