Category Archives: C#

Home / C#
48 Posts

(This is a C# LinqPad script)

Using setvar in T-SQL

Finds the SQL Command Variable definitions in a SQL script and parses their names and values.
Finds the SQL Command Variable references and parses their names.
Determines which references have no definitions.
Determines which definitions have no references.

The definition names and reference names are case insensitive.
A definition is in the form of :setvar name “value”
A reference is in the form of $(name)

 

It appears that the virtual machine manager in HyperV (VMM) reports the VM as running though the OS may not yet be fully loaded. We need to wait until the OS is responsive before continuing VM configuration. This observed behavior is more empirical than scientific and requires investigation, but a stop-gap solution is to wait until the VMM reports that the VM is running and then wait for the machine to respond to pings. This code does the second part.

 

Note that this doesn’t work so well with method name obfuscation!

Results
[Operations]
Foo.DoSomething : something 1
Foo.DoSomethingElse : something else 1
Foo.DoSomething : something 2

WCF throws System.ServiceModel.FaultExceptions (FE) between the client and server. The FE is a System.Exception so the client does not need to explicitly catch FaultException, but the FE is missing something. It does not take an InnerException in its constructor, so the server-side chain of exceptions and stack trace data are not available to the client. The typical response to this is “That’s the way it’s supposed to work!”, but if you own/control the consumers of your web service and you want the exception chain and stack trace data, then you’ll want to use FaultException.

The first thing is to decorate your service operations with the FaultContract attribute:

By default, the service implementation will throw all unhandled exceptions back to the client as a regular FaultException. To avoid this, catch exceptions and re-throw them as FaultException as such…

Great! All that detail is on its way to your client.

FaultException and FaultException<>; are derived from System.Exception, so when you don’t care about the detail, just catch Exception. When you want the detail, catch FaultException and inspect …

An instance of ExceptionDetail stores a copy of the underlying Exception data as well as the Exception’s InnerException data, which is also converted to a ExceptionDetail, thus preserving the data of the Exception chain. This means that custom exception types thrown by the server cannot specifically be caught by the client. It also means that the client does not need to reference these exception types. However, the client can determine the type of exception thrown by the server…

The client wishes to detect the ABC.EmptyGuidException thrown by a library called by the service implementation.

Clunky, but attainable.

A Unix timestamp is defined as the number of seconds elapsed between January 1, 1970 00:00:00 and a given date/time expressed in Universal Coordinated Time.
A timestamp can be generated up to the date 2038-1-19 3:14:7 before overflowing a 32-bit integer.
http://en.wikipedia.org/wiki/Unix_time

An alternative, encoding a DateTime into a BigInt and back again.

WinFormInactivity

Tags:

Generates a unique namespace that conforms to DNS name format from the input.

Releated: Validate Format of a Domain Name post

Input formats:
If email address, the host part is used.
If URI, the primary domain name is used.
If neither of the above formats then uses the input as is.
Looks in the [Subscription] table of the specified database table for the [Namespace] column to determine uniqueness.

 

SQLServerVersionQueryOutput

We have a ClickOnce application for which we are merging and/or embedding all of our dependency assemblies into a single assembly and obfuscating with Red Gate’s SmartAssembly. ClickOnce doesn’t know about the merging and embedding, so it includes the non-obfuscated individual assembly files when it publishes. It turns out that it’s easy to exclude these files from the deployment at publish-time from Visual Studio.

From Project properties …
Publish tab …
Application Files button …
Select “Exclude” for the Publish Status of the desired files …
Save project …
Publish.

To do this to an already-published ClickOnce application requires more work. Here’s a LINQPad script example to programmatically find a file, remove it and re-sign the appropriate files.