'Could not load type 'System.Runtime.Remoting.RemotingServices' from assembly 'mscorlib in Ninject - ninject

I have a problem as stated at the title. I am using Ninject as a Dependency Injection and my Service Locator as below:
internal class ServiceLocator
{
private static readonly IServiceLocator _serviceLocator;
static ServiceLocator()
{
_serviceLocator = new DefaultServiceLocator();
}
public static IServiceLocator Current
{
get
{
return _serviceLocator;
}
}
private class DefaultServiceLocator : IServiceLocator
{
private readonly IKernel kernel; // Ninject kernel
public DefaultServiceLocator()
{
kernel = new StandardKernel();
LoadBindings();
}
public T Get<T>()
{
try
{
return kernel.Get<T>();
}
catch (Exception hata)
{
throw hata;
}
}
private void LoadBindings()
{
kernel.Bind<IErrorDal>().To<ErrorDal>().InSingletonScope().WithConstructorArgument("connectionString", "myConnectionString");
kernel.Bind<IErrorBusinessRule>().To<ErrorBusinessRule>().InSingletonScope();
kernel.Bind<IApplicationBusinessRule>().To<ApplicationBusinessRule>().InSingletonScope();
kernel.Bind<ControlService>().To<ControlService>().InSingletonScope();
}
}
}
I have used ServiceLocator in my class ErrorService class as below:
public class ErrorService : IErrorService
{
private readonly IErrorBusinessRule _errorBusinessRule;
private readonly IApplicationBusinessRule _applicationBusinessRule;
private readonly ControlService _controlService;
public ErrorService()
{
//I am getting the error here.
this._errorBusinessRule = ServiceLocator.Current.Get<IErrorBusinessRule>();
this._controlService = ServiceLocator.Current.Get<ControlService>();
this._uygulamaIsKurali = ServiceLocator.Current.Get<IApplicationBusinessRule>();
}
}
I have got the System.TypeLoadException at the line
this._errorBusinessRule = ServiceLocator.Current.Get();
'Could not load type 'System.Runtime.Remoting.RemotingServices' from assembly 'mscorlib,

After investigation, I have pointed out that my test project type was MsTest Test Project. (.Net Core) When I choose unit test project (.NET Framework) the problem has solved.

Related

NinJect Get Service is not returning an instance of the class

I am working with MVC 4.0 & .net 4.0 and wish to use Ninject.
I have a static class, in which objects are created. I can NOT make the static calling class non-static!
I am trying to get an instance based on this binding
Bind<ClinicWebsite.ViewModels.ISelectionEngine>)
.To<ClinicWebsite.ViewModels.Genric_SelectionEngine>();
in the static class I call:
ClinicWebsite.ViewModels.Generic_SelectionEngine myService =
ClinicWebsite.App_Start.NinjectWebCommon.Kernel
.GetService(typeof(ClinicWebsite.ViewModels.ISelectionEngine))
as ClinicWebsite.ViewModels.Generic_SelectionEngine;
but when I examine "myservice" I get:
The name 'myservice' does not exist in the current context
What could I be doing wrong, or is there another way to explicitly create an instance with Ninject (again, cannot get rid of static calling class)
NinjectWebCommon
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
private static readonly StandardKernel kernel = new StandardKernel();
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
//DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void RegisterServices(IKernel kernel)
{
Bind<ClinicWebsite.ViewModels.ISelectionEngine>().To<ClinicWebsite.ViewModels.Generic_SelectionEngine>();
}
public static IKernel Kernel
{
get
{
return kernel;
}
}
}
I found the answer.
because this is in a static class I need to make"myservice" a static member of the class and then assign to it.

Error activating service - Ninject

I am getting the following error whenever I try to inject one of my service's dependency into the MVC controller:
Error activating IFeedService No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IFeedService into parameter svc of constructor of type FeedController
1) Request for FeedController
Suggestions:
1) Ensure that you have defined a binding for IFeedService.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
======================================================================
Here's how my code looks like:
ObjectFactory.cs
private static void RegisterServices(IKernel kernel)
{
// Contexts
kernel.Bind<IEntityObjectContext>().To<Entities>();
kernel.Bind<IAzureObjectContext>().To<AzureTableObjectContext>();
// Repositories
kernel.Bind<IEFRepository>().To<EFRepository>();
kernel.Bind<IAzureRepository>().To<AzureRepository>();
// Services
kernel.Bind<IFeedService>().To<FeedService>();
}
IEFRepository.cs
public interface IEFRepository : IDisposable
{
void SetContext(IEntityObjectContext context);
IQueryable<T> GetAll<T>() where T : class;
}
EFRepository.cs
public class EFRepository : IEFRepository
{
internal IEntityObjectContext context;
private Dictionary<Type, object> objectSets;
public EFRepository(IEntityObjectContext context)
{
this.context = context;
objectSets = new Dictionary<Type, object>();
}
public void SetContext(IEntityObjectContext context)
{
this.context = context;
}
}
IFeedService.cs
public interface IFeedService : IDisposable
{
IQueryable<FeedItem> GetPosts();
}
FeedService.cs
public class FeedService : IFeedService
{
private IEntityObjectContext _context;
private readonly IEFRepository _repo;
public FeedService(IEntityObjectContext context,
IEFRepository repo)
{
_context = context;
_repo = repo;
_repo.SetContext(_context);
}
public IQueryable<FeedItem> GetPosts()
{
using (_repo)
{
return _repo.GetAll<FeedItem>().Take(10);
}
}
}
FeedController.cs
public class FeedController : Controller
{
private readonly IFeedService _svc;
public FeedController(IFeedService svc)
{
_svc = svc;
}
}
As you can see, there are some nested dependency there in action. Not sure though, what needs to be added/removed for this bit to work.
Note: The error is thrown whenever I request the Feed/FetchFeed path. I also tried to comment out the FeedService's constructor portion to see if the nested dependencies are creating any problem, but again same error was thrown.
EDIT 1:
Rest of the code for the ObjectFactory.cs
class ObjectFactory
{
static ObjectFactory()
{
RegisterServices(kernel);
}
static IKernel kernel = new StandardKernel();
public static T GetInstance<T>()
{
return kernel.Get<T>();
}
private static void RegisterServices(IKernel kernel)
{
//...
}
}
EDIT 2:
I even tried to write a fairly basic service, but still the same error. Here's what I tried with:
public interface ITest
{
void CheckItOut();
}
public class Test : ITest
{
public void CheckItOut()
{
}
}
ObjectFactory.cs
kernel.Bind<ITest>().To<Test>();

AutoMapper IMappingEngine ConfigurationStore Initialize Not Happening

AutoMapper Version Used : 3.3.10
[TestClass]
public class AppControllerTests
{
private IMappingEngine _mappingEngine = null;
private ConfigurationStore _configurationStore = null;
[TestInitialize]
public void SetUp()
{
_configurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
_configurationStore.AddProfile(new AutoMapperProfile.AppProfile());
_mappingEngine = new MappingEngine(_configurationStore);
}
[TestMethod]
public void GetAppByAccountID()
{
// Error line
var mappingResult = _mappingEngine.Map<Category>(categoryList).AsQueryable();
}
}
public class AppProfile : Profile
{
protected override void Configure()
{
AutoMapperMappingConfigurations();
}
public void AutoMapperMappingConfigurations()
{
Mapper.CreateMap<DomainModels.Category, Category>().ReverseMap();
}
}
Exception:
An exception of type 'AutoMapper.AutoMapperMappingException'
occurred in AutoMapper.dll but was not handled in user code.
Suspect the
_configurationStore.AddProfile(new OOS.PresentationModelService.AutoMapperProfile.AppProfile());
is not able to create an istance of AppProfile if i write the manual mapping it's working as expected.
_configurationStore.CreateMap<Category, Category>().ReverseMap();

How to use the `Ninject.Extensions.Factory` to dynamically generate factories for internals classes?

I need to use the Ninject.Extensions.Factory to generate the constructor of internal classes. Follow one example:
using Ninject.Extensions.Conventions;
using Ninject.Modules;
using Ninject.Extensions.Factory;
namespace ClassLibrary
{
using System;
namespace ClassLibrary
{
internal class Class1
{
public void Print(string message)
{
Console.WriteLine(message);
}
}
internal interface IClass1Factory
{
Class1 Create();
}
public interface IInterface2
{
void PrintMessage();
}
internal class Class2 : IInterface2
{
private readonly IClass1Factory _class1Factory;
public Class2(IClass1Factory class1Factory)
{
_class1Factory = class1Factory;
}
public void PrintMessage()
{
Class1 class1 = _class1Factory.Create();
class1.Print("Class2's IInterface2 'PrintMessage' implementation.");
}
}
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
Kernel.Bind(r => r
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.BindAllInterfaces());
Kernel.Bind<IClass1Factory>().ToFactory();
}
}
}
}
Application using the library:
using ClassLibrary.ClassLibrary;
using Ninject;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel();
kernel.Load<MyNinjectModule>();
IInterface2 interface2 = kernel.Get<IInterface2>();
interface2.PrintMessage();
}
}
}
Even after include [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] I'm getting the following run tine error:
{"Type 'Castle.Proxies.IClass1FactoryProxy' from assembly
'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' is attempting to implement an inaccessible
interface.":""}
Any idea how to fix this?

Changing default object scope with Ninject 2.2

Is it possible to change the default object scope in Ninject 2.2? If so, how is it done?
As far as I can tell you could override AddBinding() on the BindingRoot (StandardKernel or NinjectModule) and modify the ScopeCallback property on the binding object.
public class CustomScopeKernel : StandardKernel
{
public CustomScopeKernel(params INinjectModule[] modules)
: base(modules)
{
}
public CustomScopeKernel(
INinjectSettings settings, params INinjectModule[] modules)
: base(settings, modules)
{
}
public override void AddBinding(IBinding binding)
{
// Set whatever scope you would like to have as the default.
binding.ScopeCallback = StandardScopeCallbacks.Singleton;
base.AddBinding(binding);
}
}
This test should now pass (using xUnit.net)
public class DefaultScopedService { }
[Fact]
public void Should_be_able_to_change_default_scope_by_overriding_add_binding()
{
var kernel = new CustomScopeKernel();
kernel.Bind<DefaultScopedService>().ToSelf();
var binding = kernel.GetBindings(typeof(DefaultScopedService)).First();
binding.ScopeCallback.ShouldBe(StandardScopeCallbacks.Singleton);
}
The CustomScopeKernel will also work with Ninject modules.
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<DefaultScopedService>().ToSelf();
}
}
[Fact]
public void Should_be_able_to_change_default_scope_for_modules()
{
var module = new ServiceModule();
var kernel = new CustomScopeKernel(module);
var binding = kernel.GetBindings(typeof(DefaultScopedService)).First();
binding.ScopeCallback.ShouldBe(StandardScopeCallbacks.Singleton);
}