We have a "smallish" NServiceBus application(s) that use a dozen EF mapped tables and RabbitMQ as communication medium. With NServiceBus.Host.Exe startup of the application takes ~26s (debug and release versions both, with or without debugger attached).
After adding EndpointConfigurationType application setting the load time dropped by 2s.
So I've been researching this and about 8-10s is spent with EF in the first query and it's various generation routines. EF loading performance could also be enhanced by NGen:ing the libraries.
However after NGen:ing up the libraries and starting NServiceBus.Host.exe, Native images are loaded by default appdomain, but also by an additional appdomain (which uses IL dlls), so it looks like it uses LoadFrom to load up the dependencies.
Is there a way around this? We'd like to use NSB.Host.exe for it's windows service features (that we're not interested in reimplementing). Also the other "IWantTo..." features are nice since we have already several (16?) endpoints using those.
edit: http://blogs.msdn.com/b/abhinaba/archive/2014/02/18/net-ngen-explicit-loads-and-load-context-promotion.aspx
All my dlls are in same directory as NServiceBus.Host.exe, so based on that, Fusion should've loaded the native dlls as well.
edit2: This is "minimal" repro that doesn't have all the b&w of nservicebus.host.exe, but it seems to work in debug situation. It starts in under 2s versus 26s of Nservicebus.host.exe
using NServiceBus;
using BABAR.Configuration;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace BABAR.NGENHelper
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting: {0}", DateTime.Now.ToLongTimeString());
StartNSB();
Console.WriteLine("NSB started: {0}", DateTime.Now.ToLongTimeString());
}
private static void StartNSB()
{
// this ends up loading EF, in this case unnoticeable
NServiceBus.Unicast.Transport.TransportConnectionString.Override(
GetRabbitMQConnectionString);
NServiceBus.SetLoggingLibrary.Log4Net(() => log4net.Config.XmlConfigurator.Configure());
var semibus = Configure.With(GetNSBAssemblies())
.DefaultBuilder()
.DefineEndpointName("NGENHelper")
.UseTransport<NServiceBus.RabbitMQ>()
.PurgeOnStartup(false)
.UnicastBus()
.ImpersonateSender(false)
.RunHandlersUnderIncomingPrincipal(false)
.CustomConfigurationSource(new DefaultNServiceBusConfigurationSource())
.MessageForwardingInCaseOfFault()
.DisableTimeoutManager();
var bus = semibus.CreateBus()
.Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>()
.Install());
}
public static string GetRabbitMQConnectionString()
{
var nc = new Access().GetNode<NodeConfiguration>();
return nc.RabbitConnectionString;
}
internal static IEnumerable<Assembly> GetNSBAssemblies()
{
return new[] {
typeof(NServiceBus.RabbitMQ).Assembly, // IConfigureTransport for NSB
typeof(BABAR.Bootstrapper).Assembly,
};
}
}
}
I think just go with self hosting https://github.com/SimonCropp/NServiceBus.SelfHost#self-host
see sample code for a self host below
The "service features" of the NSB host can be substituted by calls to sc.exe
https://github.com/SimonCropp/NServiceBus.SelfHost#install--uninstall
class ProgramService : ServiceBase
{
IStartableBus bus;
static void Main()
{
using (var service = new ProgramService())
{
// so we can run interactive from Visual Studio or as a service
if (Environment.UserInteractive)
{
service.OnStart(null);
Console.WriteLine("\r\nPress any key to stop program\r\n");
Console.Read();
service.OnStop();
}
else
{
Run(service);
}
}
}
protected override void OnStart(string[] args)
{
Configure.GetEndpointNameAction = () => "SelfHostSample";
bus = Configure.With()
.DefaultBuilder()
.UnicastBus()
.CreateBus();
bus.Start(Startup);
}
static void Startup()
{
//Only create queues when a user is debugging
if (Environment.UserInteractive && Debugger.IsAttached)
{
Configure.Instance.ForInstallationOn<Windows>().Install();
}
}
protected override void OnStop()
{
if (bus != null)
{
bus.Shutdown();
}
}
}
Related
I'm trying to write some tests for testing GUI interface. I decided to choose NUnit.Forms. But the tests fall with the following error:
TearDown : System.ComponentModel.Win32Exception : The requested resource is in use
I have two versions of the source code tests.
First:
using System.Windows.Forms;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using YAMP;
namespace Tests.GUITests
{
[TestFixture]
public class GuiTest : NUnitFormTest
{
private FrmMain _frm;
//[SetUp] // or it is still needed
public override void Setup()
{
base.Setup();
_frm = new FrmMain();
_frm.Show();
}
[Test]
public void TestData()
{
var txtInput = new TextBoxTester("txtInput") {["Text"] = "2+2"};
var txtOutput = new TextBoxTester("txtOutput");
Assert.AreEqual("2+2", txtInput.Text);
var btnRes = new ButtonTester("btnRes");
btnRes.Click();
Assert.AreEqual("4", txtOutput.Text);
}
}
}
Second:
using System.Windows.Forms;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using YAMP;
namespace Tests.GUITests
{
[TestFixture]
public class GuiTest : NUnitFormTest
{
private FrmMain _frm;
//[SetUp] // or it is still needed
public override void Setup()
{
base.Setup();
_frm = new FrmMain();
_frm.Show();
}
[TearDown]
public override void TearDown()
{
_frm.Close();
_frm.Dispose();
}
[Test]
public void TestData()
{
var txtInput = new TextBoxTester("txtInput") {["Text"] = "2+2"};
var txtOutput = new TextBoxTester("txtOutput");
Assert.AreEqual("2+2", txtInput.Text);
var btnRes = new ButtonTester("btnRes");
btnRes.Click();
Assert.AreEqual("4", txtOutput.Text);
}
}
}
And there are two different versions of the method TestNoData:
public void TestFormNoDataHandler()
{
var messageBoxTester = new MessageBoxTester("Message");
messageBoxTester.ClickOk();
}
[Test]
public void TestNoData()
{
ExpectModal("Message", TestFormNoDataHandler);
var txtInput = new TextBoxTester("txtInput") {["Text"] = string.Empty};
Assert.AreEqual(string.Empty, txtInput.Text);
var btnRes = new ButtonTester("btnRes");
btnRes.Click();
Assert.IsFalse(_frm.DialogResult == DialogResult.OK);
}
[Test]
public void TestNoData()
{
var txtInput = new TextBoxTester("txtInput") {["Text"] = string.Empty };
Assert.AreEqual(string.Empty, txtInput.Text);
var btnRes = new ButtonTester("btnRes");
btnRes.Click();
Assert.IsFalse(_frm.Enable);
}
Testable form is very simple. There are two TextBox - "txtInput", "txtOutput" and button - "btnRes". In "txtInput" introduced a mathematical expression, and "txtOutput" output response. The decision of expression occurs when you press "btnRes". If the field "txtInput" empty, the button is disabled and you can not click on it.
When searching for solutions to this problem came on the following links:
AutomaticChainsaw: WinForms testing using NUnitForms
c# - I need to create a windows form from within a NUnit test - Stack Overflow
Unfortunately I can attach only 2 links. But the information I learned is very different. Especially the part of writing methods Setup and TearDown.
In any case, I specify the version I use:
Visual Studio 2015 Community
NUnit - 2.6.4.14350
NUnitForms - 1.3.1771.29165
Because it seems to me that the problem might be too recent versions of frameworks, as article I learned quite old.
Thank you for any suggestion.
UseHidden Property: Tests are run on a separate hidden desktop. This makes them much faster and it works for any tests that are not using the keyboard or mouse controllers. They are less disruptive and input tests cannot interfere with other applications.
UseHidden property controls whether a separate desktop is used at all.
Though tests on the separate desktop are faster and safer (There is no danger of keyboard or mouse input going to separate running applications.), however for some operating systems or environments the separate desktop does not work. And the tests throw up errors like:
System.ComponentModel.Win32Exception : The requested resource is in use
--TearDown
at NUnit.Extensions.Forms.Desktop.Destroy()
at NUnit.Extensions.Forms.Desktop.Dispose()
at NUnit.Extensions.Forms.NUnitFormTest.Verify()
In that case you can override UseHidden property from test class and set it to return false. This will cause the tests to run on original, standard desktop.
I've just installed Cosmos and tried to run the test program given by default. That's the code:
using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
namespace CosmosKernel2
{
public class Kernel : Sys.Kernel
{
protected override void BeforeRun()
{
Console.WriteLine("Cosmos booted successfully. Type a line of text to get it echoed back.");
}
protected override void Run()
{
Console.Write("Input: ");
var input = Console.ReadLine();
Console.Write("Text typed: ");
Console.WriteLine(input);
}
}
}
When I try to compile it, it says:
Error 8 Plug needed. System.Void System.Threading.Monitor.Exit(System.Object)
at Cosmos.IL2CPU.ILScanner.ScanMethod(MethodBase aMethod, Boolean aIsPlug) in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 663
at Cosmos.IL2CPU.ILScanner.ScanQueue() in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 779
at Cosmos.IL2CPU.ILScanner.Execute(MethodBase aStartMethod) in c:\Data\Sources\Cosmos\source2\IL2CPU\Cosmos.IL2CPU\ILScanner.cs:line 284
at Cosmos.Build.MSBuild.IL2CPUTask.Execute() in c:\Data\Sources\Cosmos\source2\Build\Cosmos.Build.MSBuild\IL2CPUTask.cs:line 239 C:\Program Files (x86)\MSBuild\Cosmos\Cosmos.targets 32 10 CosmosKernel2Boot
I'm using Visual Studio 2010, I have installed all requirements listed here: http://cosmos.codeplex.com/releases/view/123476
Thank you in advance!
"plug needed" error means that you have used some method which relies on an internal call or PInvoke, and thus Cosmos cannot compile it.
You probably use a method that has not been plugged yet or maybe missing a reference to that implementation (which makes Cosmos think it is not implemented)
Use the below guides to help you getting started:
http://www.codeproject.com/Articles/220076/Csharp-Open-Source-Managed-Operating-System-Intro
http://www.codeproject.com/Articles/29523/Cosmos-C-Open-Source-Managed-Operating-System
Update: Try using something similar to this code:
using System;
using Cosmos.Compiler.Builder;
namespace CosmosBoot1
{
class Program
{
#region Cosmos Builder logic
// Most users wont touch this. This will call the Cosmos Build tool
[STAThread]
static void Main(string[] args)
{
BuildUI.Run();
}
#endregion
// Main entry point of the kernel
public static void Init()
{
var xBoot = new Cosmos.Sys.Boot();
xBoot.Execute();
//There's supposed to be a bit of text here. Change it to Console.WriteLine("Hello world!");
}
}
}
It seems that Cosmos isn't working well with windows 8/8.1. So the only solution is to either install Windows 7 or run a virtual machine with installed Windows 7 (the latter worked for me)
I am new here and I hope that i will find a solution for my problem. The background of the problem is as follows:
I am trying to build an expert system that constitute a C# front-end which is interacting with Swi-prolog.
I have downloaded SwiPlCs.dll (A CSharp class library to connect .NET languages with Swi-Prolog)
And added a reference to it in a Visual Studio project(Win form app) that I have created to test if I can query prolog from c# (I followed the example used in the documentation found here).
It worked fine.
Then, in a more complicated scenario, I have built a WCF service that will act as an intermediary layer between Swi-Prolog and C# client application (it consumes the service).
The service is hosted in IIS 7.0.
For the sake of simplicity, lets say my service contains three methods.
The first method initializes the prolog engine, consults prolog source file then queries the file.
The second method performs another query.
The third method calls PlCleanup().
Method#1:
public void LaunchAssessment()
{
Dictionary<string, string> questions = new Dictionary<string, string>();
#region : Querying prolog using SwiPlCs
try
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" };
PlEngine.Initialize(param);
PlQuery.PlCall("consult('D:/My FYP Work/initialAssessment')");
using (var q = new PlQuery("go(X, Y)"))
{
foreach (PlQueryVariables v in q.SolutionVariables)
{
questions.Add("name", v["X"].ToString());
questions.Add("age", v["Y"].ToString());
}
}
}
}
catch (SbsSW.SwiPlCs.Exceptions.PlException exp)
{
throw new FaultException<PrologFault>(new PrologFault(exp.Source), exp.MessagePl);
}
#endregion
Callback.PoseQuestion(questions, ResponseType.None);
}
Method#2:
public void DetermineAgeGroup(int age)
{
//Determine age group
string age_group = string.Empty;
try
{
using (var query = new PlQuery("age_group(" + age + ", G)"))
{
foreach (PlQueryVariables v in query.SolutionVariables)
age_group += v["G"].ToString();
}
}
catch (SbsSW.SwiPlCs.Exceptions.PlException exp)
{
throw new FaultException<PrologFault>(new PrologFault(exp.Source), exp.MessagePl);
}
//Check whether age_group is found or not
if (string.IsNullOrEmpty(age_group))
{
throw new FaultException<NoSolutionFoundFault>(new NoSolutionFoundFault("No solution found"), "Age specified exceeds the diagnosis range!");
}
else
{
Callback.RespondToUser(age_group, ResponseType.Age);
}
}
Method#3:
public void QuitProlog()
{
if (PlEngine.IsInitialized)
{
PlEngine.PlCleanup();
}
}
The client invokes the first method just fine and a result of the first query is successfully returned. When client tries to call the second method an exception is thrown with message (attempted to read or write protected memory) which causes the application to freeze. I checked the event viewer and this is what I get:
Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
at SbsSW.SwiPlCs.SafeNativeMethods.PL_new_term_ref()
at SbsSW.SwiPlCs.PlQuery..ctor(System.String, System.String)
at SbsSW.SwiPlCs.PlQuery..ctor(System.String)
at PrologQueryService.PrologQueryService.DetermineAgeGroup(Int32)
I also tried to use the interface for a .NET project.
Looking in the official repository of the CSharp interface to SWI-Prolog I noticed that the project is very old and the latest updates do not seem included in the binaries available in the download page of the official website.
Then I did the following steps:
The contrib repository dedicated to .NET indicates that the compatible SWI-Prolog version (at the time of writing) is "8.0.3-1" (look in the README file).
-> Then I uninstalled from my computer the latest stable and installed the indicated one. I got it from the full list of downloads of the old versions at this link.
I cloned the SWI-Prolog/contrib-swiplcs repository, unloaded the incompatible projects from the solution, in my case, since I don't use Visual Studio.
-> I set the target framework to Net Framework 4.8 and recompiled it (you can also do this with standard NET). Beware of some pragma directives defined in the old project file (For example I re-defined _PL_X64 variable via code.
I brought the main unit test methods into a new project with xUnit wiht the appropriate changes.
I set the target to x64, recompiled and rebuilt the tests and the "hello world" example.
It worked!
I was able to use SWI-Prolog both for Net 4.8 and in other Net Core applications (if you make the needed changes in order to target the Net Standard). You should not have any problem in both cases).
This is my fork as a preliminary example.
Finally, I can load a *.pl Prolog file with a program in my C# application and use it to evaluate some business logic rules (example with boolean answer [Permitted/Not-Permitted]):
[Fact]
public void ShouldLoadAProgramAndUseIt()
{
var pathValues = Environment.GetEnvironmentVariable("PATH");
pathValues += #";C:\Program Files\swipl\bin";
Environment.SetEnvironmentVariable("PATH", pathValues);
// Positioning to project folder
var currentDirectory = Directory.GetCurrentDirectory().Split('\\').ToList();
currentDirectory.RemoveAll(r => currentDirectory.ToArray().Reverse().Take(3).Contains(r));
var basePath = currentDirectory.Aggregate((c1, c2) => $"{c1}\\{c2}");
var filePath = $"{basePath}\\prolog_examples\\exec_checker.pl";
String[] param = { "-q", "-f", filePath };
PlEngine.Initialize(param);
try
{
var query = "exutable('2020-08-15',[('monthly', ['2019-12-30', '2020-03-10'])])";
_testOutputHelper.WriteLine($"Query: {query}");
using (var q = new PlQuery(query))
{
var booleanAnswer = q.NextSolution();
_testOutputHelper.WriteLine($"Answer: {booleanAnswer}");
Assert.True(booleanAnswer);
}
query = "exutable('2020-08-15',[('daily', ['2019-12-30', '2020-08-15'])])";
_testOutputHelper.WriteLine($"Query: {query}");
using (var q = new PlQuery(query))
{
var booleanAnswer = q.NextSolution();
_testOutputHelper.WriteLine($"Answer: {booleanAnswer}");
Assert.False(booleanAnswer);
}
}
finally
{
PlEngine.PlCleanup();
}
}
Try to close engine in the end of the first method and initialize it in the second again.
You can check this as the answer to the question unless you object.
In C# (.NET), can two threads running in the same application have DIFFERENT "WorkingFolders"??
As best I can tell, the answer would be "NO". I think the WORKING DIR is set by the PROCESS in Win32.. Am I wrong here?
According to the following test code, (as well the Win32 SetCurrentDirectory API call), this is NOT possible, but has anyone figured out a way to MAKE it possible?
using System;
using System.Threading;
public class TestClass {
public ManualResetEvent _ThreadDone = new ManualResetEvent(false);
public static void Main() {
Console.WriteLine(Environment.CurrentDirectory);
Thread _Thread = new Thread(new ParameterizedThreadStart(Go));
TestClass test = new TestClass();
_Thread.Start(test);
if(test._ThreadDone.WaitOne()) {
Console.WriteLine("Thread done. Checking Working Dir...");
Console.WriteLine(Environment.CurrentDirectory);
}
}
public static void Go(object instance) {
TestClass m_Test = instance as TestClass;
Console.WriteLine(Environment.CurrentDirectory);
System.IO.Directory.SetCurrentDirectory("L:\\Projects\\");
Console.WriteLine(Environment.CurrentDirectory);
m_Test._ThreadDone.Set();
}
}
I know SOMEONE out there has to have ran across this before!
I'm going to guess what you're trying to do is to make code such as File.Open("Foo.txt") behave differently on different threads. Can you do this? The short answer is No - nor should you be trying to do this. On Windows, the current working directory is set at the process level. The .NET framework does not violate that rule.
A better approach would be to create an abstraction on top of Environment.CurrentDirectory that is thread specific. Something like:
public static class ThreadEnvironment
{
[ThreadStatic]
static string _currentDir;
public static string CurrentDirectory
{
get
{
if (_currentDir == null) // If Current Directory has not been set on this thread yet, set it to the process default
{
_currentDir = Environment.CurrentDirectory;
}
return _currentDir;
}
set
{
if (value == null)
throw new ArgumentException("Cannot set Current Directory to null.");
_currentDir = value;
}
}
}
You can then refer to ThreadEnvironment.CurrentDirectory to get that thread's current directory, which will default to the process directory if it has not been set on that thread. For example:
static void Main(string[] args)
{
(new Thread(Thread1)).Start();
(new Thread(Thread2)).Start();
}
static void Thread1()
{
Console.WriteLine("Thread1 Working Dir is: {0}", ThreadEnvironment.CurrentDirectory);
ThreadEnvironment.CurrentDirectory = #"C:\";
Console.WriteLine("Thread1 Working Dir is: {0}", ThreadEnvironment.CurrentDirectory);
}
static void Thread2()
{
Console.WriteLine("Thread2 Working Dir is: {0}", ThreadEnvironment.CurrentDirectory);
ThreadEnvironment.CurrentDirectory = #"C:\Windows";
Console.WriteLine("Thread2 Working Dir is: {0}", ThreadEnvironment.CurrentDirectory);
}
You would, of course, then need to qualify that path whenever dealing with file IO, however this is arguably a safer design anyway.
has anyone figured out a way to MAKE it possible?
It's simply not possible. You can't even have different working directories per App Domain.
The windows rule is: one Environment set per Process. Running in .NET won't change the basic rules.
Instead of that, if you experienced problem in loading assemblies, consider adding the corresponding folder to the PATH environment variable.
I'm using Fluent NHibernate to run in-memory database tests (MS Test) using SQLite 1.0.66.0:
[TestClass]
public abstract class InMemoryDatabaseTest
{
private NHibernate.Cfg.Configuration configuration;
private ISessionFactory sessionFactory;
[TestInitialize]
public void Initialize()
{
// All "CreateConfiguration" does is load FNh mappings.
this.configuration = new NhConfigurationBuilder()
.CreateConfiguration()
.Database(() => SQLiteConfiguration.Standard.InMemory())
.BuildConfiguration();
this.sessionFactory = this.configuration.BuildSessionFactory();
}
[TestCleanup]
public void Cleanup()
{
new SchemaExport(this.configuration).Drop(false, true);
sessionFactory.Dispose();
}
protected ISession CreateSession()
{
var session = this.sessionFactory.OpenSession();
// Re-create the database every time a new session is created.
new SchemaExport(this.configuration)
.Execute(script: false, export: true, justDrop: false, connection: session.Connection, exportOutput: null);
session.BeginTransaction();
return session;
}
}
And then using this as an example:
[TestClass]
public class MessagesControllerTests : InMemoryDatabaseTest
{
[TestMethod]
public void SQLite_should_have_all_handles_released()
{
using (var session = this.CreateSession())
{
// Don't even need to do anything.
}
}
}
After running this test, I try to Clean the whole solution. The results are as follows:
When running this test (CTRL + R, CTRL + T), the clean is able to succeed as expected.
When debugging this test in (CTRL + R, T), the clean fails with the error: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3607,9): warning MSB3061: Unable to delete file "PathToProject\bin\Debug\System.Data.SQLite.DLL". Access to the path 'PathToProject\bin\Debug\System.Data.SQLite.DLL' is denied.
My first thought was ok, delete the DLL. When I try to, I'm prompted that QTAgent32.exe is currently using the DLL. I used Process Explorer to verify this. For some reason the ms test runner is keeping a handle on the DLL. I've tried modifying the Cleanup metehod with some suggestions from another question, but it still didn't work:
[TestCleanup]
public void Cleanup()
{
new SchemaExport(this.configuration).Drop(false, true);
sessionFactory.Close();
sessionFactory.Dispose();
SQLiteConnection.ClearAllPools();
GC.Collect();
}
I've been able to reproduce this on 3 different machines. Any know method to solve this issue would be greatly appreciated.
Update: I've cleaned up some linguistic confusion. The actual solution configuration can be in Debug/Relase. However, running the tests versus debugging the tests causes the difference in error messages.
I was continually having a similar problem (SQLite.dll being locked by the Visual Studio test runner, although in Debug mode mostly and using MbUnit tests) and found that using TestDriven.net to run my tests solved the problem for me. I never looked any further into the MSTest runner after this, sorry.