C-Sharp
There are 7 entries for the tag
C-Sharp
Overview
In a previous post, I proposed a means of deserializing JSON returned from calls to ClientScript endpoints such as XML WebServices decorated with [ScriptService] or [ScriptMethod] attributes, Ajax-enabled WCF Services and WCF services created with WebScriptServiceHostFactory.
The use case that prompted this requirement is testing endpoints called by client-side JavaScript. Calling WebScript enpoints in managed code using HttpWebRequest is easy, the trick is to specify content-type 'application/json'. And this is where the problems start.
Problem Domain
When a WebScript endpoint responds to a request with content-type 'application/json' it understandably assumes that it is being called from JavaScript and, as of 3.5sp1, wraps the actual...
/// <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...
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();
...
// 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
#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()
{
...
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(() =>
{
...
Conventional strategies for reading both StandardOut and StandardError involve helper methods and state objects similar to Listing 1
Listing 1: How it has been done:
public static void RunProcessConventional(string command, string args)
{
var proc = new Process
{
StartInfo =
...