You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how can I reference other assemblies and use inject? #6
I added reference to my project. Result:
Unable to compile template: skvbsxxk.c3i(3,17): error CS0234: The type or namespace name 'Extensions' does not exist in the namespace 'Microsoft'
When I add any other reference I get the same result. Can I manage this?
If you need to inject something in text template or reference other assemblies, probably you are overengeneering. Its not a RazorEngine responsibility go get service instances for you.
Making Inject directive work would require IoC container to be built in.
I would like to avoid that and keep engine as simple as possible.
stringtemplateText=@" <h1>@Localize(""Header"")</h1>";RazorEnginerazorEngine=newRazorEngine();RazorEngineCompiledTemplate<MyTemplateBase>compiledTemplate=razorEngine.Compile<MyTemplateBase>(templateText);stringresult=compiledTemplate.Run(instance =>{instance.Initialize(this.localizerInstance);// get instance from whatever source});
Usings in order to work require assemblies to be referenced on compilation phase
RazorEnginerazorEngine=newRazorEngine();RazorEngineCompiledTemplatecompiledTemplate=razorEngine.Compile(templateText, builder =>{builder.AddAssemblyReferenceByName("System.Security");// by namebuilder.AddAssemblyReference(typeof(System.IO.File));// by typebuilder.AddAssemblyReference(Assembly.Load("source"));// by reference});stringresult=compiledTemplate.Run(new{name="Hello"});
+ for adding injection.
Here is my use case. I'm serving fragments to my pages using SSE and a JIT approach. A page has many "Views" (containers for HTML Content) that get populated on-demand based on current conditions. A View has HTML, JavaScript Modules, and CSS that get registered in the page when received. I have a set of builder services that generate parts of what I am sending.
What I am sending is not an empty or static template. The fields are generated dynamically. It is a fully populated view with a custom "ViewController" and multiple "custom" models that can be sent back to the server. Inject is what I use to compose these components in real time.
There is no set number of builders. Each business domain provides a set of builders. For example, the Shipping domain might have builders for Ground Shipping and Air Shipping. If someone selects "Ground" a fragment is built and sent to the browser to populate a "Shipping" section of the page. "Ground" may have a builder for Carriers that would build fragments based on individual shippers.
What I am building is a framework to manage the creation and delivery of the fragments. I can render single-level Razor without problems and then put them together. But that gets messy.
This may not make a lot of sense without knowing more, but conceptually I'm doing something like this:
Well primary issue for me is that @inject is not default Razor keyword, however all @entries got parsed and stored somewhere, I dont know how to access this information.
Generally speaking I do not want to tie RazorEngineCore to particular IoC container unless there is no other choice.
If we were able to locate and process injects collection, it would be easy to come up with sutable solution
Activity
roja commentedon Mar 4, 2020
Also having issues making use of @using directives.
adoconnection commentedon Mar 4, 2020
If you need to inject something in text template or reference other assemblies, probably you are overengeneering. Its not a RazorEngine responsibility go get service instances for you.
Making Inject directive work would require IoC container to be built in.
I would like to avoid that and keep engine as simple as possible.
Localizer
Intended use
adoconnection commentedon Mar 4, 2020
Referencing assemblies
Usings in order to work require assemblies to be referenced on compilation phase
yamaritta commentedon Mar 5, 2020
Maybe it is possible to use default DI for .net core?
i.e https://github.com/toddams/RazorLight/blob/master/src/RazorLight/Extensions/ServiceCollectionExtensions.cs
adoconnection commentedon Mar 5, 2020
Can you tell what exactly are you building so I can understand better?
yamaritta commentedon Mar 11, 2020
I would like to have possibility to to register engine in DI and use inject IStringLocalizer in my razor template without RazorEngineTemplateBase
Randy-Buchholz commentedon Sep 11, 2020
+ for adding injection.
Here is my use case. I'm serving fragments to my pages using SSE and a JIT approach. A page has many "Views" (containers for HTML Content) that get populated on-demand based on current conditions. A View has HTML, JavaScript Modules, and CSS that get registered in the page when received. I have a set of builder services that generate parts of what I am sending.
// ViewComponent1.cshtml
// C# on Server
With inject I could do deeper composition.
(Made up RazorEngineCore syntax)
What I am sending is not an empty or static template. The fields are generated dynamically. It is a fully populated view with a custom "ViewController" and multiple "custom" models that can be sent back to the server. Inject is what I use to compose these components in real time.
adoconnection commentedon Sep 15, 2020
@Randy-Buchholz How many builder services you have? My first thought is to make them avaiable by adding in TemplateBase class.
Randy-Buchholz commentedon Sep 15, 2020
There is no set number of builders. Each business domain provides a set of builders. For example, the Shipping domain might have builders for Ground Shipping and Air Shipping. If someone selects "Ground" a fragment is built and sent to the browser to populate a "Shipping" section of the page. "Ground" may have a builder for Carriers that would build fragments based on individual shippers.
What I am building is a framework to manage the creation and delivery of the fragments. I can render single-level Razor without problems and then put them together. But that gets messy.
This may not make a lot of sense without knowing more, but conceptually I'm doing something like this:
Right now I render each one individually and merge them
adoconnection commentedon Sep 16, 2020
Your point is clear.
Well primary issue for me is that
@injectis not default Razor keyword, however all@entriesgot parsed and stored somewhere, I dont know how to access this information.Generally speaking I do not want to tie RazorEngineCore to particular IoC container unless there is no other choice.
If we were able to locate and process
injectscollection, it would be easy to come up with sutable solution