Error: ‘a reference to could not be added’

For some reason Visual Studio 2010 started referencing some old libraries, thus preventing me from adding an Azure library that I have already been using for some time in other projects. Here is the exact error I was getting:
“a reference to microsoft.windows azure.storage could not be added”

Needless to say, this vague error was very frustrating since I have been using this library for a while.

First thing to check: Using the .Net 4 Client Profile? No.
Are the libraries still there? Yes.

What If i remove and re-add the same library reference that has been working in another Project by referencing it in the “recent” drill down? Result: it couldn’t be added there either after I removed it … You gotta be kidding me!

So I tried browsing for the .dll instead of adding it from the “Recent” list, and sure enough, it was referencing the Azure 1.4 SDK rather than the 1.6 SDK version.

So, in case you are getting this error, try drilling down to the literal path to make sure you’re reference the correct installed library.

Strange Issue Creating Azure Table Records with Custom Properties

I just found out that you have to format your properties “just so,” or the Azure table SaveChanges() method will ignore those properties, even if they are public.

Here is the secret:

I was formatting my custom Azure Table class like this:

public class CustomTableRecord : TableServiceEntity
{
public bool boolVal;
public string strVal;
public CustomTableRecord()
{
strVal = “example default value”;
boolVal = false;
}

}

After looking at the HTTP traffic going over the wire, I noticed that boolVal and strVal were not being included with the Table Storage object that was being uploaded.

Then I changed it to this and it uploaded the properties as expected:

public class CustomTableRecord : TableServiceEntity
{
public bool boolVal { get; set; }
public string strVal { get; set; }
public CustomTableRecord()
{
strVal = “example default value”;
boolVal = false;
}

}

I would not have expected this based on my previous experience serializing simple objects.  Is this a glitch / bug with Azure?  Who knows.  I just hope this can save you the hours that it cost me tracking this issue down.

 

Post a Static Webpage on Amazon Aws (S3)

1. The easiest way to make your bucket publicly available (which will also make it into a cname that you can map  to a custom domain) is to change a few settings through your Amazon S3 account.  After creating your bucket, right click on it and click “Properties,” then click the “Website” tab at the bottom of the page.  There you can assign the default home page and error page (you also have to upload this content, of course).

If you want to assign your bucket to a custom domain (explained in step 3) use a naming convention like this:

public.yourdomain.com” or “www.yourdomain.com

2. Here is the csharp code to create an object that will be immediately available as an html document.

PutObjectRequest putObjectRequest = new PutObjectRequest()
.WithKey(“nestedVirtualDirectory/objectOrFileName.anyExtensionYouWant”)
.WithBucketName(“theBucketCreatedInStepOne”)
.WithContentBody(“<html>webPageOrHtmlcontent</html>”)
.WithContentType(“text/html”)
.WithCannedACL(S3CannedACL.PublicRead);
S3Client.PutObject(putObjectRequest);

If you set up a custom domain (step 3) this object will look like this in the address bar after you upload it:

http://public.yourdomain.com/nestedVirtualDirectory/objectOrFileName.anyExtensionYouWant

3.  To assign a custom domain to your bucket, copy the link available in your Website tab, which looks something like this:

http://public.yourdomain.com.s3-website-us-east-1.amazonaws.com/

or

http://www.yourdomain.com.s3-website-us-east-1.amazonaws.com/

Take this link into the DNS control panel of your domain registrar (GoDaddy’s Dns Control Panel, for example) and then create a CNAME that is the same as the prefix referred to in step 1.

So, if you named your bucket “public.yourdomain.com”, then create a CNAME “public” and then paste this as the host:

“public.yourdomain.com.s3-website-us-east-1.amazonaws.com”

I found that, with a CNAME i set up with my Go Daddy account, this resolved correctly within seconds.

Editor Can Find Namespace and File Compile Message: “Could Not Find Namespace”

This is a somewhat tricky issue that is related to the configuration of the project you are trying to compile in relation to existing projects the the new project is referencing.

For some reason my Visual Studio creates new projects with the Target Framework “.Net Framework 4 Client Profile” rather than the standard “.Net Framework 4.”  The “Client Profile” is a lighter version of .Net that lacks the complete functionality of the .Net Framework.

When I am referencing other projects that are compiled in the standard “.Net Framework 4″ while my new project is set to Client Profile, the compiler cannot find the other projects’ namespaces.  But when I set the new project to “.Net Framework 4″, it finds the namespaces and compiles without any issue.

The really confusing part about this is that, when I feference the existing Projects / Namespaces in the code editor, it finds the namespaces and  underlying classes, methods, etc.  Nevertheless, it still fails at compile time.

I am not sure about why exactly this occurs or how I can make Visual Studio create new Projects with the Target Framework “.Net Framework 4,” so if anyone can offer further insight, please do (in the comments).  In this meantime, however, this fix will do the trick!

The Equivalent of PHP’s fwrite() in Csharp

Sometimes I wish Csharp was more like PHP and visa versa.  For example, it would be nice if I could just write to a file the way that PHP allows with fwrite(), and forget about all the confusing options available for writing to a file in .Net.  Well, for those of you who have been searching for an equivalent of PHP’s fwrite() function, I have good news for you!

The equivalent of PHP’s fwrite() function in .Net is:

System.IO.File.WriteAllText(fullPathToString, textContentYouWantToWrite);

Since you are probably already “using System.IO;” then this command can also be called as:

File.WriteAllText(… , …)

Easy right?

This method creates the file if it doesn’t exist or rewrites the file’s contents if it already exists.  You don’t have to bother with any confusing combination of TextWriter or StreamWriter, or closing the stream when you are done, etc.  This method is actually simpler than PHP’s method since there is no need to create and close a handle to the file you are writing to.