azure-devops-dotnet-samples icon indicating copy to clipboard operation
azure-devops-dotnet-samples copied to clipboard

TfvcHttpClient: Must specify a text encoding, even if uploading a binary file?

Open davidjenkins opened this issue 8 years ago • 0 comments

// Notice that ContentType is being set to Base64Encoded.

static async Task<TfvcChangesetRef> AddFile(TfvcHttpClient client, string path, byte[] contentBytes)
{
    var item = new TfvcItem { Path = path };
    var content = new ItemContent { Content = Convert.ToBase64String(contentBytes), ContentType = ItemContentType.Base64Encoded };
    var change = new TfvcChange { ChangeType = VersionControlChangeType.Add, Item = item, NewContent = content };
    var changeset = new TfvcChangeset { Changes = new[] { change } };
    return await client.CreateChangesetAsync(changeset);
}

The sample above results in ArgumentException below, stating that I must either specify an encoding value, or provide base64 encoded content (which I already did).

ArgumentException: An explicit encoding value must be provided when supplying raw text content. Specify a valid text encoding value or provide base64 encoded content. Parameter name: pathActions.action.encoding

Specifying an encoding in content metadata works around this issue, but this inappropriate unless adding a text file and the encoding is properly detected.

// Must specify a text encoding, even if uploading a binary file?

static async Task<TfvcChangesetRef> AddFile(TfvcHttpClient client, string path, byte[] contentBytes)
{
    var contentMetadata = new FileContentMetadata { Encoding = Encoding.UTF8.CodePage };
    var item = new TfvcItem { Path = path, ContentMetadata = contentMetadata };
    var content = new ItemContent { Content = Convert.ToBase64String(contentBytes), ContentType = ItemContentType.Base64Encoded };
    var change = new TfvcChange { ChangeType = VersionControlChangeType.Add, Item = item, NewContent = content };
    var changeset = new TfvcChangeset { Changes = new[] { change } };
    return await client.CreateChangesetAsync(changeset);
}

Is this expected behavior? What is the purpose/impact of specifying a text encoding when uploading a binary file?

davidjenkins avatar Oct 17 '17 06:10 davidjenkins