Elasticsearch and .NET - c#

We're considering to switch from Solr/Solr.net to Elasticsearch. We started with NEST. We have only 4 documents in search index.
private static void Main(string[] args)
{
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node, "my-application");
var client = new ElasticClient(settings);
var stopwatch = Stopwatch.StartNew();
var sr = client.Get<Movie>(1);
Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
The code above takes approx. 250ms, while the same code with HttpClient and JsonSerializer takes 30-45ms. 250ms is too much time for just 4 documents.
Can NEST be used on high-traffic news website, or do you recommend HttpClient + JsonSerializer combo? The search page was the most visited page on our website in 2013.
Thanks in advance.

There are two things that have to happen in order for NEST to make the first request.
The Json Serializer (Json.net) in this case has to cache the type so that it knows how to serialize and deserialize the object you are sending back and forth.
Nest has its own fluent language for queries that must be translated from the intial types that represent the fluent query language and the delivered as JSON to elastic search. These document types also must be learned by the Json Serializer.
The HTTP client has to be spun up to make the request.
I currently have over 4M documents in a single index that I use with NEST and my searches from server all the way to client over the internet are taking 50-70 ms using NEST. Like you, however, after a cold start the first request is slow.

I suggest you to use https://github.com/ServiceStack/ServiceStack.Text,
the fastest Json serializer for C#.
For the driver, use the low-level one, http://nest.azurewebsites.net/elasticsearch-net/quick-start.html
below the code I started to write to log my apps in detail and analyze them. Still have to work, but can be a good beginning.
using System;
using System.Configuration;
using Elasticsearch.Net;
using Elasticsearch;
using Elasticsearch.Net.Connection;
using Elasticsearch.Net.ConnectionPool;
namespace Common {
/// <summary>
/// Elastic search. Singletone, open connection and thread safe to be open for all the time
/// the app is running, so we send ours logs to ealsticsearch to be analyzed, assychronly
/// See the fourth version;
/// http://csharpindepth.com/articles/general/singleton.aspx
/// </summary>
public sealed class ElasticSearch {
// our instance of ourself as a singleton
private static readonly ElasticSearch instance = new ElasticSearch();
ElasticsearchClient client;
string connectionString = ConfigurationManager.ConnectionStrings["Elasticsearch"].ConnectionString;
/// <summary>
/// Initializes a new instance of the <see cref="Common.ElasticSearch"/> class.
/// Follow this: http://nest.azurewebsites.net/elasticsearch-net/connecting.html
/// We use a ConnectionPool to make the connection fail-over, that means, if the
/// connection breaks, it reconnects automatically
/// </summary>
private ElasticSearch() {
var node = new Uri(connectionString);
var connectionPool = new SniffingConnectionPool(new[] { node });
var config = new ConnectionConfiguration(connectionPool);
client = new ElasticsearchClient(config); // exposed in this class
}
static ElasticSearch() {
}
/// <summary>
/// Gets the instance of our singleton class
/// </summary>
/// <value>The instance.</value>
public static ElasticSearch Instance {
get {
return instance;
}
}
/// <summary>
/// Log the specified module, id and json.
/// </summary>
/// <param name="type">Here the entity you want to save your log,
/// let's use it based on classes and StateMachines</param>
/// <param name="id">Identifier. alwayes the next</param>
/// <param name="json">Json.</param>
public void Log(string type, string id, string json) {
client.Index("mta_log", type, id, json);
}
}
}

Related

Using VST.net with Unity3d to create a simple VST host

I've successfully simplified the Vst.net host sample to directly load a vst instrument. Mostly I've just stripped out the GUI and made it automatically fire a few test notes. This code works when I build it as a console application.
using System;
using Jacobi.Vst.Core;
using Jacobi.Vst.Interop.Host;
using NAudio.Wave;
using CommonUtils.VSTPlugin;
namespace Jacobi.Vst.Samples.Host
{
///<Summary>
/// Gets the answer
///</Summary>
public class pianoVST
{
///<Summary>
/// Gets the answer
///</Summary>
public static VST vst = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
}
/// <summary>
/// Play some test notes.
/// </summary>
public void playTest()
{
var asioDriverNames = AsioOut.GetDriverNames();
if (asioDriverNames.Length > 0)
{
MidiVstTest.UtilityAudio.OpenAudio(MidiVstTest.AudioLibrary.NAudio, asioDriverNames[0]);
MidiVstTest.UtilityAudio.StartAudio();
vst = MidiVstTest.UtilityAudio.LoadVST("[path-to-vst-dll]");
for (int i = 0; i < 3; i++)
{
vst.MIDI_NoteOn(60, 100);
System.Threading.Thread.Sleep(1000);
vst.MIDI_NoteOn(60, 0);
System.Threading.Thread.Sleep(1000);
}
}
}
}
}
However, when I build this as a dll, import it into Unity and then attach it to a simple GameObject, I'm not able to get it to run or build. The error message I get is:
ArgumentException: The Assembly Jacobi.Vst.Interop is referenced by Jacobi.Vst.Samples.Host ('Assets/Jacobi.Vst.Samples.Host.dll'). But the dll is not allowed to be included or could not be found.
I've rebuilt the C++ interop dll from source but nothing I do makes it work with Unity.
Is there something I can do to make the Jacobi.Vst.Interop dll work nicely with Unity?
VST.Net has a dependency on the VC110.CRT package. That error could be a result of not have the VC runtime installed.
https://github.com/obiwanjacobi/vst.net/blob/master/docs/Using_VST.NET.md

How to connect to MongoDB via the .Net driver?

I'm following this example, ClientMongo to connect a WPF application to my MongoDB database via the connection string. But I get an error on the MongoClient when I call the GetServer method. The error states that GetServer doesn't exist, although the correct using references and usings have been added.
Can anyone spot if I've missed a step in setting this up? Or is there an alternative solution to create a connection with the remote DB?
This is the code I've used to connect, similar to the example above. The user and password have been starred out for privacy:
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDBApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string connectionString = "mongodb://<brian****>:<********123;>#ds048878.mongolab.com:48878/orders";
public MainWindow()
{
InitializeComponent();
var mongoUrl = MongoUrl.Create(connectionString);
var server = new MongoClient(connectionString).GetServer();
return server.GetDatabase(mongoUrl.DatabaseName);
}
}
}
If you are using the 2.x Version of the C# driver, forget about the Server object.
You can get your Database directly from the client:
var client = new MongoClient("<connectionString>");
return this.Client.GetDatabase("<databaseName>");

Retrieving last commit ID / revision number from a deployed application

I would like to have a function returning the last revision / commit ID of the source from which the deployed application / library was built.
I have been using the __DATE__ and __TIME__ macros in C++ but this approach has obvious drawbacks.
What tools are available to achieve this in an automated manner?
I am using Eclipse-CDT for C++ (Linux, SVN) but it I am also interested in git solutions, and sources written in Java and C#.
By using Visual Studio and C# you could within a Pre-Built step calling the SubWCRev command with a template file, that will be copied to a file used within the solution.
The command within the Pre-Built step is:
<PreBuildEvent>SubWCRev "$(ProjectDir)\" "$(ProjectDir)VersionProvider.template.cs" "$(ProjectDir)VersionProvider.cs"</PreBuildEvent>
Within the project add the following two files:
<Compile Include="VersionProvider.cs">
<DependentUpon>VersionProvider.template.cs</DependentUpon>
</Compile>
<None Include="VersionProvider.template.cs" />
With this content:
internal static class VersionProvider
{
/// <summary>
/// Provides the current subversion revision number
/// </summary>
internal const string CurrentSVNRevision = "$WCREV$";
}
Last but not least within AssemblyInfo.cs add the following line:
[assembly: AssemblyInformationalVersion(VersionProvider.CurrentSVNRevision)]
The project will by this way automatically get the current subversion revision number of this project folder baked into version info of the application which can be seen on the details page of the file properties.
You can also retrieve this information through code at runtime:
private string GetAdditionalVersionInfo()
{
var assembly = Assembly.GetEntryAssembly();
var attributesFound = assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), true);
var version = attributesFound.OfType<AssemblyInformationalVersionAttribute>().FirstOrDefault();
return version != null ? version.InformationalVersion : String.Empty;
}
/// <summary>
/// Gets the SVN date.
/// </summary>
/// <value>The SVN date. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnDate{
// ReSharper restore UnusedMember.Global
get {
const string S = "$Date$";
return S.Substring(7, 19);
}
}
/// <summary>
/// Gets the SVN ID.
/// </summary>
/// <value>The SVN ID. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnID{
// ReSharper restore UnusedMember.Global
get{
const string S = "$Id$";
const string D = "$";
return S.Replace(D + "Id: ", string.Empty).Replace(" " + D, string.Empty);
}
}
/// <summary>
/// Gets the SVN rev.
/// </summary>
/// <value>The SVN rev. value</value>
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBePrivate.Global
public static new string SvnRev{
// ReSharper restore MemberCanBePrivate.Global
// ReSharper restore UnusedMember.Global
get {
const string S = "$Rev$";
return S.Item(1);
}
}
/// <summary>
/// Gets the SVN author.
/// </summary>
/// <value>The SVN author. value</value>
// ReSharper disable UnusedMember.Global
public static new string SvnAuthor{
// ReSharper restore UnusedMember.Global
get {
const string S = "$Author$";
return S.Item(1);
}
}
Note S.Item(n) returns a space delimited zero based item. Replace with your own code. Also, new is only necessary to deal with inheritance. Props must be set in svn. I dont have a snippet showing the expansions since I am on Mercurial now.
If you were working with a Makefile and SVN, you could do something like this for C or C++:
REVISION=$(shell svnversion)
CFLAGS=... -D__REVISION__=\"$REVISION\"
I'm not that familiar with CDT, but I understand it's able to use a Makefile-based build system.

Creating persistent RAS connection with opennetcf Ras in Windows CE 6.0

I'm in need to create an GPRS connection in an PDA that has windows ce 6. Now normally i would had to use the manufacturer's dll to create that, but they said that they use ras to accomplish this. The only problem of using that is that i program in .net c#, and the library is an unmanaged code one.
Fortunately i came by the opennetcf ras library that does already the necessary pInvokes for the windows ras library, the only problem being the poor documentation.
I created then an library that would call and set-up the necessary GPRS connection on windows. I'm using an Portuguese telecom operator that uses the following definitions:
Operator Name: Optimus P
Apn: umts
Password: *******
User: ******
Consulting the gsm module definition, i had the following modem settings:
Connection Name: GPRS
Device: Hayes Compatible on COM1:
Baund Rate:115200
Data Bits: 8
Parity:1
Stop Bits: 1
Flow Control: Hardware
and of course the extra settings (or how i call it the atCall)
+cgdcont=1, "ip", "umts"
This settings when i use the control panel and do an connect with that profile, it connects and i'm able to call all the webservices without an error. It also shows an extra profile for the modem that shows the settings for the device, incluid the ipaddress, subnet mask and even the default gateway.
The problem is that when i use the library that i created to create an gprs connection programatically, and then call the webservices at some point it throws me an web exception : The remote name could not be resolved. I also checked and the extra icon does not appear, but if i see the GPRS status it appears as it is connected.
The code that create, destroys and query if it exists an connection is as follows:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Net;
using OpenNETCF.Diagnostics;
namespace gsmAdapterNet
{
/// <summary>
/// GPRS Connection class
/// </summary>
public class GPRS
{
private static string connectionName = "GPRS";
/// <summary>
/// Connects the GPRS.
/// </summary>
/// <returns></returns>
public static bool ConnectGPRS()
{
//precisamos de obter as connecoes e ligar
RasEntryCollection connecoesPossiveis = Ras.Entries;
RasEntry _currentEntry = connecoesPossiveis[connectionName];
_currentEntry.RasStatus += new RasNotificationHandler(RasStatusHandler);
RasError resultado = _currentEntry.Dial(false);
if (resultado == RasError.Success)
return true;
else
return false;
}
static void RasStatusHandler(int hConn, RasConnState State, RasError ErrorCode)
{
Logger.WriteLine("");
Logger.WriteLine("RAS STATUS: " + ErrorCode.ToString() + " , State: " + State.ToString());
}
/// <summary>
/// Disconnects the GPRS.
/// </summary>
/// <returns></returns>
public static void DisconnectGPRS()
{
RasEntryCollection entradas = Ras.Entries;
foreach (RasEntry possivelEntrada in entradas)
{
if (possivelEntrada.Name == connectionName)
{
possivelEntrada.Hangup();
}
}
}
/// <summary>
/// Determines whether this instance is connected.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is connected; otherwise, <c>false</c>.
/// </returns>
public static bool isConnected()
{
RasConnection[] conecoes = Ras.ActiveConnections;
foreach (RasConnection conecao in conecoes)
{
if (conecao.Name == connectionName)
return true;
}
return false;
}
/// <summary>
/// Dumps the ras entries.
/// </summary>
public static void DumpRasEntries()
{
foreach (RasEntry entry in Ras.Entries)
{
Logger.DumpRasEntry(entry);
}
}
}
}
So resuming the question is how i can create an viable connection with the opennetcf ras library
Best Greetings
It seems as if the network interface for the GPRS connection that you get when dialing in is not configured with the correct DNS servers. Alternatively, the domain names needed for your service calls may be wrong.
To verify this:
Is it only a specific web service whose domain name cannot be resolved? Is it always the same? Do others work? Can you simply HTTP GET something like http://stackoverflow.com programmatically after the connection has been established?

NUnit with an ASP.NET web site

I'm currently trying to upgrade our build server at work, going from having no build server to having one!
I'm using JetBrains' TeamCity (having used ReSharper for a couple of years I trust their stuff), and intend to use NUnit and MSBuild.
However, I've come up with an issue: it appears that it is not possible to test an ASP.NET web site with NUnit. I had assumed it would be possible to configure it to test App_Code after a build, however it seems that the only way to do tests nicely is through converting the web site to a web application (which my boss does not like the idea of).
How could I go about this?
Please bear in mind that the testing needs to be able to be fired automatically from TeamCity.
If you want to smoke test your site, or bang on some endpoints - see the code below.
If, on the other hand, you want to test the untestable (detestable) ASP.NET website assembly (as opposed to a web app), you are, as they say in France, S.O.L.
The assembly is a randomly named dynamically compiled assembly deep in the bowels of the framework temporary ASP.NET files, making testing close to impossible.
You really do need to consider a couple of options:
place the logic that needs testing in a seperate assembly.
change to a web application project that delivers a testable assembly.
Sorry, I don't think that you will find what you are looking for, but I could be wrong. Let's see.
Good Luck
Download Visual Studio 2008 sample with site and applicatgion.
I wrote a wrapper for WebHost.WebServer.dll which is the core of the development server that works quite well in CI. I use it all the time.
Here is a scaled down version including a usage example.
Test.cs
using System.Net;
using NUnit.Framework;
namespace Salient.Excerpts
{
[TestFixture]
public class WebHostServerFixture : WebHostServer
{
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
// debug/bin/testproject/solution/siteundertest - make sense?
StartServer(#"..\..\..\..\TestSite");
// is the equivalent of
// StartServer(#"..\..\..\..\TestSite",
// GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
StopServer();
}
[Test]
public void Test()
{
// while a reference to the web app under test is not necessary,
// if you do add a reference to this test project you may F5 debug your tests.
// if you debug this test you will break in Default.aspx.cs
string html = new WebClient().DownloadString(NormalizeUri("Default.aspx"));
}
}
}
WebHostServer.cs
// Project: Salient
// http://salient.codeplex.com
// Date: April 16 2010
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using Microsoft.VisualStudio.WebHost;
namespace Salient.Excerpts
{
/// <summary>
/// A general purpose Microsoft.VisualStudio.WebHost.Server test fixture.
/// WebHost.Server is the core of the Visual Studio Development Server (WebDev.WebServer).
///
/// This server is run in-process and may be used in F5 debugging.
/// </summary>
/// <remarks>
/// If you are adding this source code to a new project, You will need to
/// manually add a reference to WebDev.WebHost.dll to your project. It cannot
/// be added from within Visual Studio.
///
/// Please see the Readme.txt accompanying this code for details.
/// </remarks>
/// NOTE: code from various namespaces/classes in the Salient project have been merged into this
/// single class for this post in the interest of brevity
public class WebHostServer
{
private Server _server;
public string ApplicationPath { get; private set; }
public string HostName { get; private set; }
public int Port { get; private set; }
public string VirtualPath { get; private set; }
public string RootUrl
{
get { return string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}{2}", HostName, Port, VirtualPath); }
}
/// <summary>
/// Combine the RootUrl of the running web application with the relative url specified.
/// </summary>
public virtual Uri NormalizeUri(string relativeUrl)
{
return new Uri(RootUrl + relativeUrl);
}
/// <summary>
/// Will start "localhost" on first available port in the range 8000-10000 with vpath "/"
/// </summary>
/// <param name="applicationPath"></param>
public void StartServer(string applicationPath)
{
StartServer(applicationPath, GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
}
/// <summary>
/// </summary>
/// <param name="applicationPath">Physical path to application.</param>
/// <param name="port">Port to listen on.</param>
/// <param name="virtualPath">Optional. defaults to "/"</param>
/// <param name="hostName">Optional. Is used to construct RootUrl. Defaults to "localhost"</param>
public void StartServer(string applicationPath, int port, string virtualPath, string hostName)
{
if (_server != null)
{
throw new InvalidOperationException("Server already started");
}
// WebHost.Server will not run on any other IP
IPAddress ipAddress = IPAddress.Loopback;
if(!IsPortAvailable(ipAddress, port))
{
throw new Exception(string.Format("Port {0} is in use.", port));
}
applicationPath = Path.GetFullPath(applicationPath);
virtualPath = String.Format("/{0}/", (virtualPath ?? string.Empty).Trim('/')).Replace("//", "/");
_server = new Server(port, virtualPath, applicationPath, false, false);
_server.Start();
ApplicationPath = applicationPath;
Port = port;
VirtualPath = virtualPath;
HostName = string.IsNullOrEmpty(hostName) ? "localhost" : hostName;
}
/// <summary>
/// Stops the server.
/// </summary>
public void StopServer()
{
if (_server != null)
{
_server.Stop();
_server = null;
// allow some time to release the port
Thread.Sleep(100);
}
}
public void Dispose()
{
StopServer();
}
/// <summary>
/// Gently polls specified IP:Port to determine if it is available.
/// </summary>
/// <param name="ipAddress"></param>
/// <param name="port"></param>
public static bool IsPortAvailable(IPAddress ipAddress, int port)
{
bool portAvailable = false;
for (int i = 0; i < 5; i++)
{
portAvailable = GetAvailablePort(port, port, ipAddress, true) == port;
if (portAvailable)
{
break;
}
// be a little patient and wait for the port if necessary,
// the previous occupant may have just vacated
Thread.Sleep(100);
}
return portAvailable;
}
/// <summary>
/// Returns first available port on the specified IP address.
/// The port scan excludes ports that are open on ANY loopback adapter.
///
/// If the address upon which a port is requested is an 'ANY' address all
/// ports that are open on ANY IP are excluded.
/// </summary>
/// <param name="rangeStart"></param>
/// <param name="rangeEnd"></param>
/// <param name="ip">The IP address upon which to search for available port.</param>
/// <param name="includeIdlePorts">If true includes ports in TIME_WAIT state in results.
/// TIME_WAIT state is typically cool down period for recently released ports.</param>
/// <returns></returns>
public static int GetAvailablePort(int rangeStart, int rangeEnd, IPAddress ip, bool includeIdlePorts)
{
IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties();
// if the ip we want a port on is an 'any' or loopback port we need to exclude all ports that are active on any IP
Func<IPAddress, bool> isIpAnyOrLoopBack = i => IPAddress.Any.Equals(i) ||
IPAddress.IPv6Any.Equals(i) ||
IPAddress.Loopback.Equals(i) ||
IPAddress.IPv6Loopback.
Equals(i);
// get all active ports on specified IP.
List<ushort> excludedPorts = new List<ushort>();
// if a port is open on an 'any' or 'loopback' interface then include it in the excludedPorts
excludedPorts.AddRange(from n in ipProps.GetActiveTcpConnections()
where
n.LocalEndPoint.Port >= rangeStart &&
n.LocalEndPoint.Port <= rangeEnd && (
isIpAnyOrLoopBack(ip) || n.LocalEndPoint.Address.Equals(ip) ||
isIpAnyOrLoopBack(n.LocalEndPoint.Address)) &&
(!includeIdlePorts || n.State != TcpState.TimeWait)
select (ushort)n.LocalEndPoint.Port);
excludedPorts.AddRange(from n in ipProps.GetActiveTcpListeners()
where n.Port >= rangeStart && n.Port <= rangeEnd && (
isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
select (ushort)n.Port);
excludedPorts.AddRange(from n in ipProps.GetActiveUdpListeners()
where n.Port >= rangeStart && n.Port <= rangeEnd && (
isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
select (ushort)n.Port);
excludedPorts.Sort();
for (int port = rangeStart; port <= rangeEnd; port++)
{
if (!excludedPorts.Contains((ushort)port))
{
return port;
}
}
return 0;
}
}
}
NOTE: The Microsoft.VisualStudio.WebHost namespace is contained in the file WebDev.WebHost.dll. This file is in the GAC, but it is not possible to add a reference to this assembly from within Visual Studio.
To add a reference you will need to open your .csproj file in a text editor and add the reference manually.
Look for the ItemGroup that contains the project references and add the following element:
<Reference Include="WebDev.WebHost, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=x86">
<Private>False</Private>
</Reference>
Reference: http://www.codeproject.com/KB/aspnet/test-with-vs-devserver-2.aspx

Categories

Resources