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.

Hello Radzen Dev Team!
How to add custom datasource method to list?
I'm added DataService.custom.cs file with my method:
public partial class DataService
{
public void TestMethod()
{

    }
}

But it won't appear in Invoke datasource method's list

Custom methods can be added to the page partial class:

Hi Guys

I'm having issues with Custom DB methods in my partial class.

Sometimes the methods appear straight away when Invoking a Data Source Method, sometimes I have to restart Radzen several times before the method appears and sometimes I can't get the method to appear at all... hence why I am now asking for help.

From the above screen shot, you can see that the 3rd method is appearing in the list. However, the 4th method doesn't:

Can you please explain what I need to do. The code builds and runs without errors.

Thanks

Hi @MikeT,

Is the fourth method part of the data-source meta definition or it is a custom method?

It's a custom method, but using a defined data source, it's supposed to return a subset of the data, namely just outstanding tasks.

So instead of:

var items = context.Recnotes.AsQueryable();

its doing this:

var items = context.Recnotes.AsQueryable()
.AsNoTracking()
.Where(i => i.isTask == "Y" && i.isTaskComplete == "N");

Invoke Data Source Method will list only data source methods part of the data source meta. Invoke Method action will list custom methods that are defined in the page partial class.

Ok, but this is a DBService method. Surely you should be able to add DB methods to the DBService partial class and have them appear under the Invoke Data Source Method list.

It seems a bit of a workaround to use a page custom method to then call a DB custom method.

Is this something you could add in a future release?

Hi @MikeT,

As @enchev explained the Invoke Data Source method only lists the methods generated for a database or REST data source. Custom methods are not listed. We don't currently have plans to list them. The custom methods documentation is available here.

Ok, thanks for your very prompt reply.