I'm working in a Xamarin app with MVVMCross.
The app works perfectly when I run in debug mode.
But if try to run in release mode it fails with the exception:
Exception masked MvxIoCResolveException: Failed to resolve type
FlexConta.Contracts.Service.IUserService
[mvx] at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00035] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0
[mvx] at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve[T] () [0x00000] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0
[mvx] at MvvmCross.Platform.Mvx.Resolve[TService] () [0x00005] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0
[mvx] at FlexConta.Core.AppStart.Start (System.Object hint) [0x00000] in <880d0bdc2a5448ffb4d7b35d827753b5>:0
[mvx] at MvvmCross.Droid.Support.V7.AppCompat.MvxSplashScreenAppCompatActivity.TriggerFirstNavigate () [0x00005] in <74631770bbbe4bff8d50c85acb55083c>:0
[mvx] at MvvmCross.Droid.Support.V7.AppCompat.MvxSplashScreenAppCompatActivity.InitializationComplete () [0x00009] in <74631770bbbe4bff8d50c85acb55083c>:0
[mvx] at MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.<InitializeFromSplashScreen>b__7_1 () [0x0000a] in <099dd6f64bd74189922e6888bc76e146>:0
[mvx] at MvvmCross.Platform.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action action) [0x00000] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0
I'm using the MVVMCross IOC container and I'm registering de dependencies as follows:
public override void Initialize()
{
base.Initialize();
CreatableTypes()
.EndingWith("Repository")
.AsInterfaces()
.RegisterAsLazySingleton();
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
Mvx.RegisterSingleton<IUserRestAPI>(new UserRestAPI());
RegisterAppStart(new AppStart());
}
The User service class:
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly IDocumentTypesRepository _documentTypesRepository;
private readonly IUserRestAPI _userRestAPI;
public UserService(IUserRepository userRepository, IDocumentTypesRepository documentTypesRepository, IUserRestAPI userRestAPI)
{
_userRepository = userRepository;
_documentTypesRepository = documentTypesRepository;
_userRestAPI = userRestAPI;
}
.
.
.
}
What may be happening?
You can create a PreserveAttribute in your PCL and add it to the classes that the linker is stripping out. Xamarin docs describe the use as
If you do not want to take a dependency on the Xamarin libraries – for
example, you are building a cross platform portable class library
(PCL) – you can still use the Android.Runtime.Preserve attribute. To
do this, declare a PreserveAttribute class within the Android.Runtime
namespace like this:
namespace Android.Runtime
{
public sealed class PreserveAttribute : System.Attribute
{
public bool AllMembers;
public bool Conditional;
}
}
If you want to prevent linking of your shared PCL you can use link skip to prevent the linker from stripping away code from you PCL.
In your android cs proj just add <AndroidLinkSkip>YourPCLAssemblyNameHerer</AndroidLinkSkip> or via the properties UI. Android Options -> Linker -> Skip linking assemblies, enter your PCL assembly name in the input.
Related
On my .Net 5 ASP.NET application at Startup.cs I have the follwing (for Hangfire):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(serviceProvider));
...
}
I want to move to the .Net 6 way of configuration (in Program.cs), but I don't know how to get an instance of IServiceProvider to provide to the ServiceProviderJobActivator method.
The method is:
internal class ServiceProviderJobActivator : Hangfire.JobActivator
{
private readonly IServiceProvider _serviceProvider;
public ServiceProviderJobActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type type)
{
return _serviceProvider.GetService(type);
}
}
I have tried:
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(app.Services));
I also tried:
public override object ActivateJob(Type type)
{
return _serviceProvider.GetRequiredService(type);
}
but the ActivateJob returns null in both cases
Thanks
For anyone looking how to get a serviceProvider, after you call builder.Build() the app.Services resolves as an IServiceProvider.
Have you tried something like option 3 from Upgrading a .NET 5 "Startup-based" app to .NET 6 ?
It should lead to something like :
var app = builder.Build();
GlobalConfiguration.Configuration.UseActivator(new ServiceProviderJobActivator(app.Services));
I am trying to get the EF migrations to work in a Xamarin Forms project. I have a DbContext for a SQLite database:
public class TestDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
public TestDbContext()
{
SQLitePCL.Batteries_V2.Init();
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "testDb.db3");
optionsBuilder
.UseSqlite($"Filename={dbPath}");
}
}
I installed the Microsoft.EntityFrameworkCore.Tools nuget for the standard library, but it did not work. Starting from this, I added a Dummy .NET Core project.
I referenced the .NET Standard library (2.1) in the Dummy (Xamarin Forms project)
I installed the Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Tools nuget packages into the dummy project as well
I implemented an IDesignTimeDbContextFactory in the .net standard library like this:
public class DesignTimeFactory : IDesignTimeDbContextFactory<TestDbContext>
{
public TestDbContext CreateDbContext(string[] args)
{
return new TestDbContext();
}
}
When I run the command PM> Add-Migration InitialCreate -P XamarinEFTest -S EFDummy I get an exception:
Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Xamarin.Essentials.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
at Xamarin.Essentials.FileSystem.get_PlatformAppDataDirectory()
at Xamarin.Essentials.FileSystem.get_AppDataDirectory()
at XamarinEFTest.TestDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 24
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at XamarinEFTest.TestDbContext..ctor() in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 19
at XamarinEFTest.DesignTimeFactory.CreateDbContext(String[] args) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\DesignTimeFactory.cs:line 12
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
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 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass16_0.<FindContextFactory>b__1()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception has been thrown by the target of an invocation.
I have VS 2019 16.8.4, I created the test xamarin project from the layout template with the slider menu.
What do I need to do?
Edit: Maybe I need to setup the Core project with a different SQLite driver and with a different DesignTimeFactory? But how would this work? I need the migrations to work in the phone in the end.
The Xamarin.Essentials FileSystem.AppDataDirectory doesn't work on Windows/Mac, where you're trying to create the migration.
Use this sqlite database path for creating the migrations:
optionsBuilder.UseSqlite($"Data Source=migrations.db3");
This path is relative and you'll end up with a migrations.db3 file created in your project folder. Just ignote the .db file, it's only purpose was to create the migration.
EDIT:
I've been using this DbContext in my app and it works just fine.
public class MyDbContext : DbContext
{
private string _databasePath;
public DbSet<MyEntity> Issues { get; set; }
[Obsolete("Don't use this for production. This is only for creating migrations.")]
public MyDbContext() : this("nothing.db")
{
}
public MyDbContext(string dbPath)
{
_databasePath = dbPath;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite($"Data Source={_databasePath}");
}
}
}
Since MvvmCross v7 sticks on its own IoC container, I would like to replace it with the .NET Core one in order to have an easier life when registering third party libraries such as IHttpClientFactory, Polly, Automapper, etc. through already built-in extensions methods.
To achieve this, I've successfully created a class that implementas MvxSingleton<IMvxIoCProvider> described as follow:
public class HostingAdapter : MvxSingleton<IMvxIoCProvider>, IMvxIoCProvider
{
private IServiceProvider ServiceProvider;
private IServiceCollection ServiceCollection;
public HostingAdapter()
{
var host = Host
.ConfigureServices((context, serviceCollection) =>
{
// Configure local services
ConfigureServices(context, serviceCollection);
ServiceCollection = serviceCollection;
ServiceProvider = ServiceCollection.BuildServiceProvider();
})
.Build();
}
public void RegisterType<TFrom, TTo>() where TFrom : class where TTo : class, TFrom
{
ServiceCollection.AddTransient<TFrom, TTo>();
ServiceProvider = ServiceCollection.BuildServiceProvider();
}
public T GetSingleton<T>() where T : class
{
return ServiceProvider.GetRequiredService<T>();
}
public object GetSingleton(Type type)
{
return ServiceProvider.GetRequiredService(type);
}
.. and all the required methods requested by the interface.
Then on the platform specific side I override the IoC creation as follow:
protected override IMvxIoCProvider CreateIocProvider()
{
var hostingAdapter = new HostingAdapter();
return hostingAdapter;
}
The code seems to work but as soon as the app starts Mvx registers its own "extra" services such as the IMvxLoggerProvider, IMvxSettings and so on. And here issues come:
ServiceProvider = ServiceCollection.BuildServiceProvider(); is called during the Host initialization but Mvx still continue to register services after that. This means IServiceProvider is not 'in sync' with IServiceCollection and a new ServiceCollection.BuildServiceProvider(); call is needed. I temporarily solved updating the provider at each collection registration (like the code above) but I'm aware this affects performances. Anyone knows how to workaround this?
There are plenty of Mvx services that are not registered so the app fails to start. These are the IMvxLogProvider, IMvxAndroidLifetimeMonitor, IIMvxSettings, IMvxStart, etc. I just wonder, why? How can let Mvx handle the registration in my container of all what it needs to start? I partially solved some of them such as the logger thing replacing the default with a custom one, but other callbacks like InitializeLifetimeMonitor are called too late for being registered.
Do I need to change anything in my MvxApplication than the most standard implementation?
Am I really forced to replace the standard IoC container? How can I handle the IServiceCollection's extension methods that 3rd party libraries expose like services.AddHttpClient();?
If it needs, I am on Xamarin classic using the Droid platform. Thanks
Deliberately inspired by Unity.Microsoft.DependencyInjection repository I've workarounded this approaching the problem the other way round: instead of replacing the default IoC container, I manually initialize an IServiceCollection instance and I add it to the Mvx's IoC provider.
To achieve this, I've used the following code:
public class App : MvxApplication
{
public override void Initialize()
{
base.Initialize();
InitializeServiceCollection();
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
RegisterAppStart<HomeViewModel>();
}
private static void InitializeServiceCollection()
{
IServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
MapServiceCollectionToMvx(serviceProvider, serviceCollection);
}
private static void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddHttpClient();
}
private static void MapServiceCollectionToMvx(IServiceProvider serviceProvider,
IServiceCollection serviceCollection)
{
foreach (var serviceDescriptor in serviceCollection)
{
if (serviceDescriptor.ImplementationType != null)
{
Mvx.IoCProvider.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType);
}
else if (serviceDescriptor.ImplementationFactory != null)
{
var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
Mvx.IoCProvider.RegisterSingleton(serviceDescriptor.ServiceType, instance);
}
else if (serviceDescriptor.ImplementationInstance != null)
{
Mvx.IoCProvider.RegisterSingleton(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationInstance);
}
else
{
throw new InvalidOperationException("Unsupported registration type");
}
}
}
}
How do I read the appsettings.json file in my botframework (v4) app? I see the configuration is set up in the Startup.cs, but how do I access the settings in other classes?
One of the goals of the v4 ASP.NET core integration was to be idiomatic to existing .NET Core patterns. One of the things this means is that when you implement an IBot and add it with AddBot<TBot>, it becomes a participant in dependency injection just like an ASP.NET MVC controller would. This means that any services you might need to access, including configuration types such as IOptions<T>, will be injected into your bot via the constructor if you ask for them.
In this case, you just want to leverage the "options pattern" from the Configuration APIs and that would look something like this:
Startup.cs
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void ConfigureServices(IServiceCollection services)
{
// Bind MySettings to a section named "mySettings" from config
services.Configure<MySettings>(_configuration.GetSection("mySettings"));
// Add the bot
services.AddBot<MyBot>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseBotFramework();
}
}
MyBot.cs
public class MyBot : IBot
{
private readonly IOptions<MySettings> _mySettings;
public MyBot(IOptions<MySettings> mySettings)
{
_mySettings = mySettings ?? throw new ArgumentNullException(nameof(mySettings));
}
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
// use _mySettings here however you like here
}
}
I'm building a WPF application. I'm using Prism 4, and Unity. I want to add two Enterprise Library 5 blocks to the application, Logging and Exception Handling. I have a singleton LoggerFacadeCustom.cs in my Infrastructure class that supports the ILoggerFacade and I've created it in my bootstrapper, and it is generating log files. It "news" up a unity container in its constructor (second code block)
Where do I add the container.resolve for ExceptionManager? How do I connect the Exception handling block to ILoggerFacade in my bootstrapper? How do I get all the exceptions to come out in the same log? Here is my existing bootstrapper.cs
public class Bootstrapper : UnityBootstrapper {
protected override ILoggerFacade CreateLogger() {
return LoggerFacadeCustom.Instance;
}
protected override DependencyObject CreateShell() {
return Container.Resolve<Shell>();
}
protected override void InitializeShell() {
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
//Other shell stuff...
}
protected override IModuleCatalog CreateModuleCatalog() {
var catalog = new ModuleCatalog();
//These primary modules must register their own services as if they were acting independantly
catalog.AddModule(typeof(XmlCommentMergeModule));
//These support modules require at least one primary module above to be added first
catalog.AddModule(typeof(ToolboxHeaderModule));
catalog.AddModule(typeof(ToolboxFooterModule));
catalog.AddModule(typeof(ToolboxStartModule));
return catalog;
}
}
LoggerFacadeCustom:
public class LoggerFacadeCustom : ILoggerFacade {
private static readonly LoggerFacadeCustom _instance = new LoggerFacadeCustom();
public static LoggerFacadeCustom Instance { get { return _instance; } }
private LoggerFacadeCustom() {
var container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
_logWriter = container.Resolve<LogWriter>();
}
private readonly LogWriter _logWriter;
public void Write(string message) { Write(message, null); }
public void Write(string message, string category, int priority) {
_logWriter.Write(message, category, priority);
}
public void Write(string message, Dictionary<string, object> properties) {
_logWriter.Write(message, LiteralString.LogCategoryProcess, properties);
}
#region ILoggerFacade Members
public void Log(string message, Category category, Priority priority) {
throw new NotImplementedException();
}
#endregion
}
Your bootstrapper is the Composition Root of your application. You should register all dependencies there. And only there. You should never reference the container directly outside the composition root.
If your classes have a dependency you should inject that dependency using a pattern like constructor injection.
Don't use static classes. Static kills dependency injection and testability and it hides dependencies to a point where everything is referenced from everywhere.
Make your logger facade a constructor parameter. You can do the same with the error handling block.
Don't use the container as a ServiceLocator. That is considered an anti-pattern in modern software architecture.