Archive for the ‘.NET 4.5’ Category
Posted by jpluimers on 2013/09/04
Lots of people are confused about the different time out options you can set in ADO.NET.
Thanks NinethSense for explaining this well for the SqlClient DAL. Other DALs work in a similar way.
- SqlCommand.CommandTimeout = timeout limit for your SQL query. Means, how much time a (eg: SELECT, UPDATE) query can take for its execution. If it exceeds SqlCommand.CommandTimeout, then it stops execution. A command timeout error will occur.
- SqlConnection.ConnectionTimeout = timeout limit for your connection. Means, how much time your connection object can try to connect. If it exceeds the specified time, it stops connecting. A connection timeout error will occur.
–jeroen
via: .net – What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout? – Stack Overflow.
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Software Development | 1 Comment »
Posted by jpluimers on 2013/09/03
A while ago, I inherited a .NET project that used the EnterpriseLibrary, the original developers (it got developed around 2003) were gone, and every exception would end up in this:
SecurityException: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.
Searching for the combination “EnterpriseLibrary” “The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.” revealed only 1 result:
patterns & practices – Enterprise Library – View Discussion: Logging Error of FormattedEventLogTraceListener in Win7.
Even though I didn’t want anything to be logged in the EventLog, I wanted text logs, and looking at the .config files in the main project, the Enterprise Library logging setup was correct (right sinks, etc).
Somehow, the EnterpriseLibrary still insisted on writing to the eventlog, and this particular exception was the only one tricking to the unhandled exception layer…
For logging in the Eventlog, that particular event log must exist. In order to create an eventlog, you need to have administrator access. And here is the crux: apparantly, the original developers were (while working on Windows NT 4) all Administrators. So they never noticed this problem (and maybe never even looked for that log). After having the Visual Studio debugger break on all CLR exceptions, not only the unhandled ones, I could see this one shown below fired deep inside the EnterpriseLibrary. Which means that the original developers:
- did add the EnterpriseLibrary V2 (yes, this project still had V2 and part of the config files were V1.1!) configuration files to the main project:
- Had forgotten to mark these files as “Copy to Output Directory” to have the value “Copy if Newer” or “Copy Always”
Marking the files as such solved the below exception, and now on my todo list is to make the old V1.1 stuff go away, and migrate to the most current Enterprise Library. Read the rest of this entry »
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, EnterpriseLibrary, Software Development | Leave a Comment »
Posted by jpluimers on 2013/08/29
When you have a layered exception handling (for instance to translate general exceptions into domain or business exceptions, and want to control which exceptions trickle up to where), then from a debugger perspective, most exceptions actually handled.
However debugging those layers, it often makes sens to be able to break where all these exceptions are actually fired.
The screenshots (click on each to enlarge) are for Visual Studio 2010, but it works in any Visual Studio version and (since it is a debugger feature, not a language one) for all .NET languages I tried so far.
Note that as of Visual Studio 2010, if you disable these, it still breaks when exceptions are thrown from code called through reflection. This seems intentional and has 3 workarounds, but it might have been reverted in Visual Studio 2012.
This is a setting stored on the Solution level (.suo file) in Visual studio which by default is turned off. Luckily, it is very easy to turn this feature on, for instance for CLR (.NET Common Language Runtime) exceptions:
- In the “Debug” menu, choose “Exceptions” (or Press Ctrl+D, E),
- Wait a few moments for the dialog to appear
- Put a checkmark in the “Thrown” column for the “Comon Language Runtime Exceptions” row.
- Click the “OK” button. Read the rest of this entry »
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, F#, Prism, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | 1 Comment »
Posted by jpluimers on 2013/08/28
Every once in a while you are in situation where you are not allowed to use SQL Server Profiler, nor to see any query plans, but you still want see the SQL going from your .NET apps to the database server.
With that SQL, you can feed it through your favourite database tool, and see where the culprit is.
There are various ways of getting rudimentary or a bit more advanced SQL out of this. Flapper posted a solution that is specific for SQL Server (and requries both ObjectExtensions.cs and StringExtensions.cs from the DotNetX library), but posted more ready to use SQL.
I opted – and wanted to hank Justin Harris – for this piece of code on StackOverflow, which works on any IDbCommand (so you can use it for any ADO.NET data provider, like SQL Server, OLE DB, Oracle, etc):
While you will not be able to plug is into something like Enterprise Manager to run it works for logging.
public static string ToReadableString(this IDbCommand command)
{
StringBuilder builder = new StringBuilder();
if (command.CommandType == CommandType.StoredProcedure)
builder.AppendLine("Stored procedure: " + command.CommandText);
else
builder.AppendLine("Sql command: " + command.CommandText);
if (command.Parameters.Count > 0)
builder.AppendLine("With the following parameters.");
foreach (IDataParameter param in command.Parameters)
{
builder.AppendFormat(
" Paramater {0}: {1}",
param.ParameterName,
(param.Value == null ?
"NULL" : param.Value.ToString())).AppendLine();
}
return builder.ToString();
}
answered Apr 12 ’10 at 20:26; juharr
You saved my day! Not being allowed to use the profiler, this is a great way to get the actual SQL, then run it from SSMS or the Enterprise Manager. – Jeroen Wiert Pluimers
I pasted it in a DataExtensions class like this: Read the rest of this entry »
Posted in .NET, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/08/14
Everyone knows there is a size difference between a gigabyte of memory, and a gigabyte of disk space.
The former is 102410241024, the latter is 100010001000.
To facilitate this, I’ve created a C# class UnitPrefixes containing quite a few constants and readonly values.
The class is below, but a few interesting facts first:
- Most values are const, but a few are readonly static variables because they cannot calculated at compile time (the C# compiler by design does very limited calculations at compile time; it is complex enough as it already is).
As Jon Skeet explains, there are some other differences between const and readonly static, which is why I favour const.
- Though all consts are positive, I could have used UInt32 and UInt64, but the .NET framework favours signed Int32 and Int64 types for parameters, so to avoid casting, I used the signed ones.
- There is no Int128 or UInt128, but there is System.Numerics.BigInteger which I use for values too large for 64-bit integers.
Note that BigInteger is relatively new, so this code will only work in C# 4 or higher, and requires .NET 4 or higher.
This is also the place where I use the public readonly static fields, as I need to call the BigInteger constructor to initialize it.
- I used the Decimal type, as the mantissa holds up to 28 digits of accuracy.
I used the Wikipedia pages Binary Prefix and Metric Prefix (I could also have used File Size) for the unit names and abbreviations.
Note that BitsPerByte is a const I needed too, and I will probably add constants for 512 and 4096, as you see those often in computing as well.
The below sample code is also available as a changeset on BeSharp.CodePlex.com. Read the rest of this entry »
Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, Conference Topics, Conferences, Development, Event, Jon Skeet, Software Development | Tagged: endregion, region | Leave a Comment »
Posted by jpluimers on 2013/08/08
Peter Leslie Morris asked if Delphi already incorporates the `yield` keyword that C# had introduced in C# 2.
Delphi doesn’t, but for the people interested what it does in C#:
Basically `yield` is syntactic sugar to make it a lot easier to write methods that return enumerators of some sort.
It delays (hence the yield keyword) execution until the enumerator as actually being used.
It is one of the hardest C# things to master (it is the most complicated transformation in the compiler, followed by anonymous methods – well maybe with the exception of async/await), but it can be very useful.
VB.NET doesn’t have it either (thanks André!) has it too, but and also has iterator blocks.
Some start posts on yield:
–jeroen
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Development, Software Development | 10 Comments »
Posted by jpluimers on 2013/08/06
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2010, Visual Studio and tools | 7 Comments »
Posted by jpluimers on 2013/08/03
If you ever get something similar to
InvalidCastException: [A]Person cannot be cast to [B]Person.
then you are (or more precise: your process is) probably loading the same assembly twice, but from a different context.
.NET allows that, and for reason (side by side loading, appdomains, etc).
Delphi doesn’t (thanks Warren Postma for noticing). But a mistake you see quite often is that Delphi developers try to load the same Delphi type from both an EXE and a DLL wondering why they don’t match. If you want to spread your types in Delphi, then use BPLs (run-time packages) for that. BPLs are similar to .NET assemblies, but cannot be loaded in memory twice.
–jeroen
via c# – InvalidCastException for two Objects of the same type – Stack Overflow.
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 8.0, VB.NET 9.0, Visual Studio 11, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »