简体   繁体   中英

Access WebJob Information via Microsoft.Azure.Management.Fluent in C#

Using Information via Microsoft.Azure.Management.Fluent I'm trying to get to information about Web Jobs. I'm able to use it to get information about Web Apps, Service Buses, Resource Groups, App Services, etc.

But I haven't been able to find a way to get to the Web Job level. In Azure the Web Jobs are located at the level

https://ms.portal.azure.com/#resource/subscriptions/ {SubId}/resourceGroups/{ApServiceName}/providers/Microsoft.Web/sites/{ApServiceName}/webJobs

Using Microsoft.Azure.Management.Fluent I haven't been able to find a way to get to the Web Jobs level. Is this possible via the Microsoft.Azure.Management.Fluent?

Is this possible via the Microsoft.Azure.Management.Fluent?

Based on my exerpience, No . According to Azure SDK source code, it seems that there is no way to get the WebJob level.

If we want to get WebJob level, Azure supplies the WebJob API for us to operate on WebJob. About authorization for the WebJob API, we could refer to the this blog .

I had a need to execute some management API code for WebJobs and found that it is now possible, although it's not easy to find it in the API documentation.

You can do it by installing the Microsoft.Azure.Management.AppService.Fluent package (I think it's also possible to do it the non-fluent management SDK too, although I didn't try this).

Getting access to the methods for managing a WebJob can be done like this:

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;

class MyWebJobsManagementClass
{
    public async Task DoSomeWebJobsManagement()
    {
        var jobs = await Azure
            .Authenticate() // See the docs for how to authenticate with this SDK
            .WithSubscription("your-subscription-id")
            .AppServices
            .Inner 
            .WebApps
            .ListWebJobsWithHttpMessagesAsync("resource-group-name", "app-service-name")
    }
}

It's through the non-obvious AppServices.Inner that you can get a reference to an IWebAppsOperations instance which then lets you perform quite a few operations on the WebJobs, including starting and stopping them.

Authentication Side note

If you're looking for a way to authenticate with Azure.Identity , instead of the file based credentials approach they used to use with these older SDKs, then there is a way to achieve this even though it's not supported "out-the-box".

There's a GitHub repo which contains an example of how to achieve this. I think it's by one of the developers on the Microsoft team, but isn't officially supported by Microsoft. There is no NuGet package for it and they recommend just copying the bits you need.

I actually found that the code in that sample repo was overly complex for my needs and in my case that all I needed was this. Note, I've copied this from my F# project without testing it, so I might have made a mistake in the conversion to C#, but hopefully it's close enough that you get the idea .

class AzureIdentityFluentCredentialAdapter : AzureCredentials
{
    public AzureIdentityFluentCredentialAdapter(string tenantId)
        : base(default(DeviceCredentialInformation), tenantId, AzureEnvironment.AzureGlobalCloud)
    {
    }

    public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var creds = DefaultAzureCredential() // Use the new Azure.Identity library to get access tokens

        var accessToken = await creds.GetTokenAsync(
            new TokenRequestContent(new [] { "https://management.azure.com/.default" }), 
            cancellationToken);

        return await TokenCredentials(accessToken.Token)
            .ProcessHttpRequestAsync(request, cancellationToken);
    }
}

This example doesn't do any token caching, but for my purposes I wasn't too bothered about this. It's also hardcoded the scope that I request the token for because I knew I was only going to be using this with the Azure management API.

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