Monday, December 23, 2013

MVC API Controller Error

You may get this very weird error when you test the API Controller using the Entity FX code-first approach. Here is my error,

Type 'System.Data.Entity.DynamicProxies.Speaker_78C9094BC0D38F33C0C4EEAD409D4FABB88C631E3F9CDA236EF6BE10DD027500' with data contract name 'Speaker_78C9094BC0D38F33C0C4EEAD409D4FABB88C631E3F9CDA236EF6BE10DD027500:' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

and the Stacktrace,
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfSpeakerToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.<>c__DisplayClass7.<WriteToStreamAsync>b__6() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)

and My code,



to Fix this, just turn off the proxycreation for this DBContext


Thursday, December 19, 2013

How to remove the dead nodes in the Solrcloud manually

Somehow, you may find SolrCloud has a track of all the Nodes even those dead or testing Nodes in the clustermap.

  for example, there is one dead nodes, here

If you hover on it, it shows the ip and port.

so here we go, remove the 8983 one. Basically the cluster state is keeped in the Zookeeper, all we need to do is download the file and remove the dead/testing nodes, then upload it back. To do this, we need download a full version of the zookeeper , under the bin ,the is a full featured zk client zkCli which enable us to get and set the data.

So save the clusterstate.json to local file.

find the dead ndoes, and remove it then save as a new file like new.txt

then upload it back,

./ -server set /clusterstate.json "`cat new.txt`"

then it’s gone!~


Tuesday, September 17, 2013

JAVA: ByteBuffer.Allocate vs AllocateDirect

Allocation on heap, GC involved.

the process used 2G RAM

the buffer is one the HEAP Old space,

If allocatedirect on RAM,


still 2G as a whole


but not on Heap,


so The heavily used buffer appropriately continues to use a non-heap, non-garbage-collected direct buffer.

Cassandra: InvalidRequestException(why:Key may not be empty)

When you get this exception when you try to submit some changes through the THRIFT gateway to the cluster,

the code are simple,

client.remove(KEYSPACE, key,cp, System.currentTimeMillis(), ConsistencyLevel.ALL);

and when you check the key==null, it’s not null for sure, but how comes still get the key empty error?

then check ThriftValidation class,

now you know, the key is not null, the remaning() is zeor.
typically this means the key itself has been read and the pointer is at the end, so before you read the key, try duplicate a new one.

use this utilit lib instead,

Friday, September 13, 2013

How-to: Test the HBase Coprocessor tutorial

here is a quick tutorial to setup hbase instance and hook up with a basic coprocessor.

Download the tar file and unzip it from
   Change the Hbase-site.xml, point the hbase.rootdir to the local folder, by default Hbase will load the default one embedded in the hbase-x.jar, and default setting is local temp folder,


change it to a folder you fell comfortable,
  also change the hbase.temp.dir to a well know folder, you can see all the underlying folder it used,
My final change,


then you can start the hbase server ./bin/

  check the port it listened, should be a port called 60030

then you can go the http://ip:60030 to see the hbase console of the given region. and 60010 for the hbase master


then create a table and put a record


check the folder


Now let’s write a basic java program to read the data in hbase.

Create a new maven project by clicking the eclipse wizard,
Click the pom.xml, right click to add one dependency hbase.


Simple Query in another server, make sure the zookeeper ip and host name are accessible from client machine,


for the coprocessor, they are bascailly observers in 3 levels.
  Master level, you can hookup with all the DDL like create table,update column,etc (check BaseMasterObserver)

and DML level in the region servers,

And WAL level, all changes going to WAL, basically you can see all the changes. you can even reject the change
I will put the WALobserver as a example, just dump out any changes to the cluseter, we can send the changeset to another server do secondary indexing, or for logging /auditing purpose.


compile and package it to a jar file,then copy to the hbase lib folder, or any folder which is in the HBASE_CLASSPATH

then change hbase-site.xml, point to our WALobserver


then restart the instance, from the region server, you can see our WAL is loaded,

then make some changes to the data,

from the log, you should see our logging , data changes captured here

Please note the WALobserver runs in the same JVM of the hbase, make sure not fail safe and no huge extra performance hit.

Friday, September 6, 2013

How to: Sql Server encryption



If you want to export the key to another server, backup it and restore it.

Encryption by Cert


Thursday, September 5, 2013

How to: Generate the same hash value in Sql Server, C# and Java.

Since SQL Server 2005, there is one built-in function called hashbytes


please by aware if you define the string type as char and varchar , the hash varies.

so , here is one example


to be safe, using Rtrim on nchar type to get the same result vs the varchar one

C# and Sql, If you want to get the same hash for a given string, be sure to use the same encoding logic for the string, like Ascii vs Unicode.

in Java, try the commons-codec,

if you use nvarchar or nchar, make sure use unicde in c# also.

Sql server unicode


something in Java,switched it to unicdoe, follow the same byte orders

Wednesday, September 4, 2013

How to: turn on the logging for System.Net in C# 4.0

If you checked the code in HttpWebRequest by reflector, you might found it has a lot logging support code


sometimes, you do want to check the request behavior on sockets, http level, you can simply turn on the logging. here is a quick how,
change your config to somehow like this,

when you run the app with the httprequest  request , you will see the detailed logging


different trace listeners in a nutshell:


Wednesday, August 21, 2013

java, testing salted hash using the commons-codec

There is one class called DigestUtils in the apache commons-codec, here is a quick how to start tutorial to pickup the jar and do a simple one way hash.

Add a dependency to commons-codec in your pom file

<project xmlns="" xmlns:xsi=""


then update the project ,we can see the jar is included in the project



If you want included the jar in the dependenciy, add one plugin for the assembling

then mvn assembly:assembly you will see the jar with the inlined codec jar files.


Tuesday, August 6, 2013

How to FIx,”New Nexus 7 stuck at X logo, but FULLY CHARGED”

Got a New Nexus 7 (2013) modal, and it was stuck on the X logo forever, it’s fully charged.
  my firs try is to restore to the factory image, however, no official image for “flo” the New nexus 7 in the Google page.

it only have the old one

after did a lot Goggling, found this one works


fastboot boot recovery.img

then clear cache, install the zip for sdcard or sideload

then it will works, Good luck

Locations of visitors to this page