Error on BlazorWebView and Class Library with Winform

52 Views Asked by At

I have a class library (CL) project which has a class I register to COM. This CL has a project reference to a Razor Class Library which has some components I want to show on a Windows Forms dialog using BlazorWebView.

public Form1()
{
    var services = new ServiceCollection();
    services.AddWindowsFormsBlazorWebView();
    services.AddSingleton<MyService>();
    services.AddBlazorBootstrap();
    InitializeComponent();
    blazorWebView1.HostPage = "wwwroot\\index.html";
    blazorWebView1.Services = services.BuildServiceProvider();
    blazorWebView1.RootComponents.Add<App>("#app");
}
[ProgId("Test")]
[ComVisible(true), Guid("C452A43E-9D62-6111-907A-E2132655BF97")]
public class Class1()
{
  public void OpenDialog()
  {
     var form = new Form1();

      form.ShowDialog();
  }
}

There is an external software written in C++ that uses the CL, but when it is supposed to show the form, it throws this exception:

InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILoggerFactory' has been registered.

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=System.Private.CoreLib
  StackTrace:
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
   at Interop.User32.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILoggerFactory' has been registered.

I have created a WinForms project and it successfully run with the same configuration of the BlazorWebView component.

I have tried adding a logging to the services of the BlazorWebView, but it is not that. I have also tried running from a .net Console App, and also I was successfully able to Show the dialog. Just needed to add the following:

Thread t = new Thread(OpenDialog);
t.SetApartmentState(ApartmentState.STA);

t.Start();

I can also display the dialog if I remove the BlazorWebView.

Any idea? I have to use the class library as a starting point (actually the starting point is the external c++ app, but that app needs to communicate via COM to the class library)

I am using .net 6

Expected to show a WinForms dialog that has a BlazorWebView running from a .net Class Library (executed from an external app via COM) and using a Razor Class Library for the web components.

0

There are 0 best solutions below