0

我第一次尝试使用 Integration Studio 创建一个扩展来通过文件上传进行 API 调用。不幸的是,我不太熟悉.NET。这是我试图在 Integration Studio 上复制的代码:

public static async Task<PendingAttachment> Upload(string FilePath)
{
    byte[] byteFile = System.IO.File.ReadAllBytes(FilePath);
    FileInfo fi = new FileInfo(FilePath);

    PendingAttachment pa = null;
    using (var client = new HttpClient())
    {
        string token = "XXXXXXXXXXXXX";
        client.DefaultRequestHeaders.Add("Authorization", "Bearer" + token);
        using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
        {
            content.Add(new StreamContent(new MemoryStream(byteFile)), "attachment", fi.Name);

            using (var message = await client.PostAsync("https://www.yammer.com/api/v1/pending_attachments", content))
            {
                if (message.IsSuccessStatusCode)
                {
                    var result = await message.Content.ReadAsStringAsync();
                    pa = Newtonsoft.Json.JsonConvert.DeserializeObject<PendingAttachment>(result);
                    return pa;
                }
            }
        }
    }
    return null;
}

所以,这就是我尝试过的。我在 Integration Studio 上创建了以下结构:

public class PendingAttachment
{
    public int id { get; set; }
    public int network_id { get; set; }
    public string url { get; set; }
    public string web_url { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public string original_name { get; set; }
    public string full_name { get; set; }
    public string description { get; set; }
    public string content_type { get; set; }
    public string content_class { get; set; }
    public string created_at { get; set; }
    public int owner_id { get; set; }
    public bool official { get; set; }
    public string small_icon_url { get; set; }
    public string large_icon_url { get; set; }
    public string download_url { get; set; }
    public string thumbnail_url { get; set; }
    public string preview_url { get; set; }
    public string large_preview_url { get; set; }
    public int size { get; set; }
    public string owner_type { get; set; }
    public string last_uploaded_at { get; set; }
    public int last_uploaded_by_id { get; set; }
    public string last_uploaded_by_type { get; set; }
    public object uuid { get; set; }
    public object transcoded { get; set; }
    public object streaming_url { get; set; }
    public string path { get; set; }
    public int y_id { get; set; }
    public string overlay_url { get; set; }
    public string privacy { get; set; }
    public object group_id { get; set; }
    public bool is_pending { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string scaled_url { get; set; }
    public Image image { get; set; }
    public int latest_version_id { get; set; }
    public string status { get; set; }
    public Latest_Version latest_version { get; set; }
    public Stats stats { get; set; }
    public string _OriginalFileName { get; set; }
}

public class Image
{
    public string url { get; set; }
    public int size { get; set; }
    public string thumbnail_url { get; set; }
}

public class Latest_Version
{
    public int id { get; set; }
    public int file_id { get; set; }
    public string content_type { get; set; }
    public int size { get; set; }
    public int uploader_id { get; set; }
    public string created_at { get; set; }
    public string path { get; set; }
    public string download_url { get; set; }
    public string thumbnail_url { get; set; }
    public string preview_url { get; set; }
    public string large_preview_url { get; set; }
    public string post_processed_id { get; set; }
    public object streaming_url { get; set; }
    public string revert_url { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string scaled_url { get; set; }
    public string thumbnail_path { get; set; }
    public string preview_path { get; set; }
    public string large_preview_path { get; set; }
    public string status { get; set; }
}
public class Stats
{
    public int following { get; set; }
    public int followers { get; set; }
    public int updates { get; set; }
    public object first_reply_id { get; set; }
    public object first_reply_at { get; set; }
    public int latest_reply_id { get; set; }
    public string latest_reply_at { get; set; }
    public int shares { get; set; }
}

由于此扩展将用于移动设备,因此我添加了 2 个输入参数,用于发送 FileData 和 FileName,以避免必须提取该数据。我添加了一个数据类型为 PendingAttachment 的输出参数,即上述结构。

所以我面临的问题是,由于它是一个异步方法,Visual Studio 不允许我有一个输出参数。那么有没有办法解决这个问题?

PS:是的,我已经尝试通过引用 Box File Upload 插件在 Service Studio 上正常实现这一点。它不起作用(出现 500 错误)。我也得到保证,这种方法可以正常工作。

谢谢您的帮助!

4

1 回答 1

0

如果您需要使用一个不需要的async方法,您可以在该方法返回.Wait()Task对象上调用该async方法。这将保持调用线程,直到异步代码完全执行。

于 2017-12-11T11:04:07.957 回答