I hate long titles, but this is what everyone is going to be searching for if they get this error, so that’s the title today.
I just got done fixing an error and when I went to Google, there were only five entries for this phrase and none of them solved the problem. Some got me close and gave me a clue, but none of them said…
For as many places as I’ve been where they use VPNs, I’ve yet to find one that is set up correctly. I suppose there is a good reason for this, but I consider the problem to be mostly Microsoft’s fault. I mean, wouldn’t you assume that if it were possible to use your regular connection for all the network traffic EXCEPT for the traffic that needs to go through the VPN, that is what you would want? But no. Microsoft sets it up so that ALL of your traffic goes through the VPN connection.
In it I explained the main differences between using JSON and using the update panel. I really started out thinking I’d get to how to write JSON code, but I ran out of space. Well, today, we get to the HOW.
One of the cool new tricks we are able to perform in ASP.NET that we were not able to use in ASP is the ability to have ASP.NET redirect file requests in the same way that mod_rewrite does under the Apache webserver.
For example, we could have the browser ask for an image file that doesn’t exist on the file system and have ASP.NET serve up the file from the database.
I saw a question on Channel 9 that I’ve heard before. My guess is that there are enough people who have the same question that it’s worth addressing.
I know there’s probably a really good reason for this, but I can’t think of what it is, and it keeps bugging me. Why can’t you do int x = 10; string y = (string)x; in C#? I mean, you could simply use x.ToString(), but why doesn’t the explicit cast do the same?
I’ve been thinking about the state of the programmers coming into our industry recently. It seems to me that many of the college students who graduate today understand the syntax, but few know how to apply true object oriented principles to the real world.
I recently had a discussion with a friend who confirmed my observations. Simply put, most people do not think in object oriented terms.
Judging from the comments I received yesterday, it looks like we need to review serialization in .NET.
The Easy Way
There are two ways of making an object serializable. The first, and easiest way is by applying the Serializable attribute to the class. Once you’ve done this all of the member variables in the class will get stored into the stream unless you’ve attributed one of the variables with NonSerialized.
This method is useful for objects you want to be able to store temporarily. For example, you might use this to transfer objects using Web Services or if you wanted to store the object in a session variable when your sessions are being stored in a session server. You would never use this to store the object to a file that you want to be able to retrieve some time in the future after restarting your application. That is, you would never use this method to implement the Registry solution I mentioned yesterday.
The Reliable Way
What you need instead is the Serializable Interface, or more correctly, you want to implement ISerializable on your class. Using the ISerializable interface gives you a lot more control. Used correctly, you can store an object to a file system, shut down your application, install an upgrade to the application with additional information (or less information) in the class, and still successfully load the object from the file system. So, let’s illustrate with a simple class:
This is a pretty simple class. It has one member variable that we will use to save some sort of state information and will need to get stored to the stream when we save the object instance. To implement the interface, we need to change the class to look like this:
The thing to watch out for here is that you need a constructor that takes SerializationInfo and StreamingContext as parameters. Since the interface can’t remember this, you’ll have to remember it.
You’ll notice that we’ve taken control of storing and retrieving the data by using the AddValue and GetInt32 calls inside of the new, required, constructor and the GetObjectData method. GetObjectData is called when the data is being saved. The constructor gets called when the data is being loaded.
Protecting For The Future
If we leave the code as it is above, we are no better off than if we had simply implemented the Serializable attribute. What we need to add to this code is some way of versioning the object. This is easy to do by adding a few more lines of code: When we initially create the object, all we need to do is add one line of code to our GetObjectData method
publicDemo(SerializationInfo info, StreamingContext context) { int VERSION = info.GetInt32("VERSION"); m_intVar = info.GetInt32("m_intVar"); if (VERSION > 1) m_stringVar = info.GetString("m_stringVar"); else m_stringVar = "default string"; }
By incrementing the version number every time we make a change to the class and testing for the appropriate version number when we load the object, we can avoid having our application crash simply because the class has changed between the time we save the object and the time we loaded the object from the stream.
I think the VB.NET Ternary Operator may be the last operator that I really miss in VB.NET from my curly brace language experience. Although, I have to admit, I wouldn’t have missed it all that much if they never added it. There just isn’t a whole lot of use for it.
Yesterday we looked at the new var keyword in CSharp. This makes CSharp variable declaration similar to VB. After all, they’ve had the DIM keyword for years which essentially does the same thing.
Today, we’re going to look at object initializers, which have been added to both CSharp and VB.
There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files. It will not significantly reduce the amount of code that we have to write. One of those language features is the ability to create properties, which we looked at last week. Another of those features is the new var keyword. So, instead of writing: