Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

asp.net error: Operation is not valid due to the current state of the object

ASP.NET requests that have lots of form keys, files, or JSON payload receive an error response from the server. The Application log on the server has a Warning entry with a Source that is a specific version of ASP.NET, and an Event ID of 1309. The event log contains one of the following messages:

Message 1:

Application information:
    Application domain: /LM/W3SVC/1/ROOT/<App Domain>
    Trust level: Medium
    Application Virtual Path: <VDIR Path>
    Application Path: <App Path>
    Machine name: <Machine Name>
Process information:
    Process ID: 0001
    Process name: w3wp.exe
    Account name: IIS APPPOOL\DefaultAppPool
Exception information:
    Exception type: HttpException
    Exception message: The URL-encoded form data is not valid.
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.get_Form()
   at System.Web.HttpRequest.get_HasForm()
   at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)
   at System.Web.UI.Page.DeterminePostBackMode()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Message 2:

Application information:
    Application domain: /LM/W3SVC/1/ROOT/<App Domain>
    Trust level: Medium
    Application Virtual Path: <VDIR Path>
    Application Path: <App Path>
    Machine name: <Machine Name>
Process information:
    Process ID: 0001
    Process name: w3wp.exe
    Account name: IIS APPPOOL\DefaultAppPool
Exception information:
   Exception type: InvalidOperationException
    Exception message: Operation is not valid due to the current state of the object.
   at System.Web.HttpRequest.FillInFilesCollection()
   at System.Web.HttpRequest.get_Files()
   at FileUpload.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint

 

to resolve this problem, you should change your web.config

<configuration>
  <appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="5000" />
  </appSettings>
</configuration>

Random in Java

Random in Java:

public Random()

It will create a new random number generator. Its seed is initialized to a value based on the current time:

public Random() { this(System.currentTimeMillis()); }

Based on this, two Random objects created within the same millisecond will have the same sequence of random numbers.

Tips for working with template inheritance in Django

  1. If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.
  2. More {% block %} tags in your base templates are better. Remember, child templates don’t have to define all parent blocks, so you can fill in reasonable defaults in a number of blocks, then only define the ones you need later. It’s better to have more hooks than fewer hooks.
  3. If you find yourself duplicating content in a number of templates, it probably means you should move that content to a {% block %} in a parent template.
  4. If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (see the next section), since
    it was already escaped, if necessary, in the parent template.
  5. For extra readability, you can optionally give a name to your {% endblock %} tag. For example:
    {% block content %}

    {% endblock content %}
    In larger templates, this technique helps you see which {% block %} tags are being closed.

Duplicate Code

Duplicate code, sometimes referred to as clones, is a cluster of code blocks that are functionally equivalent (or nearly equivalent) spanning across two or more locations within a solution. Duplicate code is expensive to maintain because:

  1. Multiplied bugs. A bug in one clone means there’s a bug in all the copies. This can lead to a continued, repeated release of previously-fixed bugs as each copy of the bug is individually discovered by a customer and then fixed by the team.
  2. Flexibility barriers. If the copied code needs to be more flexible, changes need to be made across all the copies or ideally, the copies need to be consolidated first. Unfortunately consolidation is a high-risk, error-prone, time-consuming activity.
  3. Increased ramp-up time. Copy the code and new developers trying to get up to speed will have twice as much code to read and understand. If discovered, duplicated code tends to be harder to understand than normal code because the reader must not only understand functionality, but also understand the reason behind the duplication.

Setting an aspnet.config File per Application Pool

The aspnet.config file is a little known config file which is supported by ASP.NET 2.0 and greater.  Generally it lives in the root of the framework folder, for example:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet.config
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet.config

This config file is further leveraged in ASP.NET 4.0 for concurrency and threading.  For example, you can set maxConcurrentRequestsPerCPU, maxConcurrentThreadsPerCPU and requestQueueLimit, in addition to the previous runtime settings.

In Windows Server 2008 R2 (IIS 7.5) support was added to allow different settings per application pool.  Where previously the settings had to be applied to the whole framework version, now they can be specific to each app pool.  It does this by allowing you to create a custom aspnet.config file per app pool.  You can save them wherever you want on disk and IIS will pick them up when the app pool starts.

Note that the framework aspnet.config file is still used so only differences per app pool need to be set here.

Here’s an example of what the file can contain:

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>

    <runtime>

        <legacyUnhandledExceptionPolicy enabled="false" />

        <legacyImpersonationPolicy enabled="true"/>

        <alwaysFlowImpersonationPolicy enabled="false"/>

        <SymbolReadingPolicy enabled="1" />

        <shadowCopyVerifyByTimestamp enabled="true"/>

    </runtime>

    <startup useLegacyV2RuntimeActivationPolicy="true" />

  <system.web>

    <applicationPool

        maxConcurrentRequestsPerCPU="5000"

        maxConcurrentThreadsPerCPU="0"

        requestQueueLimit="5000" />

  </system.web>

</configuration>

Manage a checkbox via jQuery

the checkBox in html should be

<input type="checkbox" class="myCheckBox" />

Bind a click event to a checkbox

$(".myCheckBox").click(myCheckboxEvent);

The click event like this

function myCheckboxEvent() {
    alert($(this).is(':checked')); // alert the checked value
}

You can see, it is quite easy to ‘get’ the checkbox sender via jQuery -‘this’