Can't get invoke custom methods to work

I'm trying to learn Radzen & Blazor, so instead of using a stored procedure or view to query the data the way I want it, I wanted to try putting together a custom method. Following the documented procedures, I wasn't even coming close. So, I though I'd start a new project and follow the tutorial - Blazor Invoke method - Perform custom database query.

As soon as I pasted the custom code into visual studio, I could tell that there was something very wrong. Here's what I have for orders.razor.cs :

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Radzen;
using Radzen.Blazor;

namespace Northwind.Pages
{

public partial class OrdersComponent
{
    [Inject]
    NorthwindContext NorthwindContext { get; set; }

    public int OrdersByCountry(string country)
    {
        var orders = NorthwindContext.Orders.Where(o => o.ShipCountry == country);

        return orders.Count();
    }
}

}

Visual Studio is showing the same errors as Radzen. Here's what Radzen produces when I run the project:
radzen: Generating code ...
radzen: Code generation done in 678ms.
dotnet: watch :
dotnet: Started

dotnet: Pages\Orders.razor.cs(15,9): error CS0246: The type or namespace name 'NorthwindContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]

dotnet: Pages\Orders.razor.cs(14,10): error CS0246: The type or namespace name 'InjectAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]
Pages\Orders.razor.cs(14,10): error CS0246: The type or namespace name 'Inject' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]

dotnet:

dotnet: The build failed. Fix the build errors and run again.

dotnet: watch : Exited with error code 1

dotnet: watch : Waiting for a file to change before restarting dotnet...

I believe I was able to fix the Inject issues by putting in the same directives that are used in the designer files. But, I've searched all files in the project, and there are no Types, Namespaces, or anything with the word "NorthwindContext" anywhere. Shouldn't there be a reference to it in startup.cs?

I've read all of the Blazor documentation, and every Blazor community post regarding custom methods carefully. But, I'm still learning this platform and am likely doing something ridiculously stupid.

Thanks for any help you can provide,
Stephen

NorthwindContext is not available in your application most probably since you do not have Northwind data source like in our example.

Hello Enchev,
I did download the northwind datasource from here, but it was some time ago so perhaps its missing something?

Still, that doesn't explain why the project I've build from scratch in Radzen has the exact same errors:
radzen: Generating code ...
radzen: Code generation done in 731ms.
**dotnet: watch : **
dotnet: Started

dotnet: Pages\AddEquipment.razor.cs(9,9): error CS0246: The type or namespace name 'ItInventoryContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\ITInventory\server\ItInventory.csproj]

dotnet: Pages\AddEquipment.razor.cs(8,10): error CS0246: The type or namespace name 'InjectAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\ITInventory\server\ItInventory.csproj]
Pages\AddEquipment.razor.cs(8,10): error CS0246: The type or namespace name 'Inject' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\ITInventory\server\ItInventory.csproj]

**dotnet: **

dotnet: The build failed. Fix the build errors and run again.

dotnet: watch : Exited with error code 1

dotnet: watch : Waiting for a file to change before restarting dotnet...

What would you suggest?
Thank you,
Stephen

I'm not sure why I wasn't able to get support for this. Clearly the tutorial for Blazor "Invoking custom methods" is broken. I've done the tutorial many times, here are the errors produced when I reproduce it on my systems:

radzen: Generating code ...
radzen: Code generation done in 453ms.
*dotnet: watch : *
dotnet: Started

dotnet: Pages\Orders.razor.cs(13,9): error CS0246: The type or namespace name 'NorthwindContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]

dotnet: Pages\Orders.razor.cs(12,10): error CS0246: The type or namespace name 'InjectAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]
*Pages\Orders.razor.cs(12,10): error CS0246: The type or namespace name 'Inject' could not be found are you missing a using directive or an assembly reference?) [C:\Users\DevVM\Documents\RadzenProjects\NorthWind\server\Northwind.csproj]

*dotnet: *
dotnet: The build failed. Fix the build errors and run again.
*dotnet: watch : *
dotnet: Exited with error code 1
dotnet: watch : Waiting for a file to change before restarting dotnet...

The injection errors are caused by a missing directive. Adding using Microsoft.AspNetCore.Components; to orders.razor.cs fixes this issue.

To fix the NorthwindContext issue, change NorthwindContext to Data.NorhtwindContext so that it looks like this:
[Inject]
Data.NorthwindContext NorthwindContext { get; set; }

Now the project will build and run without error. A couple of other notes:

  1. Why does the tutorial give the datasource (Northwind) the same name as the project (Northwind)? It seems like a much better practice would be to name the datasource differently like "NorthwindData" to prevent confusion, especially for those that are new to this environment like myself.

  2. In several places the tutorial states "Set the action Type to Invoke custom method.


    However there is no Type of "Invoke custom method". The Types include "Invoke method" and "Invoke datasource method". This is confusing.

Stephen

It will be fixed immediately - thanks for the report. As for the missing namespaces it is your job to add them since our code examples are just parts of the code. Adding a namespace takes a second in Visual Studio / Visual Studio code. Just hover or select the code with the missing namespace and the studio will offer you to add what's missing.

1 Like

I decided to jump in this thread to clarify something.

Indeed the tutorial expects some developer knowledge - C# and .NET. After all it is custom code - code that the developer writes themselves. This is why it doesn't include instructions to import the required namespaces (not to mention some of them are application name specific). The error you get from the compiler is quite helpful too:

The type or namespace name 'InjectAttribute' could not be found (are you missing a using directive or an assembly reference?)

When you edit the project with Visual Studio it immediately recognises such errors even without compiling and suggests the fix.

1 Like

Understood.

Thank you,
Stephen