简体   繁体   中英

How to get success Response from Microsoft.Azure.Management.Fluent api method

I have created Virtual machine on Azure portal via Fluent API class in c#. my c# code is:

public JsonResult createVM()
{
    try
    {
        IAzure azure = Authenticate(subscriptionId);
        azure.VirtualMachines.Define(vmName)
             .WithRegion(location)
             .WithExistingResourceGroup(ResourceGroupName)
             .WithExistingPrimaryNetworkInterface(networkInterface)
             .WithSpecializedOSDisk(managedDisk, operatingSystem)
             .WithSize(vmSize)
             .Create();

        //Here i want complete response from above code..if success.
        return Json(string.Empty);
    }
    catch (CloudException ex)
    {
        Response.Write(ex.Response.Content);
        return Json(string.Empty);
    }
}

i get response in catch block if execution failed. but we need response if execution succeed .

Assign the azure VM creation code piece to a var. Check if that var is not null. If yes then the VM got created successfully. In case of exception you obviously go to the catch block. And if you want you can validate any property of the newly created VM object like they do it in this unit test .

Since you are using Azure Management Libraries for creating your Azure VMs, the SDK has automatically converted all the successful response into IVirtualMachine , you could just access the IVirtualMachine instance to retrieve all properties you want instead of the directly accessing the original HTTP response.

You could follow PrintVirtualMachine(IVirtualMachine virtualMachine) under here to retrieve the properties you expected and construct a new anonymous class which would contain your VM properties, then return it to your client as follows:

return Json(new
{
    ComputerName = linuxVM.ComputerName,
    PowerState = linuxVM.PowerState,
    ProvisioningState = linuxVM.ProvisioningState
    .
    .
});

I can't understand why you want the original HTTP response. But if you still insist on just retrieving the pure HTTP response, you need to follow the suggestion commented by Aravind to explicitly send the REST API Virtual Machines - Create Or Update with the related authorization by yourself. For authentication, you could follow Authentication API to access subscriptions to register your AAD app for accessing https://management.azure.com/ to create your Azure VMs. At this point, you need to do everything by yourself and you could control this process.

I am able to trace Log for Microsoft.Azure.Management.Fluent class. Follow Follow this link- Log and Trace section

i have log response in database:

 /// <summary>
    /// Here we can handle response.insert response in database
    /// </summary>
    /// <param name="invocationId">The invocation identifier.</param>
    /// <param name="response">The response message instance.</param>
    public void ReceiveResponse(string invocationId, HttpResponseMessage response)
    {
             logapResponse(response);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM