Monday, April 27, 2009

VS ASP.NET Error: it is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.

Error
Error while compiling solution in Visual Studio .NET: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

Resolution
If there are any web.config files in the sub folders that make sure you don't duplicate the entries under of web.config in the root folder, in the web.config in the sub-folder.

For instance if the folder structure of your ASP.NET application is like this...
Login.aspx
Web.config
(In web.config
system.web
authentication mode="Windows"
system.web
)
Secure (folder)
Home.aspx
Web.config
(In subfolder web.config I Add the same I get error
system.web
authentication mode="Windows"
system.web
)
(In subfolder web.config I remove those entries, error goes off
system.web
!-- comment it
authentication mode="Windows"
system.web
)

Tuesday, April 21, 2009

How to deep copy an object in .NET

Introduction
This article gives a quick look at how to deep copy your objects. The idea is not to give an insight on why cloning is required and which is the best method to do it, as there are numerous articles on cloning, this article gives the implementation of deep copying in .NET
Implementation
In a shallow copy the value types are copied, whereas the reference types refer to the same shared memory. With a deep copy the memory references by the object is also copied (cloned) and each object references its own instance of data. There are different ways of deep copying an object, one method is to use memory stream, where in object is serialized into a memory stream and then we de-serialize it into a new object. Another approach is to use the Reflections concepts of .NET, article on which can be read in Code Project at this link
http://www.codeproject.com/KB/cs/cloneimpl_class.aspx .
So in this article the approach would be to use the first one and understand the implementation and complexities involved. Please note that this approach requires us to serialize the object that needs to be cloned.
Complexities are involved where in you might need to include or exclude some fields of the object, which can be achieved using the
Conclusion
This article brings together the different approaches, to deep copy a .NET object, for reference. We also get to look at what happens in the default copy wherein Member wise copy is done.

Code
///
/// Abstract class that implement Deep Cloning using Memory stream
///

[Serializable]
public abstract class MyBaseObject : ICloneable
{
public object Clone()
{
// deep-copying

MemoryStream memoryStream = new MemoryStream();

BinaryFormatter binaryFormatter = new BinaryFormatter();

binaryFormatter.Serialize(memoryStream, this);

memoryStream.Position = 0;

object clone = binaryFormatter.Deserialize(memoryStream);

memoryStream.Close();

return clone;
}
}

///
/// this class is reference from the class that needs to be cloned
///

[Serializable]
public class MyReferenceClass : MyBaseObject
{
///
/// Some Id
///

private int _id;


}

///
/// Class that needs to be cloned
///

[Serializable]
public class MyCloneableClass : MyBaseObject
{
///
/// Class to be cloned having a reference Type
///

public MyReferenceClass _myReferenceClass;

///
/// Class to be cloned having a value type
///

private string _name;

///
/// Constructor
///

///
public MyCloneableClass(int id)
{
_myReferenceClass = new MyReferenceClass();
_myReferenceClass.ID = id;
}

}
This article brings together the different approaches, to deep copy a .NET object, for reference. We also get to look at what happens in the default copy wherein Member wise copy is done.

MSDN: U.S. Local Highlights