如何通过代码创建新的Microsoft Azure HybridConnection?

时间:2017-06-01 07:44:57

标签: c# azure

我正在使用HybridConnectionNamespace并通过Azure门户创建多个HybridConnections。问题很简单。如何以编程方式创建它(Azure SDK,PowerShell脚本等)?

enter image description here

2 个答案:

答案 0 :(得分:1)

根据这篇文章,目前有两种不同的方法来创建中继命名空间。

Azure门户和Azure资源管理器模板

如果您想以编程方式创建它,我建议您使用azure rest api按代码发送部署模板。

更多详细信息,您可以参考此article和代码:

注意:如果要使用rest api向azure发送请求,则首先需要创建Azure Active Directory应用程序和服务主体。生成服务主体后,您可以获取applicationid,访问密钥和talentid。更多细节,您可以参考此article

Rest Body(json.txt):

注意:您需要更改参数的名称和位置值。

{"properties":{"mode":"incremental","debugSetting":{"detailLevel":"RequestContent, ResponseContent"},"parameters":{"name":{"value":"yourrelayname"},"location":{"value":"location"}},"template":{"$schema":"http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#","contentVersion":"1.0.0.0","parameters":{"name":{"type":"string"},"location":{"type":"string"}},"resources":[{"apiVersion":"2016-07-01","name":"[parameters('name')]","location":"[parameters('location')]","type":"Microsoft.Relay/namespaces","properties":{"namespaceType":"Relay"}}]}}}

代码:

   string body = File.ReadAllText(@"D:\json.txt");
            // Display the file contents to the console. Variable text is a string.
            string tenantId = "tenantId";
            string clientId = "clientId(applicationid)";
            string clientSecret = "applicationSecret";
            string subscription = "subscriptionId";
            string resourcegroup = "Youresourcegroup";

            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId, clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }
            string token = result.AccessToken;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Resources/deployments/Microsoft.Relay?api-version=2016-07-01", subscription, resourcegroup));
            request.Method = "PUT";
            request.Headers["Authorization"] = "Bearer " + token;
            request.ContentType = "application/json";

            try
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //Get the response
            var httpResponse = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(httpResponse.StatusCode);

            Console.ReadLine();

结果:

enter image description here

答案 1 :(得分:0)

使用PowerShell CmdLets |混合连接管理器:

添加-HybridConnection

<强>更新-HybridConnection

删除-HybridConnection

获取-HybridConnection

设置-HybridConnectionManagerConfiguration

https://msdn.microsoft.com/en-us/library/azure/dn789178.aspx

好运