在框架和标准

时间:2018-05-14 23:09:02

标签: c# azure microservices azure-functions restsharp

我目前有一个正在构建的包装器,它使用一个函数(SendEmail)将一个JSON有效负载和一些文件发送到Http端点的Azure函数。但是,当我使用Restsharp 106.2.2发送我的请求时,似乎没有提供边界信息。当我的Azure函数接收到调用并尝试设置MultipartMemoryStreamProvider时,它不能,因为它没有看到任何边界。

控制台测试应用

private static void Main(string[] args)
{
    var emailService = new EmailService();
    var email = new MicroserviceMailMessage("from@test.com", "to@test.com",
        "Test Subject from Framework App", "Test Body from Framework App", true, DateTime.Now);
    email.Attachments.Add(new Attachment(@"c:\Temp\soccor5.jpg"));

    emailService.SendEmail(email);
}

包装库

public void SendEmail(MicroserviceMailMessage email)
{
    var client = new RestClient("http://localhost:7071/api");
    var request = new RestRequest("SendEmail", Method.POST);

    var transformedEmail = TransformEmail(email);

    request.AlwaysMultipartFormData = true;
    request.AddHeader("Content-Type", $"multipart/form-data");

    foreach (var attachment in email.Attachments)
    {
        var inline = attachment.ContentDisposition.Inline ? "inline." : "";
        request.Files.Add(new FileParameter
        {
            Name = $"attachments.{inline}{attachment.Name}",
            Writer = (s) =>
            {
                attachment.ContentStream.CopyTo(s);
                attachment.ContentStream.Dispose();
            },
            ContentType = attachment.ContentType.ToString(),
            ContentLength = attachment.ContentStream.Length,
            FileName = attachment.Name
        });
    }

    request.AddParameter("email", $"{JsonConvert.SerializeObject(transformedEmail)}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
}

Azure接收器功能

[FunctionName("EmailReceiver")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequestMessage req, ...)
{
    ...
    var provider = new MultipartMemoryStreamProvider();
    await req.Content.ReadAsMultipartAsync(provider) // Fails on this line
    ...
}

示例数据比较

使用RestSharp 106.2.2 Data with RestSharp 106.2.2

使用RestSharp 105.2.3(与Postman类似的结果) Data with RestSharp 105.2.3

错误

  

提供的“HttpContent”实例无效。它没有   带有'边界'的'multipart'内容类型标题   参数。\ r \ n参数名称:内容

到目前为止我尝试了什么:

将Restsharp恢复为105.2.3会恢复Rest请求中的边界信息,这对.Net Framework非常有用,但是我需要包装器在Framework和.Net Standard之间交叉兼容,所以这不能完全解决我的问题。问题

想法?我一直在想,如果有一些方法可以用Restsharp 106.2.2手动输入边界,但是我找不到很多关于它的文档。

1 个答案:

答案 0 :(得分:0)

这不仅仅是答案,而只是我提出的解决问题的解决方案。

我切换到Flurl.Http,最新版本兼容Standard和Framework。这就是我重写SendEmail函数的方法,

public void SendEmail(MicroserviceMailMessage email)
{
    var url = _azureEndpoint;
    var transformedEmail = TransformEmail(email);

    var content = new CapturedMultipartContent();
    content.AddJson("email", transformedEmail);

    foreach (var attachment in email.Attachments)
    {
        var inline = attachment.ContentDisposition.Inline ? "inline." : "";
        content.AddFile($"attachments.{inline}{attachment.Name}", attachment.ContentStream, attachment.Name);
    }

    url.PostAsync(content).Wait();
}

它会成功发布我需要的所有内容,文件和JSON。