Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to access interfaces from a referenced project in a Source Generator?

$
0
0

I am experimenting with Source Generators. I have followed several tutorials, most of which focus on generating code within the main project. One example that I followed can be found in this YouTube video.

However, I ran into a problem: I want to generate code based on interfaces from a referenced project and use it in my Blazor project.

Project Structure

BlazorApp1├── BlazorApp1.Client                  // Client-side implementation with generated services using HttpClient├── BlazorApp1.SourceGenerator.ServerSide  // Source Generator to generate APIs for server-side├── BlazorApp1.SourceGenerator.ClientSide  // Source Generator to generate service clients└── SharedProject                      // Contains interfaces and logic implementations├── IService.cs├── SomeService.cs

What I Want to Achieve

  1. SharedProject will contain interfaces and service implementations.
  2. On the server-side, I want to directly call these services and use a Source Generator to generate APIs.
  3. On the client-side, I want to generate implementations for these services using HttpClient to call the APIs on the server-side.

Problem

The issue I am facing is that the Source Generator cannot see the interfaces from the SharedProject. It only recognizes code from the main project where it is running (e.g., BlazorApp1).

I have tried declaring a variable directly as the data type from SharedProject in the hopes that the assembly would be loaded, allowing the source generator to see it, but it still does not work.

EDIT

In response to Selvin's request for code, I have created a project on GitHub at the following address: DemoSourceGenerator_SharedProject_NotWorking.

To explain further about the project and my intention in using Source Generator:

  • BlazorApp.Shared: Contains the models that need to be shared between the client and server, as well as the declarations of services (interfaces).
  • BlazorApp.ServerLogic: Implements the actual logic for services on the server side.
  • BlazorApp: The Blazor server-side application, where I have also added controllers for the client-side to access via API.
  • BlazorApp.Client: The Blazor assembly runs here, and I have created an additional folder called ClientLogic to implement the client-side logic, which involves using HttpRequest to call the API.
  • BlazorApp.SourceGenerator.ServerSide: This is where I will implement the code generator on the server side. The main goal is to automatically generate controllers instead of implementing them manually. For illustration purposes, I have manually implemented a controller named WeatherForecastController. I have also added two interfaces: ISomeService1 located in BlazorApp, and ISomeService2 in BlazorApp.Shared; both are annotated with the NeedGenerator attribute to allow for controller generation later. As you can see, when running the application, only ISomeService1 has a generated controller.

In this demo project, the main project is BlazorApp, which has a reference to BlazorApp.SourceGenerator.ServerSide. There was supposed to be an additional project called BlazorApp.SourceGenerator.ClientSide to automatically generate API calling logic; however, the main purpose was to demonstrate that it does not work in the referenced project, so I think it is not necessary to implement it yet.

In fact, if I directly add a reference from BlazorApp.SourceGenerator.ServerSide to BlazorApp.Shared, it would generate a controller for the ISomeService2 interface, but it would no longer generate a controller for the ISomeService1 interface. Moreover, if that were the case, BlazorApp.Shared would also need to add a reference to BlazorApp.SourceGenerator.ClientSide, and thus, instead of separating the logic for client-side and server-side, everything would be generated in one place, which is not reasonable at all.

enter image description here


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>