# Services in Custom ViewModels | WPF Controls | DevExpress Documentation

This documentation topic describes how you can implement support of the Service mechanism in a custom View Model (not derived from the [ViewModelBase](/WPF/17351/mvvm-framework/viewmodels/viewmodelbase) class and not a [POCO](/WPF/17352/mvvm-framework/viewmodels/runtime-generated-poco-viewmodels) ViewModel).

If your View Model is not a POCO View Model and it is not derived from the ViewModelBase class, you can still use the Service mechanism. To implement Service support, implement the **ISupportServices** interface in your View Model.

The ISupportServices interface is shown below.

- C#
- VB.NET

<section id="tabpanel_whZHg1i9uQ_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">public interface ISupportServices {
    IServiceContainer ServiceContainer { get; }
}
</code></pre></section>
<section id="tabpanel_whZHg1i9uQ_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Public Interface ISupportServices
    ReadOnly Property ServiceContainer() As IServiceContainer
End Interface
</code></pre></section>

To implement the ISupportServices interface, use the following construction.

- C#
- VB.NET

<section id="tabpanel_whZHg1i9uQ-1_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">public class ViewModel : ISupportServices {
    IServiceContainer serviceContainer = null;
    protected IServiceContainer ServiceContainer {
        get {
            if(serviceContainer == null)
                serviceContainer = new ServiceContainer(this);
            return serviceContainer; 
        }
    }
    IServiceContainer ISupportServices.ServiceContainer { get { return ServiceContainer; } }
}
</code></pre></section>
<section id="tabpanel_whZHg1i9uQ-1_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Public Class ViewModel
    Implements ISupportServices
    Private m_serviceContainer As IServiceContainer = Nothing
    Protected ReadOnly Property ServiceContainer() As IServiceContainer
        Get
            If m_serviceContainer Is Nothing Then
                m_serviceContainer = New ServiceContainer(Me)
            End If
            Return m_serviceContainer
        End Get
    End Property
    Private ReadOnly Property ISupportServices_ServiceContainer() As IServiceContainer Implements ISupportServices.ServiceContainer
        Get
            Return ServiceContainer
        End Get
    End Property
End Class
</code></pre></section>

The ServiceContainer class implements the IServiceContainer interface that provides methods for registering and accessing services.

The IServiceContainer interface is shown below.

- C#
- VB.NET

<section id="tabpanel_whZHg1i9uQ-2_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">public interface IServiceContainer {
    void Clear();
    T GetService&lt;T&gt;(ServiceSearchMode searchMode = ServiceSearchMode.PreferLocal) where T : class;
    T GetService&lt;T&gt;(string key, ServiceSearchMode searchMode = ServiceSearchMode.PreferLocal) where T : class;
    void RegisterService(object service);
    void RegisterService(string key, object service);
}
</code></pre></section>
<section id="tabpanel_whZHg1i9uQ-2_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Public Interface IServiceContainer
    Sub Clear()
    Function GetService(Of T As Class)(Optional searchMode As ServiceSearchMode = ServiceSearchMode.PreferLocal) As T
    Function GetService(Of T As Class)(key As String, Optional searchMode As ServiceSearchMode = ServiceSearchMode.PreferLocal) As T
    Sub RegisterService(service As Object)
    Sub RegisterService(key As String, service As Object)
End Interface
</code></pre></section>

When Services (derived from the **ServiceBase** class) are registered in a View, they analyze their DataContext. If the DataContext is set to an **ISupportServices** object, a service registers itself within this object. This is accomplished by calling the **IServiceContainer.RegisterService** method on the **ISupportServices.ServiceContainer** object. Thus, the service becomes available for use within the ISupportServices object via the **IServiceContainer.GetService&lt;T&gt;** method.

The following code defines a command within a View Model that displays a message box via a dedicated Message Box service. The service is accessed via the **GetService&lt;T&gt;** method invoked on the View Model’s **ServiceContainer** protected property. The GetService method retrieves an actual service from a View that implements the specified IMessageBoxService interface.

- C#
- VB.NET

<section id="tabpanel_whZHg1i9uQ-3_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code class="lang-csharp">public class ViewModel : ISupportServices {
    IServiceContainer serviceContainer = null;
    protected IServiceContainer ServiceContainer {
        get {
            if(serviceContainer == null)
                serviceContainer = new ServiceContainer(this);
            return serviceContainer; 
        }
    }
    IServiceContainer ISupportServices.ServiceContainer { get { return ServiceContainer; } }
    IMessageBoxService MessageBoxService { get { return ServiceContainer.GetService&lt;IMessageBoxService&gt;(); } }

    public ICommand ShowMessageCommand { get; private set; }
    public ViewModel() {
        ShowMessageCommand = new DelegateCommand(ShowMessage);
    }
    void ShowMessage() {
        MessageBoxService.Show(&quot;Hello&quot;);
    }
}
</code></pre></section>
<section id="tabpanel_whZHg1i9uQ-3_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code class="lang-vb">Public Class ViewModel
    Implements ISupportServices
    Private m_serviceContainer As IServiceContainer = Nothing
    Protected ReadOnly Property ServiceContainer() As IServiceContainer
        Get
            If m_serviceContainer Is Nothing Then
                m_serviceContainer = New ServiceContainer(Me)
            End If
            Return m_serviceContainer
        End Get
    End Property
    Private ReadOnly Property ISupportServices_ServiceContainer() As IServiceContainer Implements ISupportServices.ServiceContainer
        Get
            Return ServiceContainer
        End Get
    End Property
    Private ReadOnly Property MessageBoxService() As IMessageBoxService
        Get
            Return ServiceContainer.GetService(Of IMessageBoxService)()
        End Get
    End Property

    Public Property ShowMessageCommand() As ICommand
        Get
            Return m_ShowMessageCommand
        End Get
        Private Set(value As ICommand)
            m_ShowMessageCommand = Value
        End Set
    End Property
    Private m_ShowMessageCommand As ICommand
    Public Sub New()
        ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
    End Sub
    Private Sub ShowMessage()
        MessageBoxService.Show(&quot;Hello&quot;)
    End Sub
End Class
</code></pre></section>

The following documentation topic contains an example that demonstrates how to support the ISupportService interface in a custom View Model and use services: [DXMessageBoxService](/WPF/17415/mvvm-framework/services/predefined-set/message-box-services/dxmessageboxservice).