chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 279 forks source link

Changes for custom hosts #249

Closed smx-smx closed 4 years ago

smx-smx commented 4 years ago

Hello

This is the initial set of changes required to override CefClient and CefApp while keeping the AppBuilder structure.

It's not perfect, but it's a start. Here's an example: https://github.com/uxmal/reko-chromely/tree/037635c093c7163e9543f0e85884326a8c5df36b/src/BrowserHost

Do you have a suggestion on how to avoid having to do this?: https://github.com/uxmal/reko-chromely/blob/037635c093c7163e9543f0e85884326a8c5df36b/src/BrowserHost/RekoApp.cs#L25-L26

mattkol commented 4 years ago

@smx-smx

Do you have a suggestion on how to avoid having to do this?: https://github.com/uxmal/reko-chromely/blob/037635c093c7163e9543f0e85884326a8c5df36b/src/BrowserHost/RekoApp.cs#L25-L26


 public class RekoApp : ChromelyBasicApp
 {

        public override void ConfigureServices(ServiceCollection services)
        {
            base.ConfigureServices(services);
            services.AddSingleton<ChromelyWindowController, RekoBrowserHostController>();

           // TryAddSingleton is also correct at this stage 
           // - it will work because this is the entry point for registrations - 
           // but if you are certain that it is what is needed 
           // just add and it will force it to be the latest by using AddSingleton
           //   services.TryAddSingleton<ChromelyWindowController, RekoBrowserHostController>();
        }
    }

So just like we discussed earlier, if you do not register the custom controller class, the default class will be will be registered for you in ConfigureCoreServices using TryAddSingleton. Developers should not have to make changes to ConfigureCoreServices (it is a safety net). The other alternative is to register the core services first and then replace with custom in ConfigureServices. I am against that pattern because then one is registering services one does not need.

https://stackoverflow.com/questions/48185894/when-to-use-tryaddsingleton-or-addsingleton#:~:text=1%20Answer&text=As%20you%20already%20noticed%2C%20the,for%20the%20given%20service%20type.

.. the difference between TryAddSingleton and AddSingleton is that AddSingleton always 
appends the registration to the collection, while TryAddSingleton only does this when 
there exists no registration for the given service type.

When multiple registrations exist for the same service type, but one instance is requested, .NET Core
 will always return the last one. This means that the behavior of AddSingleton is to 
replace instances for non-collection resolves. 
mattkol commented 4 years ago

@smx-smx please let me know when it is appropriate to create new nugets. If you do not find anything from the Linux checks, the next build may be the release. Thanks.