Friday, May 22, 2020

C# Properties/Fields and Serialization

I was dealing with an odd dilemma while updating an ASP.Net MVC API. I had created 2 new classes to represent the data the front end was going to POST.

Here's the 2 class definitions in question:

    public class GraphicModel
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Byline { get; set; }
        public string BylineTitleId { get; set; }
        public string BylineTitleDescription { get; set; }
        public string Category { get; set; }

        public List<GraphicBinary> GraphicBinaries;
    }

and:

    public class GraphicBinary
    {
        public SubmittedImageType ImageType { get; set; }
        public string OriginalFilename { get; set; }
        public string ItemId { get; set; }
        public string RecordId { get; set; }
        public int RecordSequenceNumber { get; set; }
    }

I had set things up on the front end to post the following JSON:

{
    "Title": "AP Poll Stay at Home Protest Approval",
    "Description": "A new UChicago Divinity School/AP-NORC poll finds that two-thirds of Democrats and about half of Republicans disapprove of recent protests of stay at home orders.;",
    "Byline": "F. Strauss",
    "BylineTitleDescription": "STAFF",
    "BylineTitleId": "STF",
    "Category": "a",
    "GraphicBinaries": [
        {
            "ImageType": "PrintGraphicAI",
            "ItemId": "dd142b48fe7c4cc6bf9b42c9c9402e7d",
            "RecordId": "dd142b48fe7c4cc6bf9b42c9c9402e7d",
            "RecordSequenceNumber": 0,
            "OriginalFilename": "ChicagoShootings.ai"
        },
        {
            "ImageType": "PrintGraphicJPEG",
            "ItemId": "ccce25ddc1cb45d898b09eb0d91fcecc",
            "RecordId": "ccce25ddc1cb45d898b09eb0d91fcecc",
            "RecordSequenceNumber": 0,
            "OriginalFilename": "ChicagoShootings.jpg"
        }
    ]
}

Here's the signature on my controller method:

    [HttpPost]
    public JsonResult Graphic(PhotoAction actionType, bool hid, GraphicSubmission graphicModel)

What was happening is the method was invoked properly but the GraphicBinaries field was always empty even though the JSON was correct. 

I posted a question on Stack Overflow (greatest site on earth!) here:


The long and short of it is there's a difference between fields and properties in C# as noted here:


This is critical because only properties are serialized/deserialized and the GraphicBinaries field was a field not a property!

Wednesday, February 15, 2017

Running Windows VM on OSX on multiple drives

I had been having disk space issues on my Macbook due to the fact that I had almost 100Gb of my 250Gb SSD drive dedicated to a Windows VM (VMWare Fusion 7). Space was getting cramped on both machines as 100Gb wasn't really enough for my Windows work either.

I decided to try to move the VM off of the SSD drive to a Western Digital Passport 1Tb USB drive. Sadly performance using the USB drive was terribly slow and there were other strange issues I encountered when running from the USB drive. Sometimes the VM would not recognize the mouse or keyboard on a reboot. Other times the mouse would move but the VM would not register mouse clicks. Things rapidly progressed to the point where the VM was almost unusable for day to day work.

I started researching to see if it would be possible to minimize the size of the Windows boot drive to allow it to stay on the SSD but offload much of the data to a volume on the USB drive.

It turns out this is quite possible. The keys to accomplishing this are:
  1. Relocating personal folders to the external drive
  2. Relocating certain Windows data folders to the external drive and creating junction links to them so Windows can still get to them. These folders included: Windows\Installer, ProgramData\Package Cache.
I did find a few articles that were particularly helpful for this process:

http://www.techsupportalert.com/content/how-move-windows-7-personal-folders-my-documents-another-drive.htm
http://superuser.com/questions/931992/moving-windows-folders-around-with-junction-links-mklink

The first step was to create a new virtual hard drive for the data volume on the VM. Here's an article with details on how to do this:

https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1004113

Once I had the new volume available on the VM I performed the following steps to migrate data off of the boot drive to a newly created volume on the USB drive:
  1. Moved any folders I knew weren't critical to Windows and didn't require any "extra" work. In my case these included Python, Cygwin and my root source code directory.
  2. Relocated the User folders Documents, Downloads, Music, Pictures and Videos
  3. Moved the Windows\Installer folder and created a junction link for it.
  4. Moved the AppData\Package Cache folder and created a junction link for it.
Note: Steps 3 and 4 required booting into Safe Mode before moving them.

By performing these steps I moved approximately 50Gb of files from the boot drive to the external drive. However, at this point the VM and the associated volumes were still on the USB drive and the VMDK file associated with the boot drive was larger than the amount of free space I had on the SSD drive. This required that I "shrink" the boot volume. I followed the steps in this article:

https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1001934

Once completed the VMDK file for the boot volume was small enough to move back to the SSD drive but the newly added data volume was too large. What I wound up doing was making a copy of the VM (it's really just a folder) on the USB drive, opening the VM folder and deleting the data volume VMDK file (remember it's still in the original folder). I then copied the resultant VM back to the SSD drive.

With the cloned VM back on the SSD we need to attach the data volume on the USB drive to the VM on the SSD drive. You can do this by doing the following:

https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2005146

At this point you can boot up the VM and should be good to go from there.


Friday, September 19, 2014

Batttleship Bot

Going to start working on a fun challenge that's part of our Tech Summit Challenge.

The challenge is to build a Battleship playing "bot" along with a UI for displaying game status and results.

I'm planning on creating the bot portion in Python and the UI using AngularJS. Should be interesting and that's the point of the challenge to be fun and use technologies I don't use in my day-to-day work.

I've found a few potentially useful resources.

First is a Battleship game API here:

http://battle.platform45.com/

As well as this one:

http://arnosoftwaredev.blogspot.com/2008/06/battleship-game-algorithm-explained.html

I hope to use it to test my game bot against. I can't really use it for the UI as it does not maintain a board status but in theory I must keep the board status inside the bot.