# Xamarin.Forms: understanding DependencyService

> The DependencyService is a dependency solver that allows applications to call on platform specific functionalities from shared code. This feature therefore allows…

- Author: Jérôme Giacomini
- Language: en
- Canonical URL: [Xamarin.Forms: understanding DependencyService](https://jeromegiacomini.net/articles/2018/04/09/xamarin-forms-understanding-dependencyservice)
- Published: 2018-04-09
- Last modified: 2026-09-05
- Topics: .NET, C#, Xamarin, Windows

The **DependencyService** is a dependency solver that allows applications to call on platform-specific functionalities from shared code.

This feature therefore allows Xamarin.Forms applications to do everything a native application can do.

In practice, an interface is defined and the **DependencyService** In order to ensure the correct implementation of this interface from the different platform projects.

How do we use it?
1. Declare an interface common to all platforms.
2. Implement the interface on each platform
3. Record the implementation of the interface
4. Recover the instance implementing the interface


## Declare the interface

The first step is to declare an interface for the functionality you want to use on your OS.
For our example we'll share the d’URL sharing feature on iOS and Android.

**Déclearance of l’common interface between OS:**

```csharp
public class IShareService
{
      void Share( string url);
}

```


## Implementing the interface

**ImpléThis is not a problem.**

```csharp
public class DroidShareService : IShareService
{
   public void Share(string url)
   {
   var intent = new Intent(Intent.ActionView);

   intent.SetData(Android.Net.Uri.Parse(url));
   intent.SetFlags(ActivityFlags.ClearTop);
   intent.SetFlags(ActivityFlags.NewTask);

   Application.Context.StartActivity(intent);
   }
}
```


**Impléuse of iOS :**

```csharp
public class iOSShareService : IShareService
{
   public void Share(string url)
   {
        UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
   }
}

```


## Registration

Then for each platform you'll have to register the services.

The first way is by using the attribute **Dependency Attribute** as follows:

```csharp
[assembly: Xamarin.Forms.Dependency (typeof (DroidShareService))]
```


The second way is to call the method **Register** of **DependencyService**:

```csharp
Xamarin.Forms.DependencyService.Register<DroidShareService>();
```


Note: When you target UWP it is best to use the second method because when you compile in .NET Native it may delete your type. **Dependency Attribute**.

## Recover the instance implementing the interface

To recover the instance implementing the interface nothing simpler, simply call the static method **Get out&lt;TInstance&gt;** of the type **DependencyService**

```csharp
var shareService = Xamarin.Forms.DependencyService.Get<IShareService>();

shareService.Share("https://www.https://blogs.infinitesquare.com/");
```


## How does this work?

At the first call of the method **Get out&lt;T&gt;** the **DependencyService** will initialize its list of implementations by searching all assemblies for the uses of the attribute **Dependency Attribute**.

**Initialization:**

```csharp
            Type targetAttrType = typeof(DependencyAttribute);

            // Don't use LINQ for performance reasons
            // Naive implementation can easily take over a second to run
            foreach (Assembly assembly in assemblies)
            {
                Attribute[] attributes = assembly.GetCustomAttributes(targetAttrType).ToArray();
                if (attributes.Length == 0)
                    continue;

                foreach (DependencyAttribute attribute in attributes)
                {
                    if (!DependencyTypes.Contains(attribute.Implementor))
                    {
                        DependencyTypes.Add(attribute.Implementor);
                    }
                }
            }

```


**Note: if your application contains many assemblies, this first initialization may take some time.**

Then once the DependencyTypes lists are initialized it dynamically creates the service instance through the class **The activator.**

```csharp
(T)Activator.CreateInstance(dependencyImplementation.ImplementorType);

```


## To go further:
- [Source code of the DependencyService](https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/DependencyService.cs)
- [Develop your cross-platform apps for iOS, Android and Windows](https://www.editions-eni.fr/supports-de-cours/livre/xamarin-developpez-vos-applications-multiplateformes-pour-ios-android-et-windows-9782409007477)
