从Azure保存磁盘信息:
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal("myclientId", "mytenant", "mysecretId", AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Authenticate(credentials).WithSubscription("mySubscription");
var groupName = "myResourceGroup";
var vmName = "myVM";
var location = Region.USWest;
var vm = azure.Disks.List();
Console.WriteLine("Getting information about the virtual machine...");
MongoClient client = new MongoClient("mylocalhost");
IMongoDatabase database = client.GetDatabase("VM");
var collection = database.GetCollection<IDisk>("Disk ");
collection.InsertManyAsync(vm);
将它们保存到Mongodb时,出现错误:
已超过最大序列化深度(被序列化的对象是否具有循环引用?)
我在这里做什么错了?
答案 0 :(得分:0)
听起来您从该API返回的IDisk
正在解析为Mongodb不太满意的圆形图表。那么,最简单的解决方法是:不要序列化IFile
-毕竟,这不是您的类型,并且您无法控制它。相反,创建自己的某种类型的您想要的东西,然后序列化那个。例如:
sealed class MyDisk // TODO: rename me
{
public string Key {get;set;}
public string Name {get;set;}
public string RegionName {get;set;}
public int SizeGB {get;set;}
// etc, but *only* the information you actually want, and *only* using
// primitive types or types you control
}
...
var disks = (from disk in azure.Disks.List();
select new MyDisk {
Key = disk.Key,
Name= disk.Name,
RegionName = disk.RegionName,
SizeGB = disk.SizeInGB,
// etc
}).ToList();
并存储disks
,我们知道没有循环引用,因为我们对其进行控制。