I'm trying to migrate my code from a Webjobs project runing on .NET Framework 4.6.1 to a new .NET Core 2.0 Console project. I'm getting errors some errors here:
class Program
{
// Here I'm getting IKernel is obsolete. Use IKernelConfiguration and IReadOnlyKernel message.
// Also a message that reads: StandardKerynel is obsolete. Use StandardKernelConfiguration and StandardReadOnlyKernel
static readonly IKernel Kernel = new StandardKernel();
static JobHostConfiguration config;
static void Main(string[] args)
{
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", "connection");
Environment.SetEnvironmentVariable("AzureWebJobsStorage", "storage connection");
BootStrapIoc();
config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost(config);
host.RunAndBlock();
}
private static void BootStrapIoc()
{
// Also getting an error here that reads: Argument 1: Cannot convert System.Reflection.Assembly to System.Collections.Generic.IEnumerable<Ninject.Modules.NinjectModule>
Kernel.Load(Assembly.GetExecutingAssembly());
config = new JobHostConfiguration
{
JobActivator = new BrmJobActivator(Kernel)
};
}
}
I'm also getting errors in my BrmJobActivator code:
public class BrmJobActivator : IJobActivator
{
private readonly IKernel _container;
public BrmJobActivator(IKernel container)
{
_container = container;
}
public T CreateInstance<T>()
{
return _container.Get<T>();
}
}
UPDATE:
This is the warning message under NuGet packages in my project after installing Ninject package 3.2.2:
Also getting an error here that reads: Argument 1: Cannot convert System.Reflection.Assembly to System.Collections.Generic.IEnumerable
There are some changes in the latest prerelease version of Ninject. Please install the latest stable 3.2.2 version instead.
I tested your code on my side. After updated the Ninject version to 3.2.2, the code worked fine.
Ninject 3.3.0 was released September 26th 2017 and now targets .NET Standard 2.0 and thus also runs on .NET Core 2.0. Updating to 3.3.0 will fix the warning.
Related
I'm trying to login with integration to social networks, more specifically to Google in .NET MAUI. I've done it with Xamarin Forms and it worked perfectly, however, in MAUI a standard error is occurring:
Error CS0246 The type or namespace name 'Android' could not be found (are you missing a using directive or an assembly reference?) LoginWithRedes (net6.0-ios), LoginWithRedes (net6.0-maccatalyst), LoginWithRedes (net6.0-windows10.0.19041) C:\MAUI\LoginWithRedes\LoginWithRedes\Platforms\Android\GoogleManager.cs
Libraries not being recognized
Packages I added to the project
Code of the GoogleManager.CS Class where the standard error occurs to me:
`[assembly: Dependency(typeof(GoogleManager))]
namespace LoginWithRedes.Platforms.Android
{
public class GoogleManager : Java.Lang.Object, IGoogleManager, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
{
public static GoogleApiClient _googleApiClient { get; set; }
public static GoogleManager Instance { get; private set; }
public bool IsLogedIn { get; set; }
Context _context;
public GoogleManager()
{
_context = global::Android.App.Application.Context;
Instance = this;
}
public void Login()
{
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
.RequestEmail()
.Build();
_googleApiClient = new GoogleApiClient.Builder((_context).ApplicationContext)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
.AddScope(new Scope(Scopes.Profile))
.Build();
Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
((MainActivity)Forms.Context).StartActivityForResult(signInIntent, 1);
_googleApiClient.Connect();
}
public void Logout()
{
var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestEmail();
GoogleSignIn.GetClient(_context, gsoBuilder.Build())?.SignOut();
_googleApiClient.Disconnect();
}
public void OnAuthCompleted(GoogleSignInResult result)
{
if (result.IsSuccess)
{
IsLogedIn = true;
Application.Current.MainPage = new MainPage();
}
else
{
}
}`
OnActivityResult method that I implemented in MainActivity class
If anyone can help me with this error, I would be very grateful.
Note: I'm new to Xamarin and Maui.
Thank you very much in advance
I'm also new to Maui, and in my experience, these errors were caused by using native Xamarin libraries in Maui. Xamarin targets each platform separately using separate nuget packages. Maui's combined 1-project architecture means you need to use packages that work for both at once.
At least when I was getting started a few months ago, these weren't readily available. Firebase client was not yet released for .NET 6.0 (Multiplatform by default).
Things may have changed since then. But I had great success using Firebase with this plugin https://github.com/TobiasBuchholz/Plugin.Firebase. It wraps up the platform specific libraries into a single project API, with a really easy to use c# interface. You can use await and stuff as you would expect. Calling the native APIs was difficult, and required a lot of code duplication. This plugin saves a lot of time, and I haven't yet run into any problems.
The documentation on the plugin is a bit sparse, but hey, it's free and works.
I have a Xamarin Forms application that works fine on Android, but when I run on iOS, I get this error when I set up the database
Method not found: string SQLitePCL.raw.sqlite3_column_name(SQLitePCL.sqlite3_stmt,int)
and then when I try to insert into the database table
Method not found: string SQLitePCL.raw.sqlite3_errmsg(SQLitePCL.sqlite3)
The application uses mvvmlight and I have the sqlite-net-pcl package installed there and also in the platform code. The versions all marry up.
My iOS initialising code for sqlite looks like this (on the platform)
public class SQLiteConnectionFactory : ISqliteConnectionFactory
{
readonly string Filename = "mydb";
public SQLiteConnection GetConnection()
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
path = Path.Combine(path, Filename);
SimpleIoc.Default.Register<ISqliteConnectionFactory, SQLiteConnectionFactory>();
return new SQLiteConnection(path, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.SharedCache);
}
and the insert code (in the mvvmlight project) like this
public void SaveListData<T>(List<T> toStore)
{
try
{
lock (dbLock)
{
connection.InsertAll(toStore);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
I've had issues with Xam.Android and sqlite-net-pcl, but never on iOS
I have the sqlite-net-pcl 1.7.302-beta and SQLitePCL.* 2.0.2 packages installed (using the beta version to prevent some of the linking issues on the stable version)
Below is a shot of the iOS packages installed
Downgrade your sqlite-net-pcl package to version 1.5.231, that did the job for me
I'll start out with the questions first and follow up with context:
Is there a version of Unity.Interception available that is compatible with .NET Core?
Is there a .NET Core compatible alternative to Unity.Interception?
I am looking at using the Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor to short-circuit calls to certain interfaces (example code below), but it seems that the suggested NuGet package, Unity.Interception 4.0.1, is not compatible with .NET Core.
I have made an attempt to shoe-horn in the usage of Unity.Interception 4.0.1, as the code snippets used works fine in classic .NET; but as mentioned I am running into problems with .NET Core:
Install-Package : Package Unity.Interception 4.0.1 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Unity.Interception 4.0.1 supports: net45 (.NETFramework,Version=v4.5
)
I tried to circumvent this by adding net451 to the PackageTargetFallback list:
<PackageTargetFallback>$(PackageTargetFallback);net451;dnxcore50;portable-net451+win8</PackageTargetFallback>
This allowed me to install the package, but it then complains something fierce about needing a reference to mscorlib:
Error CS0012
The type 'Type' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I'm not going to attempt to reference in the Classic .NET framework into a .NET Core application, so I'm pretty much at a dead end here.
Example code:
public class Interceptions
{
public static object CreateCustomInterceptedProxy(Type type)
{
var interceptor = new InterfaceInterceptor();
var proxy = interceptor.CreateProxy(type, null);
var interceptionBehavior = new CustomInterceptionBehavior();
proxy.AddInterceptionBehavior(interceptionBehavior);
return proxy;
}
}
public class CustomInterceptionBehavior : IInterceptionBehavior
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
object response = null; // replace with stuff that builds an actual object
return input.CreateMethodReturn(response, new object[] { });
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public bool WillExecute => true;
}
I know the question is for around a month ago, but I thought it might be useful for other developers as well (because It's been a nightmare for me).
I have forked out Unity project and ported that to .Net Core 2.0. You can find it under this repository:
https://github.com/Chavoshi/Unity.NetCore
And also these are the nuget packages:
https://www.nuget.org/packages/Unity.NetCore/
https://www.nuget.org/packages/Unity.Interception.NetCore/
P.S: The only part which I was not able to port is TransparentProxyInterception that uses .Net Remoting which is totally discontinued in .Net Core.
Unfortunately you have to use 3rd party libraries like:
Unity fork: https://www.nuget.org/packages/Unity.Interception.NetCore/
Dora Interception: https://www.nuget.org/packages/Dora.Interception/ it has a detailed usage documentation here.
It seems Castle.Core's DynamicProxy is what I needed:
using Castle.DynamicProxy;
public class CustomInterceptor : IInterceptor
{
public static object CreateCustomInterceptedProxy(Type type)
{
var proxyGenerator = new ProxyGenerator();
var interceptor = new Interceptor();
var proxy = proxyGenerator.CreateInterfaceProxyWithoutTarget(type, interceptor);
return proxy;
}
}
public class CustomInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
object returnValue; // Do stuff to populate return value
invocation.ReturnValue = returnValue;
}
}
I create a new project in VS2017 RC I add a console lib and a class library.
now I can see that t he frameworks are added as dependencies.
but why does the core console not have NetStandard.Library ?
Instead I can see Microsoft.NetCore.app
As a result when I try to start the console app with dotnet -run
I get this error
[![enter image description here][2]][2]
Because you have the SDK/build-tools for 1.1.0 installed, but not the shared framework 1.1.0.
The SDK and the shared framework are not the same thing.
Maybe it's best explained in terms of Linux command line.
If you had it, what you currently have installed is
apt-get install dotnet-dev-1.0.1
but what you're missing is
apt-get install dotnet-sharedframework-microsoft.netcore.app-1.1.0
And the deeper reason is that you have
apt-get install dotnet-dev-1.0.1
instead of
apt-get install dotnet-dev-1.1.0
SDK version 1.1 will install shared framework 1.1.0, but if you're SDK is 1.0.1, then 1.1.0 has never been installed.
As to netstandard vs. netcore, David Fowler has a nice explanation attempt here.
Basically, every version of ".NET Core Framework" and ".NET Framework non-core" extend a version of netstandard, where a netstandard-library can be used in both CORE and NON-Core):
Concretely, your app can only run as either .NET Core or as .NET Framework. There is no netstandard-app (currently), only netstandard-libraries.
Both NetFramework_vX and NetCoreFramework_vX are supersets of NetStandardLibray_vX.
using System;
namespace Analogy
{
// Each interface represents a target framework and methods represents groups of APIs available on that target framework.
// The goal is to show the relationship between .NET Standard API surface and other .NET platforms
// .NET Standard
interface INetStandard10
{
void Primitives();
void Reflection();
void Tasks();
void Collections();
void Linq();
}
interface INetStandard11 : INetStandard10
{
void ConcurrentCollections();
void InteropServices();
}
interface INetStandard12 : INetStandard11
{
void ThreadingTimer();
}
interface INetStandard13 : INetStandard12
{
void FileSystem();
void Console();
void ThreadPool();
void Process();
void Sockets();
void AsyncLocal();
}
interface INetStandard14 : INetStandard13
{
void IsolatedStorage();
}
interface INetStandard15 : INetStandard14
{
void AssemblyLoadContext();
}
// .NET Framework
interface INetFramework45 : INetStandard11
{
void FileSystem();
void Console();
void ThreadPool();
void Crypto();
void WebSockets();
void Process();
void Sockets();
void AppDomain();
void Xml();
void Drawing();
void SystemWeb();
void WPF();
void WindowsForms();
void WCF();
}
interface INetFramework451 : INetFramework45, INetStandard12
{
// TODO: .NET Framework 4.5.1 specific APIs
}
interface INetFramework452 : INetFramework451, INetStandard12
{
// TODO: .NET Framework 4.5.2 specific APIs
}
interface INetFramework46 : INetFramework452, INetStandard13
{
// TODO: .NET Framework 4.6 specific APIs
}
interface INetFramework461 : INetFramework46, INetStandard14
{
// TODO: .NET Framework 4.6.1 specific APIs
}
interface INetFramework462 : INetFramework461, INetStandard15
{
// TODO: .NET Framework 4.6 specific APIs
}
// Mono
interface IMono43 : INetFramework46
{
void MonoSpecificApi();
}
// Windows Universal Platform
interface IWindowsUniversalPlatform : INetStandard14
{
void GPS();
void Xaml();
}
// Xamarin
interface IXamarinIOS : INetStandard15
{
void AppleAPIs();
}
interface IXamarinAndroid : INetStandard15
{
void GoogleAPIs();
}
// .NET Core
interface INetCoreApp10 : INetStandard15
{
}
// Future platform
interface ISomeFuturePlatform : INetStandard13
{
// A future platform chooses to implement a specific .NET Standard version.
// All libraries that target that version are instantly compatible with this new
// platform
}
}
For the difference between netcoreapp and netstandard please check: What's the difference between the new netstandardapp and netcoreapp TFMs?.
When you run dotnet -run on the command line you need to have installed the .NET Core SDK, which is a seperate installation of .NET Core. Installing VS2017 RC doesn't install this SDK. You can download the .NET Core 1.1 SDK here
I'm trying to create a Nancy/Unity-bootstrapper that can work with a OWIN-startup-class like the one described here:
Minimal sample: Self hosted Nancy using Owin, Unity bootstrapper including xUnit test
public class Startup
{
public void Configuration(IAppBuilder app)
{
IUnityContainer container = new UnityContainer();
container.RegisterType<IMessageHelper, MessageHelper>();
app.UseNancy(new NancyOptions
{
EnableClientCertificates = true,
Bootstrapper = new NancyOwinBoxBootstrapper(container)
});
}
}
public class NancyOwinBoxBootstrapper : UnityNancyBootstrapper
{
private IUnityContainer _container;
public NancyOwinBoxBootstrapper(IUnityContainer container)
{
_container = container;
}
protected override IUnityContainer GetApplicationContainer()
{
return _container;
}
}
The sample contained in the NancyOwinBox.zip works fine, but it is based on Nancy.Bootstrappers.Unity version 1.1. When i upgrade the NuGet package to the latest version (1.2) the sample fails with:
Resolution of the dependency failed, type = "Nancy.Bootstrapper.IApplicationStartup", name = "Nancy.ViewEngines.ViewEngineApplicationStartup".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, System.Collections.Generic.IEnumerable`1[Nancy.ViewEngines.IViewEngine], is an interface and cannot be constructed. Are you missing a type mapping?
At the time of the exception, the container was:
Resolving Nancy.ViewEngines.ViewEngineApplicationStartup,Nancy.ViewEngines.ViewEngineApplicationStartup (mapped from Nancy.Bootstrapper.IApplicationStartup, Nancy.ViewEngines.ViewEngineApplicationStartup)
Resolving parameter "viewEngines" of constructor Nancy.ViewEngines.ViewEngineApplicationStartup(System.Collections.Generic.IEnumerable1[[Nancy.ViewEngines.IViewEngine, Nancy, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null]] viewEngines, Nancy.ViewEngines.IViewCache viewCache, Nancy.ViewEngines.IViewLocator viewLocator)
Resolving System.Collections.Generic.IEnumerable1[Nancy.ViewEngines.IViewEngine],(none)
The current type, System.Collections.Generic.IEnumerable`1[Nancy.ViewEngines.IViewEngine], is an interface and cannot be constructed. Are you missing a type mapping?
Does anyone have an idea why this error occurs?
You need to register the EnumerableExtension in your container, like so:
container.AddNewExtension<EnumerableExtension>();
See UnityNancyBootstrapper.cs for Reference.