【问题标题】:C# async/await to F# using Azure ML exampleC# async/await 到 F# 使用 Azure ML 示例
【发布时间】:2014-09-14 22:12:24
【问题描述】:

我正在使用 Azure ML,并且我有代码示例来调用我的 Web 服务(可惜它仅在 C# 中)。有人可以帮我把它翻译成 F# 吗?除了 async 和 await 之外,我什么都完成了。

 static async Task InvokeRequestResponseService()
        {
            using (var client = new HttpClient())
            {
                ScoreData scoreData = new ScoreData()
                {
                    FeatureVector = new Dictionary<string, string>() 
                    {
                        { "Zip Code", "0" },
                        { "Race", "0" },
                        { "Party", "0" },
                        { "Gender", "0" },
                        { "Age", "0" },
                        { "Voted Ind", "0" },
                    },
                    GlobalParameters = new Dictionary<string, string>() 
                    {
                    }
                };

                ScoreRequest scoreRequest = new ScoreRequest()
                {
                    Id = "score00001",
                    Instance = scoreData
                };

                const string apiKey = "abc123"; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);

                client.BaseAddress = new Uri("https://ussouthcentral.services.azureml.net/workspaces/19a2e623b6a944a3a7f07c74b31c3b6d/services/f51945a42efa42a49f563a59561f5014/score");
                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);
                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Result: {0}", result);
                }
                else
                {
                    Console.WriteLine("Failed with status code: {0}", response.StatusCode);
                }
            }

谢谢

【问题讨论】:

  • 顺便说一句:期待您的下一篇博文 :-) 听起来会很酷!
  • 谢谢。现在无法在记录上键入映射。查看 Fiddler,它显示 ErrorCode=MissingScoreInstance。我确保案例匹配。我想知道 Json 是否以不同的方式处理记录和类型?看起来 is 在后挂一个 @ 符号?
  • 就是这样。从记录类型更改为类使其工作。我周二的博客会很有趣

标签: azure f# azure-machine-learning-studio


【解决方案1】:

我无法编译和运行代码,但您可能需要这样的东西:

let invokeRequestResponseService() = async {
    use client = new HttpClient()
    let scoreData = (...)
    let apiKey = "abc123"
    client.DefaultRequestHeaders.Authorization <- 
        new AuthenticationHeaderValue("Bearer", apiKey)
    client.BaseAddress <- Uri("https://ussouthcentral..../score");
    let! response = client.PostAsJsonAsync("", scoreRequest) |> Async.AwaitTask
    if response.IsSuccessStatusCode then
        let! result = response.Content.ReadAsStringAsync() |> Async.AwaitTask
        Console.WriteLine("Result: {0}", result);
    else
        Console.WriteLine("Failed with status code: {0}", response.StatusCode) }
  • 将代码包装在 async { .. } 块中使其异步,并允许您在块内使用 let! 来执行异步等待(即在 C# 中使用 await 的地方)

  • F# 使用 Async&lt;T&gt; 类型而不是 .NET 任务,因此当您等待任务时,需要插入 Async.AwaitTask(或者您可以为最常用的操作编写包装器)

  • invokeRequestResponseService()函数返回F# async,所以如果你需要将它传递给其他库函数(或者如果它需要返回一个任务),你可以使用Async.StartAsTask

    李>

【讨论】:

  • 我们真的需要一个在线的 C# 到 F# 转换器。
  • 这并不完全可能,因为 C# 具有 F# 不支持的功能,例如受保护的方法或隐式接口实现。
  • C# -> F# 转换器没有任何意义。 F# 不是另一种具有奇怪、更简洁语法的 C#。
  • 关于如何做到这一点的任何想法,而不必用 async { } 包装函数
  • @DavidCrook 你是什么意思? async { .. } 的重点是让你使用异步构造并进行异步调用......所以如果你想编写异步代码,使用async { .. } 是要走的路......
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 2018-10-09
  • 2017-02-18
  • 1970-01-01
相关资源
最近更新 更多