Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
posts - 96, comments - 70, trackbacks - 0

CodeProject-Tip

There are 14 entries for the tag CodeProject-Tip
Using WMI to fetch the command line that started all instances of a process

/// <summary> /// Using WMI to fetch the command line that started all instances of a process /// </summary> /// <param name="processName">Image name, e.g. WebDev.WebServer.exe</param> /// <returns></returns> /// adapted from http://stackoverflow.com/questions/504208/how-to-read-command-line-arguments-of-another-process-in-c/504378%23504378 /// original code by http://stackoverflow.com/users/61396/xcud private static IEnumerable<string> GetCommandLines(string processName) { List<string> results = new List<string>(); string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery)) { using (ManagementObjectCollection retObjectCollection = searcher.Get()) { foreach...

posted @ Sunday, April 11, 2010 2:12 PM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Preventing caching of content in ASP.Net Pages and other HttpHandlers

  Sometimes I forget to squash caching on my JSON handlers and end up with odd runtime behavior, especially when using IE. You can ensure that each request actually pulls data instead of hitting the cache by adding this to the top of a handler (.ashx) or in the page_load of a .aspx. Drop 'context.' for use in page_load.   context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetNoStore();   This is what the head of my JSON handlers typically look like: public void ProcessRequest(HttpContext context) { context.Response.ClearContent(); ...

posted @ Tuesday, March 23, 2010 2:59 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Coerce Windows and Internet Explorer (IE) to diplay JSON text inline

  Cheeso posted and eventually answered this question on StackOverflow about how to get IE to stop popping a file download dialog when rendering JSON text. A few things I noticed about the answer that might help others follow: You need to add the registry editor version as the first line in the .reg file You might want to also include 'text/json' content type.   Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json] "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}" "Encoding"=dword:00080000 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json] "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}" "encoding"=dword:00080000   Technorati tags: CodeProject-Tip

posted @ Tuesday, March 23, 2010 2:49 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Building Mono C# compiler (MCS) in Visual Studio 2008

If you are an old hand at building Mono on Windows using cygwin, then this post will not be of interest. The target audience for this post are those who just wish to build MCS, GMCS etc in Visual Studio 2008 without the need for MCS. Building the Mono C# compiler in Visual Studio 2008 would be as simple as opening gmcs.sln in VS and building if not for the need to generate the cs-parser.cs file. Supplied in the MCS package in the latest stable, 2.6.1,  is cs-parser.jay, which is an input file for the Jay Parser Generator for C#. The C++ source and...

posted @ Tuesday, March 09, 2010 1:09 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Generic NullSafe IDataRecord Field Getter

// usage var name = GetValueOrDefault<string>(reader, "Name"); var name = reader.GetValueOrDefault<string>("Name"); var name = reader.GetValueOrDefault<string>(0); // extension public static T GetValueOrDefault<T>(this IDataRecord row, string fieldName) { int ordinal = row.GetOrdinal(fieldName); return row.GetValueOrDefault<T>(ordinal); } public static T GetValueOrDefault<T>(this IDataRecord row, int ordinal) { return (T)(row.IsDBNull(ordinal) ? default(T) : row.GetValue(ordinal)); } Technorati tags: C-Sharp, ADO.Net, CodeProject-Tip

posted @ Tuesday, March 02, 2010 12:15 PM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Testing and Parsing an ASP.NET YSOD (Yellow Screen Of Death) from an XMLHttpRequest.responseText

  After a minor bout of forgetfulness regarding legal regexp flags in Javascript, I knocked this one out that parses the comment block at the end of an YSOD.   var rxYSOD = /<!--\s*\[(.*?)]:(\s*.*\s(.*[\n\r]*)*?)\s*(at(.*[\n\r]*)*)-->/; if (rxYSOD.test(text)) { // looks like one.. var ysod = rxYSOD.exec(text); errObj = { Message: ysod[2], StackTrace: ysod[4], ExceptionType: ysod[1] }; } will find and parse the comment block shown. I am guessing that is why they put it there....   <html> <!-- omitted --> <body bgcolor="white"> <!-- omitted --> </body> </html> <!-- [ArgumentException]: Unknown web method ValidateUser. Parameter name: methodName at...

posted @ Saturday, February 27, 2010 11:46 PM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Adding a ajax/json endpoint to an existing WCF service

Mixing and matching security to get something like this locked down and providing the desired exposure might be challenging but I didn't offer to solve that in the title, did I?    <system.serviceModel>     <behaviors>       <endpointBehaviors>         <behavior name="webScriptBehavior">           <enableWebScript />         </behavior>       </endpointBehaviors>       <serviceBehaviors>         <behavior name="Salient.ScriptModel.Services.DualServiceBehavior">           <serviceMetadata httpGetEnabled="true" />           <serviceDebug includeExceptionDetailInFaults="false" />         </behavior>       </serviceBehaviors>     </behaviors>       <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />     <services>       <service behaviorConfiguration="Salient.ScriptModel.Services.DualServiceBehavior" name="Salient.ScriptModel.Services.DualService">         <endpoint...

posted @ Friday, February 26, 2010 7:04 PM | Feedback (0) | Filed Under [ CodeProject-Tip ]

WCF to JSON Dates and back again

  I have been struggling with the seemingly unbeatable UTCness of js Date.getTime in respect to dealing with dates going between javascript and wcf. I tried adding the offset/60/1000, munging local times, closing one eye and sticking out my tongue and nothing. nothing. The server was always getting a utc long and treating it as a local. I have been following Rick's saga with wcf dates r.e. ajax and gave his solutions a go and got pleasant results with the .parseWithDate() tweak to json2.js but could not get results with the .stringifyWcf(). Got UTC with no offset no matter how much attention...

posted @ Thursday, February 18, 2010 5:33 PM | Feedback (2) | Filed Under [ CodeProject-Tip ]

Get CustomAttributes the easy way....

A few extension methods to help out..... // Project: Salient.Reflection // http://salient.codeplex.com using System; using System.Collections.Generic; using System.Linq; using System.Reflection; public static class AttributeHelpers { /// <summary> /// Returns first non-inherited custom attribute of type T /// </summary> public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider) where T : Attribute { return GetCustomAttribute<T>(provider, false); } /// <summary> /// Returns first custom attribute of type T in the inheritance chain ...

posted @ Wednesday, January 20, 2010 1:50 PM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Strongly typed deserialization of ASP.NET 3.5 ScriptService JSON in Managed code. e.g. {"d":{"__type":

Doing interop with an ASP.NET ScriptService in JavaScript is simple, just deserialize the payload, e.g. result.d. But if you are consuming with managed code it gets a bit tricky. The 3.5 stack wraps the payload in a 'd' container making deserialization to a similarly shaped type or a type in a shared library (if you are using a shared library, why use JSON? There are reasons... ;-p) impossible using any of the ms serializers. My solution is to use Newtonsoft's JSON.NET with a simple generic helper class to unwrap the payload.. public class AjaxWrapper<T> { public T d; } and then deserialize...

posted @ Tuesday, January 12, 2010 3:04 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Adding/Removing UAC Shield to button

#region UAC private const int BcmFirst = 0x1600; //Normal button private const int BcmSetshield = (BcmFirst + 0x000C); //Elevated button [DllImport("user32")] private static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam); private static void AddShieldToButton(Button b) { b.FlatStyle = FlatStyle.System; SendMessage(b.Handle, BcmSetshield, 0, 0xFFFFFFFF); } private static void RemoveShieldFromButton(Button b) { b.FlatStyle = FlatStyle.System; SendMessage(b.Handle, BcmSetshield, 0, 0x0); } private static bool IsAdmin() { ...

posted @ Thursday, January 07, 2010 3:43 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Short: Inline multithreading with lambdas

Not going to write a novel. just a snippet. Mayhaps will explain when I am sure I know what I am talking about. private static void InlineThreadingWithLambda() { int local = 0; var thread = new Thread(() => { ...

posted @ Wednesday, December 16, 2009 3:03 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Visual Studio 2008 Build Event XCopy Bug

Wondering why your post-build xcopy not working? From Microsoft Connect Thanks for reporting this bug. Unfortunately, after much investigation, we (MSBuild team) discovered that this is actually a documented bug in xcopy.exe. The owners of xcopy.exe claim that this bug is not worth fixing, and so there's not much we can do at this point. However, the good news is that there is a fairly simple (though completely non-intuitive and non-discoverable workaround), and that is to append "<NUL:" to the end of your xcopy command. So, in your case, just do this:     xcopy "$(TargetDir)*.*" "$(SolutionDir)..\bin\" /S /I /F <NUL: and that should work for you. If...

posted @ Saturday, September 05, 2009 4:51 AM | Feedback (12) | Filed Under [ CodeProject-Tip ]

Javascript enum creation pattern with Visual Studio intellisense support

visual studio javascript intellisense friendly // enum creation pattern // NOTE: i am not doing the intensive argument validation // and restricting the enum values to integer as is done in // msajax so you can probably break this with little effort. function createEnum(type, flags) { for (var i in type.prototype) { type[i] = type.prototype[i]; } // __xxx props are msajax/vs talking to each other type.__enum = true; type.__flags = flags; } intEnum = function() { /// <summary> ...

posted @ Wednesday, July 29, 2009 3:54 AM | Feedback (0) | Filed Under [ CodeProject-Tip ]

Powered by: