Monday, November 4, 2019

Creating JSON in C# With Dynamic

I'm excited to share a new (to me) way of creating JSON on-the-fly.  It uses the dynamic type in C#, which I knew about, but hadn't used too much.  Now I see how powerful it really is.

I ran across a problem where I had to build a JSON string to send to a web service as a parameter.  It was short but nested several layers deep.  I also needed it to be highly configurable.  I could build it with a string, or better yet, a StringBuffer.  Or I could build an object, then serialize it to JSON.  The former seems fragile, the later feels like overkill (And, there is nothing worse than trying to decode what another programmer has done with a thousand little classes you have to hunt down.)

Enter the dynamic type.  I knew it was very late-binding, thus avoiding compile-time type checking.  It gets its type at run-time.  So, it infers the type when the program runs and is able to do this:

dynamic nVal = 23.4;
dynamic sVal = "Hello";
Console.WriteLine(nVal.GetType().ToString());   // System.Double
Console.WriteLine(sVal.GetType().ToString());   // System.String

However, a little talked about feature is that it is an object that can be a container too.  And it can contain different types including arrays and objects.  This sets us up to be able to nest JSON very nicely.  Check out this example:

dynamic message = new JObject();
product.to = "UserToken";
product.priority = 5;
product.notification = new JObject();
product.notification.message = "You have a new order.";
product.notification.sound = "default";

Console.WriteLine(message.ToString());

// Output JSON
{
    "to": "UserToken",
    "priority": 5,
    "notification": {
        "message": "You have a new order.",
        "sound": "default"
    }
}

Hopefully, this will help you create simpler JSON data too.

Friday, September 20, 2019

Web Load Testing with Fiddler

If you have been in the web business long, you've got to love Fiddler.  There is so much packed into a little package.  It can save you hours of debugging -- if you know how to use it.

Here's a new load test feature (at least it's new to me.)  Basically, in the Results window, select any request or set of requests, then press CTRL-R.  This will bring up a dialog box asking for how many times you want to run the command.  Enter your test count, press OK and let the barrage begin!

While there isn't too much instrumentation, there is a fair amount.  If you select the Timeline tab you can get an idea of concurrent transfers.  And the Statistics tab gives you performance information.

I was recently performance testing a web server application.  I used this technique in conjunction with Perfmon and was able to get some deep statistics.

Try it out and let me know how it works!

Sunday, August 25, 2019

Setting the Default Terminal to zsh In VS Code

If you are like me, you've fallen in love with both VS Code and zsh.  VS Code has made my Angular and Ionic development much easier.  And if you are here, you know zsh is the most beautiful shell out there.  So, how do you mash them together?

It turns out it's pretty easy... much easier than it used to be.

Just fire-up any project in VS Code.  Then bring up the terminal window (Control ~).  Next, look for a dropdown that is on the right top of the terminal window.  If you click on that dropdown, you can select zsh and make it your default.





Happy coding.

Wednesday, January 16, 2019

Mac OS Mojave Dark Mode - Set Mail Background to White

This is an example bad-looking email.
I don't entirely hate the Mac OS Mojave Dark Mode.  It's kind of a cool, different view of our real estate.  I don't know that it was entirely worth the download, but at least my Mac isn't bothering me every day to upgrade.

Anyway, one problem I had was that email backgrounds were all dark.  This made reading emails super-tough when I got replies in dark text.  Emails never quite look right.  Well, there is a fix for it!

To return your Mac Mail emails to defaulting to white, follow these steps:
  1. First be in Dark Mode (Apple -> System Preferences -> General.  Set Appearance: Dark)
  2. Next, open Mac Mail and go to Mail preferences.  Mail -> Preferences
  3. Select the Viewing icon
  4. Un-check the Use dark backgrounds for messages

Have an awesome, productive day!

Friday, June 22, 2018

Regenerating XSD Dataset After Change

I find myself constantly having to maintain projects built with XSD datasets.  The thing I always *strongly disliked* about XSD's is their tendency to be fragile.  Whenever I make a change everything breaks and I'd end up fooling around for a 1/2 day getting it to work again.

Well, I think I've finally figured out how to make changes to an XSD, then regenerate the schema correctly.  Here are the steps that are working well for me now:

Open the XSD and make whatever changes you need.  In this sample, I'm just adding a new column to a SQL Server stored procedure.  Once I've made the change in the stored procedure, it's time to synchronize the XSD.

Go to the XSD view in Visual Studio, right-click on the object you need to change and click "Configure."


Go through all the steps of configuring that item...


You will notice that there is a file called "...Designer.cs".  This is the file auto-generated by the DataSetGenerator tool (or at least it's supposed to be auto-generated).


If this file isn't being changed, you can force it to make a new one.  First, delete the Designer.cs file (Right click => Delete).

Then right-click on the .XSD file and select "Run Custom Tool".  This will regenerate the Designer.cs file and you should be off and running.



What?  You don't have a menu item that says "Run Custom Tool"?  Then you need to add it to the XSD file type.  You do that by left-clicking on the XSD file, then go to the properties.



Under custom tool, make sure it says MSDataSetGenerator.  If it doesn't, add it.

Happy coding!




Tuesday, January 16, 2018

SSRS Report Rendering GIF and PNG Format

This is a historical post so I don't forget how to turn this on in MS Reporting Services.  Hopefully, it helps others too as it isn't immediately obvious (like everything Microsoft makes, I suppose).

Anyway, to turn GIF and PNG rendering on in SSRS:
  1. Edit the file:

    {Reporting Path}/rsreportserver.config
  2. Which happens to be this on the server I'm working on:

    C:\Program Files\Microsoft SQL Server\MSRS10.MSSQLSERVER\Reporting Services\ReportServ\rsreportserver.config
  3. Now, search for the tag <render> within this XML file.
  4. Add the following rendering options within this <render> element:
<Extension Name="PNG" Type="Microsoft.ReportingServices.Rendering.ImageRenderer.ImageRenderer,Microsoft.ReportingServices.ImageRendering">
                <Configuration>
                    <DeviceInfo>
                        <ColorDepth>24</ColorDepth>
                        <OutputFormat>PNG</OutputFormat>
                    </DeviceInfo>
                </Configuration>
            </Extension>
<Extension Name="GIF" Type="Microsoft.ReportingServices.Rendering.ImageRenderer.ImageRenderer,Microsoft.ReportingServices.ImageRendering">
                <Configuration>
                    <DeviceInfo>
                        <ColorDepth>32</ColorDepth>
                        <OutputFormat>GIF</OutputFormat>
                    </DeviceInfo>
                </Configuration>
            </Extension>

You should be all set. SSRS will pick up the change immediately, and it will be an available output rendering format.

As a bonus, you can add this to your automatic Reporting Services web service, by sending in this device info string:

@"<DeviceInfo><StartPage>{0}</StartPage><EndPage>{1}</EndPage><OutputFormat>PNG</OutputFormat></DeviceInfo>"

Maybe I'll do another blog on how to use Reporting Service's Web Service to render reports in code.  When I do, I'll link to it here.

Wednesday, October 18, 2017

Null Coalescing Operator

Remember this operator from your first Perl class back in 1994?  Well, if you don't it's a good one for your back pocket.  It works in Perl, Bash, C#, F#, Objective-C, Swift, and even SQL.  It is great for a quick way to do a Null comparison:

[C#]
string strNewValue = strRequestValue ?? "Default";

In this case, if strRequestValue is null, "Default" is assigned to strNewValue.

In SQL Server, it looks like this:

[SQL]
ISNULL(strRequestValue, 'Default')
Neither of these formats are super readable unless you are used to seeing them.  In the case of the SQL example, it looks very close to the IS NULL comparison, but whatever => the SQL team at MSFT sometimes does things like that.

It might be you want to do this instead:

[C#]
string strNewValue = isNullOrEmpty(strRequestValue) ? "Default", strRequestValue;

but I never found this to be very readable either.

I think the real power of Null Coalescing Operators is when it's used to create an object instance like this:

[C#]
private IList<Foo> _foo;
public IList<Foo> ListOfFoo
             { get { return _foo ?? (_foo = new List<Foo>()); } }
Boom!  You're off an running!

(Props go to @wilbursullivan who showed me this)