[Conditional("DEBUG")]
void Foo() {
}

The advantage here is that the code is not reachable anymore if the condition is not met. This means that it will result in compile errors if you reference this method. #if … #endif does not enforce this constraint.

When using Http, proxy configuration madness is a fact of life. If misconfigured, you can wind up trying to decipher obtuse 502 (Bad Gateway) and 504 (Gateway Timeout) errors.

Fortunately there are a number of binding settings available in WCF to control your Http proxy usage. These settings are available directly on BasicHttpBinding and WsHttpBinding (as well as on HttpTransportBindingElement when you are using a CustomBinding).

* public bool UseDefaultWebProxy (default == true): if set to true, will use the global value HttpWebRequest.DefaultProxy as your proxy. HttpWebRequest.DefaultProxy is controllable through System.Net config, and defaults to using the system proxy settings (i.e. when you see in your Internet Explorer properties).

* public Uri ProxyAddress (default == null): If you want to specify a proxy directly you can set a proxy Uri directly here. To ensure no proxy is used you can specify “null” here. In both cases be sure to set UseDefaultWebProxy = false as well.

* public bool BypassProxyOnLocal (default == false): used in conjunction with ProxyAddress for when you specify a proxy. If you want “local” addresses (i.e. addresses on your intranet) to connect directly without using the proxy, set this value to true.

* public HttpProxyCredentialType HttpTransportSecurity.ProxyCredentialType (default == None): Specifies the authentication mode used with your Http proxy. For custom bindings the equivalent setting is public AuthenticationSchemes ProxyAuthenticationScheme (default == Anonymous) on HttpTransportBindingElement. For proxy authentication we will obtain the credential using the shared WCF provisioning framework (SecurityTokenProvider, etc). We simply pass in the proxy address (rather than the target address) for acquiring these credentials.

One last note about proxies: if you see a 502 or a 504 error returned to your client, then your client is using a proxy server. If this was not your intention, you can disable the server by setting UseDefaultWebProxy to false, and using the default ProxyAddress of null. The other possibility is that your proxy is misconfigured and you can use the above settings to rectify that situation.

Versioning with the Override and New Keywords (C# Programming Guide)

Anyway, I think keyword new for versioning should be avoided as much as possible..
Here is small example:

	public static void RunSnippet()
	{
		Child child = new Child();
		child.Run();
		Intermediate intermediate = new Child();
		intermediate.Run();
		Base bs = new Child();
		bs.Run();
		IClass interf = new Child();
		interf.Run();
		IClass ch2 = new Child2();
		ch2.Run();
	}

	public class Child2 : Intermediate, IClass
	{
		public new void Run()
		{
			WL("Child");
		}
	}

	public class Child : Intermediate
	{
		public new void Run()
		{
			WL("Child");
		}
	}

	public class Intermediate : Base
	{
	}

	public class Base : IClass
	{
		public void Run()
		{
			WL("Base");
		}
	}

	public interface IClass
	{
		void Run();
	}

which produces output:

Child
Base
Base
Base
Child

An interesting resource with many free books about programming and more..

Books directory

I want to share this comprehensive blog article by John Robbins. No need to paraphrase the title, it perfectly describes the content already: PDB Files: What Every Developer Must Know.

Sometimes it is helpful to verify a change to isolated storage using the file system of the operating system. Developers might also need to know the location of isolated storage files. This location is different depending on the operating system. The following table shows the root locations where isolated storage is created on a few common operating systems. Look for Microsoft\IsolatedStorage directories under this root location. You must change folder settings to show hidden files and folders in order to see isolated storage in the file system.

Operating system Location in file system
Windows 98, Windows Me – user profiles not enabled Roaming-enabled stores =

<SYSTEMROOT>\Application Data

NonRoaming stores = WINDOWS\Local Settings\Application Data

Windows 98, Windows Me – user profiles enabled Roaming-enabled stores =

<SYSTEMROOT>\Profiles\<user>\Application Data

Nonroaming stores = Windows\Local Settings\Application Data

Windows NT 4.0 <SYSTEMROOT>\Profiles\<user>\Application Data
Windows NT 4.0 – Service Pack 4 Roaming-enabled stores =

<SYSTEMROOT>\Profiles\<user>\Application Data

Nonroaming stores =

<SYSTEMROOT>\Profiles\<user>\Local Settings\Application Data

Windows 2000, Windows XP, Windows Server 2003 – upgrade from NT 4.0 Roaming-enabled stores =

<SYSTEMROOT>\Profiles\<user>\Application Data

Nonroaming stores =

<SYSTEMROOT>\Profiles\<user>\Local Settings\Application Data

Windows 2000 – clean install (and upgrades from Windows 98 and NT 3.51) Roaming-enabled stores =

<SYSTEMDRIVE>\Documents and Settings\<user>\Application Data

Nonroaming stores =

<SYSTEMDRIVE>\Documents and Settings\<user>\Local Settings\Application Data

Windows XP, Windows Server 2003 – clean install (and upgrades from Windows 2000 and Windows 98) Roaming-enabled stores =

<SYSTEMDRIVE>\Documents and Settings\<user>\Application Data

Nonroaming stores =

<SYSTEMDRIVE>\Documents and Settings\<user>\Local Settings\Application Data

Windows Vista Roaming-enabled stores =

<SYSTEMDRIVE>\Users\<user>\AppData\Roaming

Nonroaming stores =

<SYSTEMDRIVE>\Users\<user>\AppData\Local

20. That’s weird….
19. It’s never done that before.
18. It worked yesterday.
17. How is that possible?
16. It must be a hardware problem.
15. What did you type in wrong to get it to crash?
14. There is something funky in your data. OR It’s a data problem, not a program problem.
13. I haven’t touched that module in weeks!
12. You must have the wrong version.
11. It’s just some unlucky coincidence.
10. I can’t test everything!
9. THIS can’t be the source of THAT.
8. It works, but it hasn’t been tested.
7. Somebody must have changed my code.
6. Did you check for a virus on your system?
5. Even though it doesn’t work, how does it feel?
4. You can’t use that version on your system.
3. Why do you want to do it that way?
2. Where were you when the program blew up?

And the Number One Thing Programmers Say When Their Programs Don’t Work:

1. It works on my machine.

In some cases, you may want to access this configuration file programmatically, but the question here is how to know the path of the configuration file without hard coding? After some investigation, I just found a solution to the question, that is using the property: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, it will return the file path of the configuration file. For example, the following C# code loads the configuration file into a XMLDocument object:

XmlDocument doc = new XmlDocument();
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

The Install

First you will need to download and run the System.Data.SQLite installer. This is an open source ADO.NET provider for SQLite. The install package has the necessary assemblies and Visual Studio plugins to allow you to add a server connection to a SQLite database. Run the installer and follow the wizard.

Tools

The tool that comes with Visual Studio Integration is nice, but sometimes I don’t want to fire up Visual Studio to inspect my SQLite database. I use SQLite Manager as my external managment tool. It is a Firefox plugin that works very nicely and has all the commands necessary to create, update, and manage your SQLite instance.

Code

Time to write some code to access your newly created database. You should have used Visual Studio or SQLite Manager to create a database with a schema. SQLite does not support stored procedures so you are limited to LINQ or writing SQL within code. It kind of smells to have SQL in code, but if you seperate your concerns (three tier architecture) you should be able to contain the smell.

My repository takes a SQLiteConnection. A SQLiteConnection points directly to your file. To Initialize your connection it looks like this.

var connectionString = “Data Source=MyFile.sqlite;Version=3;”;
var connection = new SQLiteConnection(connectionString);
public SQLiteRepository(SQLiteConnection connection)
{
_connection = connection;
}

Now how about executing some sql against the database? Well let’s see how it’s done but you won’t be suprised much, it looks like regular ADO.NET. The variable SqlSelectAll is a regular Select query written in SQL syntax.

public IQueryable< Message > GetAll()
{
var messages = new List();
try
{
_connection.Open();
var command = new SQLiteCommand(SqlSelectAll, _connection);
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var message = new Message();
message.Id = (long) reader["Id"];
message.Subject = (string) reader["Subject"];
message.Text = (string) reader["Text"];
messages.Add(message);
}
}
}
finally
{
_connection.Close();
}
return messages.AsQueryable();
}

The one gotcha is that you must remember to Open the connection or else your attempt to retrieve data will fail miserably.

How about a parameterized query execution sample? Well I got one for that as well. Again the SqlDelete variable is just a simple delete in SQL syntax.

public void Remove(Message message)
{
try
{
_connection.Open();
var command = new SQLiteCommand(SqlDelete, _connection);
command.Parameters.Add(“@Id”, DbType.Int32).Value = message.Id;
command.ExecuteNonQuery();
}
finally
{
_connection.Close();
}
}

Conclusion

Compact databases are just the right tool in a lot of instances, especially when building windows based applications but that doesn’t mean they aren’t also great for the web. Look at your data saving needs and you’ll probably realize you could probably go with SQLite very easily.
Update – March 10th 2009

If you are developing for a x64 based system don’t forget to use the proper assemblies when deploying your projects. The current SQLite libraries are compiled for x86 systems and will not work in a x64 system. Read more about it at the SQLite ADO.NET Forums .

This configuration can be added to .NET config file to specify folders where assemblies can be searched:

<runtime>
<assemblyBinding>
<!-- separate assembly paths. These are hints to help fusion when it gets confused. -->
<probing privatePath="Drivers\Oracle"/>
</assemblyBinding>
</runtime>

« Articoli Precedenti    Articoli Successivi »
Answer to Life, the Universe, and Everything is based on WordPress platform, RSS tech , RSS comments design by Gx3.