Easiest way to generate MachineKey

Have you ever needed to generate a MachineKey to use in your application’s configuration file or in machine.config? You may need a MachineKey in several scenarios. One of them is the scenario where you deploy your application in a web farm. One another scenario is to need to encrypt Forms Authentication tickets.

You have some options to generate a MachineKey:

  • You can build the sample application from the following article: http://support.microsoft.com/kb/312906
  • You can search in Bing for “MachineKey generator” and use an online service. Honestly I wouldn’t rely on third party online services for generating MachineKey because I wouldn’t have any control over them and I couldn’t make sure that if they wouldn’t log my IP address and MachineKey in a database to use that later – yes, I know it sounds like “paranoia” 🙂
There is one another way which I have learned from one of my customers today. It was over there waiting in IIS user interface but I never gave it a try before (shame on me). You can use IIS 7.5 user interface to generate the MachineKey section and save it in the web.config of your application / root web.config file. Steps are quite easy:
1) Open IIS manager.
2) If you need to generate and save the MachineKey for all your applications select the server name in the left pane, in that case you will be modifying the root web.config file (which is placed in the .NET framework folder). If your intention is to create MachineKey for a specific web site/application then select the web site / application from the left pane. In that case you will be modifying the web.config file of your application.
3) Double click the Machine Key icon in ASP.NET settings in the middle pane:
2072-select_machinekey
4) MachineKey section will be read from your configuration file and be shown in the UI. If you did not configure a specific MachineKey and it is generated automatically you will see the following options:
6431-machinekey_default
5) Now you can click Generate Keys on the right pane to generate random MachineKeys. When you click Apply, all settings will be saved in the web.config file.

ASP.NET MVC RenderPartial With Null Model

If you’ll try to pass null to the RenderPartial method it will actually pull the model from the page and will, most likely, give an error.
So instead of

you better write

That’s how the RenderPartial method works (I know should have been documented, blogged about, etc…, me too I find this a little strange). If you do not specify a model or pass null it will use the model of the parent page. To avoid this you might use the null coalescing operator:

And if you are really curious as to how this is implemented there’s an excerpt from the relevant parts of the ASP.NET MVC 2 source code:

 

Notice how the case of null model is handled.

Transmitting Network Data Using Volley

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.

Volley offers the following benefits:

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

Volley excels at RPC-type operations used to populate a UI, such as fetching a page of search results as structured data. It integrates easily with any protocol and comes out of the box with support for raw strings, images, and JSON. By providing built-in support for the features you need, Volley frees you from writing boilerplate code and allows you to concentrate on the logic that is specific to your app.

Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing. For large download operations, consider using an alternative like DownloadManager.

The core Volley library is developed in the open AOSP repository at frameworks/volley and contains the main request dispatch pipeline as well as a set of commonly applicable utilities, available in the Volley “toolbox.” The easiest way to add Volley to your project is to add the following dependency to your app’s build.gradle file:

You can also clone the Volley repository and set it as a library project:

  1. Git clone the repository by typing the following at the command line:
  2. Import the downloaded source into your app project as an Android library module as described in Create an Android Library.

Creating POST method in Web API

In this blog I will explain HTTP Post Method in Web API.

SNO Action HTTP Method Relative URI
1. Create New Record POST /api/Record

you need to create an object in web api project with Activity, CustomValue, PropertyIndex properties

and HttpPost will be

Creating Resources HTTP POST

Note: Method name should be start from Post.

Snippet

full code snap

postrecord

Thanks

Dharmesh Sharma

HTTP post XML data in C#

An example of an easy way to post XML data and get the response (as a string) would be the following function:

Snippet

Paste JSON As Classes’ in ASP.NET and Web Tools 2012.2 RC

‘Paste JSON As Classes’ is a cool feature in ASP.NET and Web Tools 2012.2 RC. This feature will help you generate strongly typed classes in C# or VB.NET from valid JSON text.

With ASP.NET and Web Tools 2012.2 RC installed, you will see new menu option like below for C# and VB.NET Website and Web Application projects only. This new menu option will be enabled for .cs and .vb file extensions inside these projects:

clip_image001

JSON to C#/VB.NET class conversion

To use this feature, just copy sample JSON text and “Paste JSON As Classes” inside .vb or .cs file. This feature uses Newtonsoft JSON parser to parse JSON text from clipboard. Once Newtonsoft JSON parser validates the clipboard data as valid JSON, then it will be converted into C# or VB.NET class depending on the selected file type.

JSON to Classes Conversion rules

  1. Outermost class name is always Rootobject.
  2. All classes and properties are public.
  3. If property is a keyword in C#/VB.NET then it will be prepended with _ (underscore).
  4. If property is number then it will be converted to int/float/double/Single type
  5. JSON string is converted to string in C# and String in VB.NET
  6. Boolean data types are converted to bool in C# and Boolean in VB.NET
  7. If JSON text in clipboard is array of multiple JSON objects and for given property if value is null in one object and value is supported type in another then it will be marked as nullable type.
  8. With single JSON object, nullable data type will not be emitted.

Let’s look at some examples:

C# class for above JSON object will look like below.

VB.NET class will be like below

DateTime Formats

JSON parser recognizes some DateTime formats as valid DateTime data types and some are not.

Below JSON object has some valid DateTime data and some invalid DateTime data types

C# class for above JSON object will look like below.

First two properties of Class1 are valid DateTime objects, 3rd property is deemed as string as the DateTime format is not valid/recognized. Last property is converted as object as one object in array is valid DateTime and other object in array is invalid DateTime object. So property type cannot be DateTime nor can be string. Common base type for string and DateTime is object.

Single Dimensional Arrays

JSON Arrays are represented with [ ] notation in C# and with () in VB. Example as below.

C# class for above JSON object will look like below.

VB. NET class as below

 

Multi-Dimensional Arrays

You can convert multi-dimension JSON arrays to classes. JSON Example for two dimensional array:

[ [ 1, 2, 3 ] ]

C# class for above JSON object will look like below:

VB.NET class:

Also you can convert JSON with objects having arrays inside array as shown in the example below

C# class for above JSON object will look like below:

VB.NET class:

Troubleshooting

If JSON Object in clipboard is invalid, error message will be shown with the reason.

Example: Below JSON object contains max value for decimal data type. Newtonsoft JSON Parser does not recognize values above 1.7976931348623157E+308(which is max value for double data type).

This will result in error like below when “Paste JSON As Classes” is called.

clip_image002

Another example of invalid JSON object where “” are not valid character:

This will result in error like below when “Paste JSON As Classes” is called.

clip_image003

Thank you for your time reading this blog post. Looking forward to hear your feedback on this feature!

Introduction to Redis

Redis is cache technology  open source for memory data structure store, used as database, cache and message broker. It supports many data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps,hyperloglogs and geospatial indexes with radius queries. You can use with many technology. Redis has built-in replication, Lua scripting, LRU eviction,transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.

In order to achieve its outstanding performance, Redis works with an in-memory dataset. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while, or by appending each command to a log. Persistence can be optionally disabled, if you just need a feature-rich, networked, in-memory cache.

Redis also supports trivial-to-setup master-slave asynchronous replication with fast respnse, with very fast non-blocking first synchronization, auto-reconnection with partial resynchronization on net split.

Other features include:

  • Transactions
  • Pub/Sub
  • Lua scripting
  • Keys with a limited time-to-live
  • LRU eviction of keys
  • Automatic failover

You can use Redis from most programming languages out there.

Redis is written in ANSI C and works in most POSIX systems like Linux, *BSD, OS X without external dependencies. Linux and OS X are the two operating systems where Redis is developed and more tested, and we recommend using Linux for deploying. Redis may work in Solaris-derived systems like SmartOS, but the support is best effort. There is no official support for Windows builds, but Microsoft develops and maintains a Win-64 port of Redis.