markneustadt.com

Public/Private key problems

Lately, I’ve been working on a project that has some pretty heavy duty encryption requirements.  This stuff is very cool, but somewhat mysterious.  I have run into these two errors and couldn’t really find a good description of what they are or more importantly, how to fix them.


Input too large for rsa cipher

and


Unknown block type

The problem is that without the code changing at all these two errors pop up all the time but very randomly. Read more

Pausing to catch your breath in C#

Sometimes in your code, you find the need to try an operation that’s dependent upon some other system.  That other system might be a little laggy.  As such, you want to try your operation and then pause a moment or two before you try again.  A simple way to do this is with

Thread.Sleep(1000);

That little snippet of code will wait for one second and then continue.  There’s a better way though…

Read more

WebAPI – Accepting both form data and query string parameters

Recently, I had to write a WebAPI method in C# that would be an HTTP POST.  Basically, I wanted to hit

http://localhost:99/api-demo/samplemethod/myIdString

and pass a bunch of data in the body of the request. Simple, no?

In our project, we make heavy use of the Route attribute to decouple the method name from the URL.  My method looked okay.

    [RoutePrefix("api-demo")]
    [SetUser]
    public class DemoController : ApiController
    {
        [HttpPost]
        [Route("samplemethod")]
        public IHttpActionResult Sample([FromBody] string requestBody, string id)
        {
            return Ok(String.Format("Request with ID {0} body has a length of {1}", id, requestBody.Length));
        }
    }

This sample code is clearly very simple.  I have decoupled the controller to api-demo and the method to samplemethod. It appears like everything would work as expected, however when I would test it via POSTMAN, I get the following response:

{
  "$id": "1",
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost:99/api-demo/samplemethod/1134630'.",
  "MessageDetail": "No type was found that matches the controller named 'api-demo'."
}

Read more

Performance – Using Try/Catch

I just wrote the stupidest program ever.  It is inefficient by design.  If anyone did this thinking “hey… this is great code” they should be fired.

Anyway… generally speaking, using try/catch blocks is not a great idea.  There are a myriad of reasons but it’s usually something along the lines of:

If you truly have an unexpected error it’s better to let the program crash and then fix the bug.

If you do have to use the try/catch exception handling block, you should only catch explicit exceptions.  For example catch a file access exception or a database invalid primary key exception.  Just catching the generic exception is poor practice.

There’s another reason to avoid try/catch though… it is a performance hog.

Read more

Inherited Object Serialization with Newtonsoft.JSON

Wow… I’m coming up with some sexy titles for posts lately.

This is going to be how to deserialize JSON objects into native C# objects using Newtonsoft.JSON.  Serialization of objects to JSON is very straightforward.  Serializing objects inherited from a base class is pretty simple, but there’s a little bit of doing to do first. Converting those items back though… that’s a trick.

Read more

Scroll To Top