I have a little problem. My environment is a console app in .NET Core 2.1.
Look at the this code:
private static void Main(string[] args)
{
try
{
Console.WriteLine($"Test starts: {DateTime.Now.ToString("o")}");
string connectionString = "[My connection string]";
string containerName = "mycontainer";
CloudStorageAccount account = CloudStorageAccount.Parse(connectionString);
CloudBlobClient serviceClient = account.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference(containerName);
container.CreateIfNotExistsAsync().Wait();
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference($"{containerName}/Test.txt");
CloudBlobStream cloudBlobStream = cloudBlockBlob.OpenWriteAsync().Result;
string json = JsonConvert.SerializeObject(cloudBlobStream);
Console.WriteLine($"Test ends: {DateTime.Now.ToString("o")}");
}
catch (Exception e)
{
string stackTrace = e.StackTrace;
while(e != null)
{
Console.WriteLine(e.Message);
e = e.InnerException;
}
Console.WriteLine(stackTrace);
}
Console.Write("Press any key to exit...");
Console.ReadKey();
}
When I try to serialize the CloudBlobStream
object with the command string json = JsonConvert.SerializeObject(cloudBlobStream);
, I obtain the following Exception:
Error getting value from 'Length' on 'Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream'. Specified method is not supported. at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) at AzureBlobStreamSerializationTest.Program.Main(String[] args) in C:\Projects\AzureBlobStreamSerializationTest\AzureBlobStreamSerializationTest\Program.cs:line 28
Any idea on how to solve the problem?
Regards, Attilio
Length
, which is not readable on a stream that is opened for writing. – Brian Rogers