When extending Geocortex Mobile, you may find yourself creating multiple components that have a shared concern, like a data source, or a REST endpoint call. Shared concerns like this present a good use case for creating a custom service. Implementing a custom service in Geocortex Mobile allows you to implement logic and shared resources that are available on startup to any component. Custom Services are also the recommended way of registering implementations for custom commands and operations.
This article will guide you through creating a custom service that fetches data from a dynamic source and delivers it to components through an operation.
Create a new file services/CustomService.cs under the platform agnostic project.
In the file, add a new component class CustomService and register it with Autofac.
Next, we can mock a dynamically updating data source that the service consumes. In a real application, this might be a webhook, or an RSS feed, or database.
note
It's essential to properly dispose of any managed resources, like the background thread in this example, to prevent memory leaks.
You've now built a custom service that fetches from a dynamic data source and exposes that data to other components! You could take this farther by creating an event that informs components and services when the data has changed, or returning a reference from the fetch data operation that can be observed for changes.