Developer forum

Forum » Development » Using EndPoint management in a custom scheduled task

Using EndPoint management in a custom scheduled task

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I need to build a scheduled task that does the following:

1. Connect to F&O
2. Call a custom end point that returns JSON
3. Convert JSON to XML
4. Save the XML to disk
5. Kick of a job that imports the XML
I know how to do 3-5 but could use some help with 1 and 2. How do I reuse the existing EndPoint management features in a scheduled task? I want to use the configured end points so I don't have to deal with URLs and authentication in the scheduled task.

Is there any (existing) source code available I can look at?

Thanks!

Imar


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Imar

I am building a connection to BC using endpoints - if you have access to devops it is here: https://dev.azure.com/dynamicwebsoftware/Dynamicweb/_git/Dynamicweb10/pullrequest/21346

Below is a code example that can get you started:

There's an existing HttpRestClient in the OData provider infrastructure, but it's internal to Dynamicweb.DataIntegration, so you can't use it directly from your own module. The cleanest approach is the same pattern the BC invoice module above uses: pull the Endpoint from EndpointService and call auth.AddIn.PrepareClient() yourself. All the auth plumbing is there — you just need to wire it together.

Here's the minimal version, using the same low-level calls that HttpRestClient uses internally:

 

The simplest approach — use EndpointService directly

using System;
using System.Net.Http;
using Dynamicweb.DataIntegration.EndpointManagement;
using Dynamicweb.Scheduling;
using Dynamicweb.Extensibility.AddIns;

[AddInName("FoImportScheduledTask")]

[Schedule(ScheduleInterval.Daily)]

public class FoImportScheduledTask : ScheduledTask

{

    [AddInParameter("Endpoint ID")]

    [AddInParameterEditor(typeof(IntegerParameterEditor), "")]

    public int EndpointId { get; set; }


    public override bool Execute()

    {

        var endpointService = new EndpointService();

        var endpoint = endpointService.GetEndpointById(EndpointId)

            ?? throw new InvalidOperationException($"Endpoint {EndpointId} not found.");


        using var handler = new HttpClientHandler { CheckCertificateRevocationList = true };

        using var client = new HttpClient(handler);


        // This applies OAuth2, bearer tokens, certificates — whatever is configured on the endpoint

        var auth = endpoint.Authentication;

        if (auth?.AddIn is not null && !string.IsNullOrEmpty(endpoint.Url))

            auth.AddIn.PrepareClient(new Uri(endpoint.Url), client, handler, endpoint);


        // Build your F&O custom endpoint URL

        var url = endpoint.Url.TrimEnd('/') + "/your/custom/path";
        var json = client.GetStringAsync(url).GetAwaiter().GetResult();
        // Steps 3-5: your JSON→XML conversion, save, and import job
        ProcessJson(json);
        return true;
    }
}

That's it. EndpointService is in Dynamicweb.DataIntegration.EndpointManagement — the same assembly that drives the OData provider.

 

If you want to reuse more of the OData infrastructure

The OData provider stack (HttpRestClient, ODataSourceReader, etc.) is all internal, so it's not directly reusable. But if your F&O endpoint follows the standard OData $metadata / entity set conventions, you could configure a standard OData Provider job in Data Integration pointing at the F&O endpoint and skip writing HTTP code entirely — the DI job handles the connection, auth, pagination, and XML/staging table output for you.

If the endpoint is a custom non-OData REST endpoint, the manual EndpointService pattern above is the right approach.

 

Key types

What you need

Type

Namespace

Look up the endpoint by ID

EndpointService.GetEndpointById(int)

Dynamicweb.DataIntegration.EndpointManagement

Apply auth (OAuth, certs, etc.)

endpoint.Authentication.AddIn.PrepareClient(...)

same

Your task base class

ScheduledTask

Dynamicweb.Scheduling

The existing reference for the PrepareClient pattern is BcEndpointProvider.cs on the above branch.

 

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Cool, thank you, that will get rme started.

>> but if your F&O endpoint follows the standard OData $metadata / entity set conventions, you could configure a standard OData Provider job in Data Integration

I have a combination. We need standard OData for standard data such as products and some custom end points for prices and others.

Thanks!

 

You must be logged in to post in the forum