No source available when debuggin ninject - ninject

I have same the problem as here No Source Available Error With Ninject When Debugging Code
Checking "Tools/Options/Debug/Just my code" makes absolutely no difference at all.
The error appears when debugger steps out from class below:
class TestModule : Ninject.Modules.NinjectModule {
public override void Load() {
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>();
}
}
I installed Ninject yesterday using 'Add library package reference' in VS2010

Related

Read Config From Another Project - Log4Net ASP.NET Core 3.1

I am trying to write a class library that uses log4net that looks something like this:
public class Logging
{
private ILog log4netLogger = null;
public Logging(Type type)
{
XmlDocument log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead("log4net.config"));
var repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
log4netLogger = LogManager.GetLogger(type);
}
public void Debug(string message)
{
log4netLogger(message);
}
public void Info(string message)
{
log4netLogger(message);
}
}
However, the xml configuration is in my test console app in C:\....\TestLogging\TestLog4Net\bin\Debug\netcoreapp3.1. I actually started with this console app to test log4net but I have moved all my code from the main method of Program.cs to the Logging.cs constructor, but I think the LogManager will not be able to find this now.
Is this at all possible?
I think it is possible. To use the log4net in the class library, you have to install the log4net package in the class library, then, you could add the class library reference in the console application and use the class library method. But, as you said, the log4net.config file should be in the console application netcoreapp3.1 folder, otherwise, the class library will not find the log4net.config file:

Service not calling OnShutdown() when windows shuts down

I have .net core console application, which is hosted as windows service.
I want to catch an event if the user logs off/shutdown the computer.
I have found ways to catch this event in .net framework (here & here).
But I cant figure out how to achieve this in .net core.
To create service I am using "ServiceBase" class. Sample code is as given below:
public class MyService : ServiceBase
{
readonly string LogPath = "D:\\TestAppService.txt";
#region Constructors
public MyService()
{
this.CanShutdown = true;
}
#endregion
#region Protected Functions
protected override void OnStart(string[] args)
{
//your code here
// call the base class so it has a chance
// to perform any work it needs to
base.OnStart(args);
}
protected override void OnStop()
{
//your code here
// Call the base class
base.OnStop();
}
protected override void OnShutdown()
{
using (StreamWriter sw = File.AppendText(LogPath))
{
sw.WriteLine("shutdown == true");
}
//your code here
base.OnShutdown();
}
#endregion
}
The OnStop and OnStart methods are being called.
but when I shutdown the computer my OnShutdown method is not called.
According to aspisof.net, you should be able to use the SessionEnding API. This is because it is listed as being exposed in the windows Compatibility Pack - available on NuGet here.
This article on learn.microsoft.com shows how you can include it in a .NET Core application.
tl;dr
Add the NuGet package
Target Windows only
One thing to note: this was originally designed to be a temporary fix for porting Windows specific .NET code over to .NET Core.
The more accepted way to implement Windows only features is to move as much code to .NET Standard libraries as possible, and to use conditional compilation directives to include platform specific code when building for that platform.
By design dotnet core is not "friendly" with platform specific stuff
(like listening to log off event seems to me).
The solution I use in one of Windows-hosted services is described here.
When application domain is forced to close by operating system on shutdown - there is a room for using AppDomain event handlers.

NUnit3-Console.exe on Win Server 2012 has no TestFixtures

I have been running NUnit3-Console on Win 2012 for almost two years. I recently updated my tests to .Net 4.7.2 and updated the NUnit and Selenium to the latest versions. I have installed NUnit3-Console v3.8 on the server and .Net 4.7.2.
All of the tests that used to run fine now fail with the message "Has no TestFixtures.
I wrote a simple test to isolate the issue and it does the same thing.
My Base class
enter code here
using NUnit.Framework;
using Utilities;
namespace CommonCode2.TestBases
{
public class NoSeleniumBase
{
public Parameters parms = new Parameters();
[OneTimeSetUp]
public void InitializePageTests()
{
parms.GetParameters();
}
[OneTimeTearDown]
public void CleanupPageTests()
{
}
}
}
My SimpleTest
enter code here
using CommonCode2.TestBases;
using NUnit.Framework;
using System;
namespace SimpleTest
{
[TestFixture]
public class TestClass : NoSeleniumBase
{
[Test]
public void Atest()
{
Console.WriteLine("This is a simple test");
}
}
}
The NUnit files are installed in C:\NUnit and the test is invoked using.
C:\Nunit\NUnit3-Console.exe --where "name =~ 'Atest'"
"C:\QA_Libraries3\SimpleTest.dll"
The TestResult file contains this tag
<property name="_SKIPREASON" value="Has no TestFixtures" />
and a message block
<![CDATA[Has no TestFixtures]]></message>
I am hoping that someone has encountered this issue and can point me in the right direction.
Thanks to Charlie Poole and Rob Prouse for their responsive and helpful support. The issue was not with NUnit3-Console but with the tests library directory access. Deleting and recreating the directory resolved the issue.

Use Ninject in both main and referenced projects

I have MVC4 website project and WCF project, both using Ninject.
I want to use class from WCF project in website project. I add reference to project and get both NinjectWebCommon.Start() executing (with "The static container already has a kernel associated with it!" error).
Is there way to make what I want?
Solved this using this startup in referenced project
public class Global : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
}

ASP.NET MVC4 StructureMap ExceptionCode202

I'm in the process of converting an ASP.NET MVC3 (LinqToSQL, EntityFramework) project to MVC4. I've created a fresh MVC4 project in VS2012, added packages, copied my Views, Controllers, etc.
Most things seem to work fine except when I try to access a controller that makes use of a Respository, as follows:
public class CustomerController : Controller
{
private ICustomerRepository _cr;
public CustomerController()
{
this._cr = new CustomerRepository(TTDataProvider.DB);
}
public CustomerController(ICustomerRepository customerRepository)
{
this._cr = customerRepository;
}
if I'm in VS2012 and debugging, what I'll get is an exception: "Activation error occured while trying to get instance of type CustomerController, key """. The exception is of type Microsoft.Practices.ServiceLocation.Activation and the Inner Exception is: "StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily TTLW.Models.TTLWDataContext, TTLW, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}.
My IoC code is:
using StructureMap;
using FluentSecurity;
using System.Diagnostics;
namespace TTLW {
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IPolicyViolationHandler>();
});
});
return ObjectFactory.Container;
}
}
}
And here's StructureMapMVC.cs
using System.Web.Http;
using System.Web.Mvc;
using StructureMap;
using TTLW.DependencyResolution;
[assembly: WebActivator.PreApplicationStartMethod(typeof(TTLW.App_Start.StructuremapMvc), "Start")]
namespace TTLW.App_Start {
public static class StructuremapMvc {
public static void Start() {
IContainer container = IoC.Initialize();
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = DependencyResolver.Current.ToServiceResolver();
}
}
}
As I say, this was all working without problems in my MVC3 application (although I was of course using the MVC3 version of StructureMap).
Once I hit the exception, if I just choose to continue then everything works (i.e. the controller functions); this is confirmed by choosing "Start Without Debugging" instead of "Debug". When I do that there is no exception thrown and things work as designed.
I've searched and come across posts from Phil Haack, Brett Allred and others (in fact I've already incorporated Allred's code in the last line of StructureMapMVC) but haven't found a solution. I can't consider the project converted as long as this exception is staring me in the face.
I've included all the code and messages I think are reasonable and would appreciate any insights. If you need to see more just let me know.
Thanks in advance.