Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Azure by (45.3k points)

I am working with Azure ML and I have the code sample to invoke my web service (alas it is only in C#). Can someone help me translate this to F#? I have everything but the async and await done.

 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);

                }

            }

Thanks 

1 Answer

0 votes
by (16.8k points)

I was facing a similar problem, but ended up finding this solution, check the code below:

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 { .. } wraps your code in the block, thereby making them asynchronous, and lets you use let! inside your block in order to perform the asynchronous waiting.

Browse Categories

...