Blazor - Custom Methods

Hello, I am working with a Blazor project and I'm trying to work out how to create custom methods that would show up in the Load | Invoke DataSource Method.

So far I have attempted to create a service class that sits with the SecurityService and my datasource service, and add a scoped service via the Startup | ConfigureServices method, but this gets wiped out once back in Radzen and code regeneration is run.

Looking at the Startup Class I can see a Partial Method called OnConfigureServices and I'm wondering if I should create a Startup.Custom.cs file and add my service here.

A tad confused, any help would be appreciated.

Cheers
Jason
Perth, Western Australia.

Hi Jason,

Yes, you should create a Startup.Custom.cs partial class with the same namespace as the generated one and define a OnConfigureServices method where you register what you need.

Hi Korchev, I did have a go at this after sending the message but it did not appear to work.

I have pasted 3 screenshots below for your reference.

It appears code generation is not picking up on the new service and making the custom method available to the Radzen UI.

My apologies if I am missing something very basic here :wink:

Regards
Jason

Indeed the Invoke Data Source method dropdown lists only the data source methods. Custom services aren't listed there.

We are currently working on a new feature which is similar to the Invoke custom method available in Angular Radzen applications.

To invoke your service now you need to inject it first in the Page.cshtml.cs file (Radzen does not overwrite it) and then invoke via Execute C# action. Since the Page.cshtml.cs file is C# you have to use attribute injection:

public partial class PageComponent
{
    // Dependency injection works even if using the
    // InjectAttribute in a component's base class.
    [Inject]
    protected IDataAccess DataRepository { get; set; }
}

Thansks Krochev, I think I've almost got it.

It looks like the Load method gets a little tangled up though.

Can you point me in the right direction one more time ?

Cheers
Jason

This unfortunately is a bug. We will fix it with the next Radzen release.

Awesome, thanks Korchev. Enjoy your Friday :wink:

Cheers
Jason

Hello!
How to override services.AddIdentity<ApplicationUser, IdentityRole>()
in ConfigureServices method?

I need to register custom IdentityErrorDescriber in Startup.Custom.cs

partial void OnConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores()
.AddErrorDescriber();
}
but this return error System.InvalidOperationException: "Scheme already exists: Identity.Application"

It turned out to do this:
partial void OnConfigureServices(IServiceCollection services)
{
(new IdentityBuilder(typeof(ApplicationUser), services)).AddErrorDescriber();
}

Sorry, need help again.
At the "Finishing touches" of create a complete application tutorial says:

The final step is to update the personal data when the user submits the form.

  1. Add a new file server\Services\SecurityService.Custom.cs .
  2. Add add a new partial method OnUserUpdated which will set the custom properties FirstName , LastName and Picture .
using System;
using System.Collections.Generic;
using RadzenCrm.Models;

namespace RadzenCrm
{
    public partial class SecurityService
    {
        partial void OnUserUpdated(ApplicationUser user, ApplicationUser data)
        {
            user.FirstName = data.FirstName;
            user.LastName = data.LastName;
            user.Picture = data.Picture;
        }
    }
}

But generated SecurityService class isn't partial, so we can't declare another partial class...
Any ideas?

That steps should no longer be needed. The properties should be set automatically. We will update the tutorial to reflect that.