コード例 #1
0
        private async Task Assign(string encryptionKey)
        {
            // create a zip package
            var contentRoot = Path.Combine(Path.GetTempPath(), @"FunctionsTest");
            var sourcePath  = Path.Combine(Directory.GetCurrentDirectory(), @"TestScripts\Node\HttpTrigger");
            var zipFilePath = Path.Combine(contentRoot, "content.zip");
            await TestHelpers.CreateContentZip(contentRoot, zipFilePath, @"TestScripts\Node\HttpTrigger");

            // upload the blob and get a SAS uri
            var    configuration    = _httpServer.Host.Services.GetService <IConfiguration>();
            string connectionString = configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage);
            var    sasUri           = await TestHelpers.CreateBlobSas(connectionString, zipFilePath, "azure-functions-test", "appcontents.zip");

            // Now specialize the host by invoking assign
            var    secretManager = _httpServer.Host.Services.GetService <ISecretManagerProvider>().Current;
            var    masterKey     = (await secretManager.GetHostSecretsAsync()).MasterKey;
            string uri           = "admin/instance/assign";
            var    request       = new HttpRequestMessage(HttpMethod.Post, uri);
            var    environment   = new Dictionary <string, string>()
            {
                { EnvironmentSettingNames.AzureWebsiteZipDeployment, sasUri.ToString() },
                { RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName, "~2" },
                { RpcWorkerConstants.FunctionWorkerRuntimeSettingName, "node" }
            };
            var assignmentContext = new HostAssignmentContext
            {
                SiteId      = 1234,
                SiteName    = "TestApp",
                Environment = environment
            };
            var    encryptedAssignmentContext = EncryptedHostAssignmentContext.Create(assignmentContext, encryptionKey);
            string json = JsonConvert.SerializeObject(encryptedAssignmentContext);

            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            request.Headers.Add(AuthenticationLevelHandler.FunctionsKeyHeaderName, masterKey);
            var response = await _httpClient.SendAsync(request);

            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
        }