FunctionWithCollation unit test fails with System.Data.Sqlite - c#

I downloaded SqlLite 3.7.3 and allegedly the best .NET wrapper for this database engine. I put them in the same directory and ran their provided test.exe.
Most tests succeeded, but two failed. FunctionWithCollation failed with SQLite error "no such function: CHARINDEX" and FunctionWithCollation2 failed with "The method or operation is not implemented".
The support site is silent and so is google. Does anybody have an idea what that means, and how if at all should I proceed?

First of all it would be very good to mention what are FunctionWithCollation and FunctionWithCollation2 do so it would be easier to see what happens.
AFAIK sqlite3 does not support collation built-in as it is quite hard thing to do and allows you to plug in your own collation functionality: http://www.sqlite.org/c3ref/create_collation.html, AFAIK there are some ICU based collation providers.
So check this direction if it does not require you do add some plugin or so.
EDIT: From quick glance to the source code, the unit tests do some testing on charindex function. This function is extension, so:
It seems a problem with either build or with the program or combination of them
I don't think you should care unless you are using extensions. See: http://www.sqlite.org/contrib
In any case I would suggest filling a bug.

Related

Use Prolog with C# (Unity)

I would like to know if there is an "easy" way to call Prolog (preferably SWI-Prolog) code from C# code (in a Unity context).
I have already tried to use the SWI-Prolog - C# interface (source here) however it is not well maintained and I can't make it work with the last version of SWI-Prolog 64 bits (8.2.4 as of today).
I have also seen that it is possible to send information using an SWI-Prolog socket and a C# socket (or Unity Socket) but I can't make it work, and I am not sure using socket is the best idea for the performance.
The only solution working for me was using another "custom" prolog engine : CSharpProlog. This allows me to integrate Prolog code in CSharp files. Yet I would like to use SWI-Prolog if possible, do anyone knows how I can do that ?
From the link you provided, is not apparent that the source is available here.
Just try to compile it, to fit your target machine architecture.
I'm trying to generate the interface on Linux, with MonoDevelop, but it's not immediate, will retry later. Meanwhile, you could try on Windows. Probably will be simpler...
edit
Now I had the time to attempt to compile from source in Windows 10 64 bits, and so far it seems to run pretty well. I'm using VisualStudio Community 2017, and after opened the solution (contrib-swiplcs\SwiPlCs_git.sln), have defined _PL_X64 in SwiPLcs properties - compilation.
HTH
edit
Have uploaded this repo that shows a simplified usage of SWIPlCs.
Note there is a directory (swipl_cs) where I have simply copied (unchanged) the indispensable source files from SwiPlCs.
There is a single Prolog file, that computes N-Queens problem solutions (just the first, to keep things as simple as possible) and displays the steps performed by CLP(FD) - thanks to Markus Triska for the original implementation.

License error using Benchmark.NET + DevArt dotConnect for PostgreSQL

I'm working on an application consisting of several projects and using EntityFramework with dotConnect to run against PostgreSQL. I also have a license for dotConnect which successfully works in the main application.
In parallel, I'm crafting a console application(a different solution) using Benchmark.Net to measure the performance of the logic of one of the projects. But every time I run the benchmark I'm getting the error below:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> Devart.Data.PostgreSql.PgSqlException: Assembly that contains embedded dotConnect for PostgreSQL license cannot be used with this application: 0f238e83-669a-46b8-876f-40331880ee79.exe.exe.
Following this instruction, I have already generated licenses.licx through Visual Studio and <exe file>.licenses via lc.exe. But it is still producing the same error.
I'm suspecting that the fact that Benchmark.NET generates its own exe to run the benchmark causing this error but I'm not 100% sure. So I'm looking for a solution if anybody has one?
Thank you
I'm not sure it's a good idea to create a benchmark for code that does database calls etc. You're benchmarking not the code then, but your whole system instead: the file system, the database drivers, possible interop stuff, and so on.
This is not the idea of BenchmarkDotNet. It's actually created for benchmarking of relatively small CPU-bound tasks to find bottlenecks and perform optimizations based on measurements.
However, if you still want to do that, a solution might be to run the benchmark in-process of the console app you've created, without producing special benchmarking assemblies.
To do so, use the [InProcess] attribute. Just apply it to your benchmark class instead of usual job attributes:
[InProcess]
public class TypeWithBenchmarks
{
[Benchmark]
public void BenchmarkedMethod()
{
}
}

Passing commandline argument after service installation using c#

We had a problem with WCF hosting on service in Windows 2003.
We found a command line tool (httpcfg) which helped us to resolve the issue temporarily.
The following command line argument needs to run after the service installation:
httpcfg set urlacl -u http://+:8080/ -a "D:(A;;GX;;;S-1-5-21-490459244-4280451753-3120260354-1829)"
We need to pass this argument via installer using C#
That is via service controller after installer event. How would I do the same in C# using ServiceController?
I can think of two approaches:
Just run httpcfg.exe using System.Diagnostics.Process
Use PInvoke to access the underlying Win32 API HttpSetServiceConfiguration
The second approach is a little messy but not that hard. The PInvoke.net page describes the call and even gives a working example:
PInvoke.net: httpsetserviceconfiguration (httpapi)
EDIT: Some notes about cleanup issues inspired by Rob's comment. I put them here in this existing answer, because they don't make a good one on their own.
Please note, that whatever approach you take (httpcfg.exe or direct use of the HTTP-API), make really sure that you unregister the URLs in your uninstall process.
The reason is, that HTTP.SYS (which actually is a kernel component) has only so much (nonpaged) memory available for managing URL reservations. If you exceed this, by having to many (possibly stale / left-over) registrations, you get errors and cannot register any new URLs (see http://support.microsoft.com/kb/824033).
While this is "solvable" by manually using httpcfg.exe to cleanup registrations you think(!) are orphaned (or allowing more memory - careful!) it is a royal pain and totally unnecessary, if uninstallers work as suggested.

Preprocessing C# - Detecting Methods

I require the ability to preprocess a number of C# files as a prebuild step for a project, detect the start of methods, and insert generated code at the start of the method, before any existing code. I am, however, having a problem detecting the opening of a method. I initially tried a regular expression to match, but ended up with far too many false positives.
I would use reflection, but the MethodInfo class does not reference the point in the original source.
EDIT: What I am really trying to do here is to support pre-conditions on methods, that pre-condition code being determined by attributes on the method. My initial thought being that I could look for the beginning of the method, and then insert generated code for handling the pre-conditions.
Is there a better way to do this? I am open to creating a Visual Studio Addin if need be.
This is a .NET 2.0 project.
Cheers
PostSharp or Mono.Cecil will let you do this cleanly by altering the generated code without getting into writing a C# parser which is unlikely to be core business for you...
Havent done anything of consequence with PostSharp but would be guessing its more appropriate than Mono for implementing something like preconditions or AOP. Alternately you might be able to do something AOPy with a DI container like Ninject
But of course the applicability of this idea Depends - you didnt say much other than that you wanted to insert code at the start of methods...
EDIT: In light of your desire to do preconditions... Code Contracts in .net 4 is definitely in that direction.
What sort of a tool do you have? Whats wrong with having a single Mono.Cecil.dll DLL shipped? Either way something other than a parser is the tool for the job.
I am sure there is an easier way but this might be a good excuse to take MGrammer for a spin.

Error logging in C#

I am making my switch from coding in C++ to C#. I need to replace my C++ error logging/reporting macro system with something similar in C#.
In my C++ source I can write
LOGERR("Some error");
or
LOGERR("Error with inputs %s and %d", stringvar, intvar);
The macro & supporting library code then passes the (possibly varargs) formatted message into a database along with the source file, source line, user name, and time. The same data is also stuffed into a data structure for later reporting to the user.
Does anybody have C# code snippets or pointers to examples that do this basic error reporting/logging?
Edit: At the time I asked this question I was really new to .NET and was unaware of System.Diagnostics.Trace. System.Diagnostics.Trace was what I needed at that time. Since then I have used log4net on projects where the logging requirements were larger and more complex. Just edit that 500 line XML configuration file and log4net will do everything you will ever need :)
Lots of log4net advocates here so I'm sure this will be ignored, but I'll add my own preference:
System.Diagnostics.Trace
This includes listeners that listen for your Trace() methods, and then write to a log file/output window/event log, ones in the framework that are included are DefaultTraceListener, TextWriterTraceListener and the EventLogTraceListener. It allows you to specify levels (Warning,Error,Info) and categories.
Trace class on MSDN
Writing to the Event Log in a Web Application
UdpTraceListener - write log4net compatible XML messages to a log viewer such as log2console
I would highly recommend looking at log4Net. This post covers the majority of what you need to get started.
Another good logging library is NLog, which can log to a lot of different places, such as files, databases, event logger etc.
I use The Object Guy's Logging Framework--as do most people who try it. This guy has some interesting comments about it.
Enterprise Library is a solid alternative to log4net and it offers a bunch of other capabilities as well (caching, exception handling, validation, etc...). I use it on just about every project I build.
Highly recommended.
Even though I personally hate it, log4net seems to be the de facto standard for C# logging. Sample usage:
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Error(“Some error”);
log.ErrorFormat("Error with inputs {0} and {1}", stringvar, intvar);
As I said in another thread, we've been using The Object Guy's Logging Framework in multiple production apps for several years. It's super easy to use and extend.
Log4Net is a rather comprehensive logging framework that will allow you to log to different levels (Debug, Error, Fatal) and output these log statements to may different places (rolling file, web service, windows errors)
I am able to easily log anywhere by creating an instance of the logger
private static readonly ILog _log = LogManager.GetLogger(typeof([Class Name]));
and then logging the error.
_log.Error("Error messsage", ex);
Serilog is late to the party here, but brings some interesting options to the table. It looks much like classical text-based loggers to use:
Log.Information("Hello, {0}", username);
But, unlike earlier frameworks, it only renders the message and arguments into a string when writing text, e.g. to a file or the console.
The idea is that if you're using a 'NoSQL'-style data store for logs, you can record events like:
{
Timestamp: "2014-02-....",
Message: "Hello, nblumhardt",
Properties:
{
"0": "nblumhardt"
}
}
The .NET format string syntax is extended so you can write the above example as:
Log.Information("Hello, {Name}", username);
In this case the property will be called Name (rather than 0), making querying and correlation easier.
There are already a few good options for storage. MongoDB and Azure Table Storage seem to be quite popular for DIY. I originally built Serilog (though it is a community project) and I'm now working on a product called Seq, which provides storage and querying of these kinds of structured log events.
You can use built in .NET logging. Look into TraceSource and TraceListeners, they can be configured in the .config file.
Ditto for log4net. I'm adding my two bits because for actual use, it makes sense to look at some open source implementations to see real world code samples with some handy additions. For log4net, I'd suggest off the top of my head looking at subtext. Particularly take a look at the application start and assemblyinfo bits.
Further to the couple of comments realting to the use of the System.Diagnostics methods for logging, I would also like to point out that the DebugView tool is very neat for checking debug output when needed - unless you require it, there is no need for the apps to produce a log file, you just launch DebugView as and when needed.
The built in tracing in System.Diagnostics is fine in the .NET Framework and I use it on many applications. However, one of the primary reasons I still use log4net is that the built in .NET Framework tracing lacks many of the useful full featured appenders that log4net already supplies built in.
For instance there really isn't a good rolling file trace listener defined in the .NET Framework other than the one in a VB.NET dll which really is not all that full featured.
Depending on your development environment I would recommend using log4net unless 3rd party tools are not available, then I'd say use the System.Diagnostics tracing classes. If you really need a better appender/tracelistener you can always implement it yourself.
For instance many of our customers require that we do not use open source libraries when installed on their corporate machines, so in that case the .NET Framework tracing classes are a perfect fit.
Additionally - http://www.postsharp.org/ is an AOP library I'm looking into that may also assist in logging as demonstrated here on code project:http://www.codeproject.com/KB/dotnet/log4postsharp-intro.aspx.
ExceptionLess is one of the easiest nuget package available to use for logging. Its an open source project. It automatically takes care of unhandled exception, and options for manually logs are available. You can log to online or self host on local server.
Log4Net, as others have said, is fairly common and similar to Log4j which will help you if you ever do any Java.
You also have the option of using the Logging Application Block http://www.codeproject.com/KB/architecture/GetStartedLoggingBlock.aspx

Categories