OpenRIAServices / OpenRiaServices

The Open RIA Services project continues what was previously known as WCF RIA Services.
https://openriaservices.gitbook.io/openriaservices/
Apache License 2.0
54 stars 47 forks source link

Code generation error when using .net 6 lib with DbDomainService #433

Closed spiridonovav closed 10 months ago

spiridonovav commented 10 months ago

Compile time exception:

Exception : Exception has been thrown by the target of an invocation. Exception : Could not load file or assembly 'System.Data.SqlClient, Version=4.6.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies cannot be loaded for execution. (0x80131058) Exception : Cannot load a reference assembly for execution.

StackTrace: at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at OpenRiaServices.Tools.DbContextUtilities.SetDbInitializer(Type contextType, Type dbContextTypeReference, Object initializer) in //src/OpenRiaServices.Tools/Framework/DbContextUtilities.cs:line 61 at OpenRiaServices.Tools.DomainServiceCatalog.AddDomainServiceType(Type domainServiceType) in //src/OpenRiaServices.Tools/Framework/DomainServiceCatalog.cs:line 118 at OpenRiaServices.Tools.DomainServiceCatalog.AddDomainServiceDescriptions() in //src/OpenRiaServices.Tools/Framework/DomainServiceCatalog.cs:line 214 at OpenRiaServices.Tools.DomainServiceCatalog..ctor(IEnumerable`1 assembliesToLoad, ILogger logger) in //src/OpenRiaServices.Tools/Framework/DomainServiceCatalog.cs:line 45 at OpenRiaServices.Tools.ClientCodeGenerationDispatcher.GenerateCode(ICodeGenerationHost host, ClientCodeGenerationOptions options, IEnumerable`1 assembliesToLoad, String codeGeneratorName) in //src/OpenRiaServices.Tools/Framework/ClientCodeGenerationDispatcher.cs:line 162 at OpenRiaServices.Tools.ClientCodeGenerationDispatcher.GenerateCode(ClientCodeGenerationOptions options, SharedCodeServiceParameters parameters, ILoggingService loggingService, String codeGeneratorName) in //src/OpenRiaServices.Tools/Framework/ClientCodeGenerationDispatcher.cs:line 84 ClientLib C:\Users\spiridonov_av.nuget\packages\openriaservices.client.codegen\5.4.0-tags-v5-4-0-preview-1.1\build\OpenRiaServices.Client.CodeGen.targets 341

Project to reproduce attached to the bug: OpenRIACodeGenError.zip

If i disable call to Database.SetInitializer<> in OpenRiaServices.Tools.DbContextUtilities.SetDbInitializer then ServiceContext generated fine.

Daniel-Svensson commented 10 months ago

@SandstromErik do you have the time to look into this.

The error "Cannot load a reference assembly for execution" should not happen as long as the server project is a normal .exe (the deps.json should list the runtime dlls).

I haven't looked at the sample since I am on mobile. If the linked server project is not the webapp but a library then we should have a clearer error message for that scenario.

Daniel-Svensson commented 10 months ago

I have not looked at the code yet but here are some comments:

As for Database.SetInitializer<> with null is probably there to avoid having ef try to create the database (or apply migration) during code generation in order to avoid database accesses during build.

It is unexpected that the call tries to load the sqlclient assembly

spiridonovav commented 10 months ago

I'm a stupid one! I use OpenRIAServices.EntityFramework nuget package in .net 6 lib! When i change on OpenRIAServices.EntityFrameworkCore code generation error was gone and service context generated well.

spiridonovav commented 10 months ago

I'm sorry, it's my fault. Bug closed.

Daniel-Svensson commented 10 months ago

@spiridonovav @SandstromErik using EF6 should work, the problem was that the server project was a stand alone library where some dependencies could not be found.

It should work if you add <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to the PropertyGroup in ServLib.csproj (or create a standard web application where you host the runtime and use that as the target for code generation)

It would be a good idea to update the old project templates to include the <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>, and maybe add a better starting point than the one which exist today. I tried to write some "getting started"/setup instructions at https://github.com/OpenRIAServices/Samples/tree/main/WpfCore_AspNetCore and the in the readme for the aspnet hosting project

Daniel-Svensson commented 10 months ago

If we get more of these error reports we might want to improve error messages or add a check for the setting (on failure) @SandstromErik