Use custom services in data source service

What do I have to do to inject a custom service in the data source partial class?

For explanation:
I have a custom service "SomeService"
image

That service is registered in the Startup partial class:
image

I have created a partial data source class "NorthwindDbService.Custom.cs" where I try to inject the "SomeService" and call its methods.
image

I didn't find any useful explanation if it is possible to have a different constructor in a partial class.

When I try to run it this way I get an exception:

If I inject the "SomeService" in the main constructor, and remove the constructor in the partial class. It works, but the IDE will override it.

Is there any solution for this?

Thanks

You should extend the generated service constructor:

public SampleService(SampleContext context, NavigationManager navigationManager, YourService service)
...

This will be overwritten and set back to default by the Radzen IDE.

Not if you use a partial class. Also please check this article which explains how to control what Radzen overwrites and what not.

I did read this article before, but it didn't show an example how to extend an constructor.

If I extend the generated service constructor (NorthwindDbService.cs) it will be set back to default at next start with the Radzen IDE. (Running this directly in Visual Studio will work)

If I have the extended constructor in custom partial class (NorthwindDbService.Custom.cs), I get an exception at first page load, because Context is null

So is the only solution to add the generated datasource service file to the ignore list?

No. You just need to assign the context field as the generated constructor will not be called.You can also use constructor chaining.

.NET Dependency injection works by picking the constructor with most arguments.

Thanks,

that was what I did in the second picture of my last post, but I found my mistake, I didn't assign them.
Somehow I thought it would cherrypick it, because it was assigned in the generated constructor.

Sorry for the inconvenience.

For others that stuck with the same issue:

1 Like