Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sep 2, 2011

LinkedIn API - request failed with 401 - unauthorized (cause #1 of N)


If you experience that, make sure you are encoding your request properly. For example http://api.linkedin.com/v1/people-search:(people:(id,first-name,last-name,picture-url,headline,location:(name)),num-results)?keyword=" + HttpUtility.UrlPathEncode(user.FullName).

In the above example if the user full name is Pavel Donchev, if you use HttpUtility.UrlEncode(user.FullName) the result will be Pavel%Donchev.
It should be Pavel%20Donchev, which is result of calling HttpUtility.UrlPathEncode instead HttpUtility.UrlEncode.

Almost unnoticeable difference mixed with generic message that may cause lots of head aches for you.

Aug 9, 2011

A book is born!!!

As most of you know, I was involved in writing a book about C# programming (we were 30 authors, each had to write a chapter and edit a chapter).You can click on its cover in order to be taken to download the PDF version:

Hope you will enjoy it.

Oh, almost forgot – it is in BULGARIAN Winking smile. There were some plans to have it translated but as I am not a part of telerik anymore, I don’t have any information from the kitchen how far this project went.

Please be sure to say a great thanks to telerik for sponsoring this book!

Jul 14, 2011

Application suddenly becomes slow

I experienced this while I was developing my application in Visual Studio.
It happened for no apparent reason (I didn't remember to make any signifficant changes in this application).

It was really frustrating. The thought that I may need to rething the whole application was a nightmare.

I was in panic and started to think what the problem for this sudden degradation might be.

And guess what?
It appeared to be because of a conditional breakpoint I have set in one of the complex modules.

The program flow was passing through this line of code hundreds of thousand of times so Visual Studio was kind of slow determining if the condition for the breakpoint is true or not.

So remember this for the rest of your life:
"Conditional breakpoints may slow your application considerably, before doing stupid things in case of sudden performance degradation - be sure to remove your breakpoints first (Debug Menu => Disable All Break Points), then check the performance again.".

Jun 28, 2011

Visual Studio AddIn - never put dots in a command.

I was following an article on how to create a command and add it to the Code Window context menu, it was going really well until some point.

I ran the code and Visual Studio was crying with E_INVALID_ARGS error on the

this._applicationObject.DTE.Commands.AddNamedCommand

This one is widelly used and pretty standard method so I thought the problem is somewhere in my code.

I realized that almost all the parameters I am passing to this method are pretty common as well so the problem must be at ....

Well, the command name I used was containing dots, that's why the error.
If you receive this message, please check the command name for dots (the second parameter you are passing to this method).

Since Visual Studio and other addin types seem to be working with very similar framework (the so called "extensibillity framework"), I suspect this rule is also applicable for other types of addins such as Outlook, Excel, Word, etc.

WebForm_InitCallback is not defined error (and some resolutions)

I was happilly working while all the sudden I started to get this message.
It took some googlin' in order to understand what is going on.

It was because my development server date was wrong (few months in the past).
Synchronizing it resolved the issue.
Also I read you may experience this problem in case you are changing the current thread culture.

Hope this helps ...

Jun 10, 2011

Convert string to a known type (using its default converter)

I was in a need to get a type, instantiate it and then fill its properties (I'm currently doing an engine that will give me an instant CRUD UI).

So what I basically needed was to be able to fill a new object instance out of a Hashtable (the key is the name of the property and the value is its string representation).

I wanted to be able to convert that string value into the type it needs to be.
Here is some code to help you get it:

foreach(string strPropertyName in htValues.Keys)
{
PropertyInfo pi = entity.GetType()
.GetProperties()
.ToList()
.FirstOrDefault(property => property.Name == strPropertyName);
}
(this is kind of pretotype so don't blame me if you don't like the code :).

In the above code I am iterating in the keys of the hashtable, so if I have an user object it will strPropertyName will hold "FirstName", then "LastName".
So far so good, I am able to fill the object and send it to the EntityFramework for DB Persistance.

The problem was when the strPropertyName was holding "BirthDate" as it is a DateTime and the EntityFramework or the Reflection (not quite sure which one) was crying it can't Convert the string to the respective type for me.

Here is how I solved the problem:
// Create a new Instance of the given type.
object entity = Activator.CreateInstance(this.EntityType);
// Iterate through all the properties.
foreach(string strPropertyName in htValues.Keys)
{
// Get the property with that name:
PropertyInfo pi = entity.GetType()
.GetProperties()
.ToList()
.FirstOrDefault(property => property.Name == strPropertyName);
// Get the default type converter for the given property type:
TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType);
// Set the value by converting non-values to the given type
// and setting null where it is a null value.
pi.SetValue(entity, htValues[pi.Name] == null ? null : converter.ConvertFromString(htValues[pi.Name].ToString()), new object[] { });
}

Basically the thing you (and I!) need to remember is that the TypeDescriptor class has a GetConverter() method that takes a type and returns a TypeConvertor for that type.

Now in order to be able to convert from string to that type you just need to call .ConvertFromString(string value) of the TypeConverter returned by the TypeDescriptor.

Hope this helps someone out there. To me it seems it can give you a great deal of abstraction and I (for some reason) believe this is heavilly used in nowadays OR/M's.

Jun 9, 2011

Get the underlying type out of a Nullable

It appeared to be really simple actually, this post title suggest the solution:

Type nonNullableType = null;
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
nonNullableType = new NullableConverter(property.PropertyType).UnderlyingType;
else
nonNullableType = property.PropertyType;

What we actually do is to check if the type is nullable or not, if so - we are getting its Underlying type, and if not - we are getting the type itself.

Check if a type is Nullable or not

I was in a need to determine if the type of PropertyInfo is a nullable or not.
Here is what I've found on MSDN about this:

property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)

So you basically need to determine if the type is generic (makes sense as nullable is a type that takes T (Nullable)) and then to check if the type is a Nullable then.

Now, I really wondered do I really need both checks?
As it was coded that way at MSDN I first didn't look at that, but I wanted to tackle it a bit more.

First I thought that the first one is needed to protect you from the null value returned by the GetGenericTypeDefinition() method.
But what's the problem even if it does?
Well, you get InvalidOperationException saying "This operation is only valid on generic types.".

So you definetelly need both checks ;).
It is kind of ugly for me, maybe it will be better to create an extender to the Type type. Something like "IsGeneric()", but the Is prefix always suggests property while this is a method.

Anyway, I'll go with that.
In the next post I'll show you how to get the type out of a Nullable<>.

May 27, 2011

Programming - Rule #1 follow your data from the UI to the Database and back

It can save you a lot of time :).
I had to fix a bug with a value not updating on the UI.
I attached with the debugger and followed the code to the Save() method of the manager.
Then I started looking for some problems in the database (asynchronous triggers failing etc.).

I wasn't quite sure where the problem is for a while.
Then I decided to hit F11 on the Save() method of the manager with the hope to see what query it was generating.

Guess what?
It wasn't the query but the fact that the Manager was overriding the Base Manager Save() method, as it needs to convert a DTO object to DataObject in order to save it.
Well I didn't even need to read through all the properties to know for sure that the property that wasn't updating was missing in the list of assignments in the convert method (in order to defend myself I would say that 99.9% of our managers aren't overriding save methods, it is very unlikely so I didn't considered that to be the problem).

Happy coding and remember Assumptions are your worst enemy :). They are the things that waste most of your time.

May 26, 2011

Serializing object to JSON returns empty string?

Consider the following code:

string JSONObject = string.Empty;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(this.GetType());
using (MemoryStream ms = new MemoryStream())
{
    serializer.WriteObject(ms, this);
    // Donchev - we need to reset because the write has gone to the end of the stream
    // and the StreamReader.ReadToEnd() won't seek to the beginning of the stream again.
    using (StreamReader sr = new StreamReader(ms))
    {
        JSONObject = sr.ReadToEnd();
        sr.Close();
        sr.Dispose();
    }
    ms.Close();
    ms.Dispose();
}
return JSONObject;

It seems perfectly correct but have a few problems.

And the first one is very important – it is based on wrong assumption and it is returning empty string because of that.

I will let you tackle it for a while to see if you can find where the problem is.

Ready?

If not – I’ll tell you – the problem is that when I wrote this code, I was expecting the ReadToEnd() method of the StreamReader class to auto reset the stream to its beginning, it sounds for some reason to me that this method it is saying - “Read the whole thing.”.

Unfortunately this assumption evaluated to false.

The method actually says - “Read from where you are to the end of the thing.”.

So before calling JSONObject = sr.ReadToEnd();

I needed to simply call:

ms.Seek(0, SeekOrigin.Begin);

In order to reset the pointer to the beginning of the stream.

Voilla!

The other question I asked myself is why the pointer is at the end of the stream (after the string is empty and not some portion of the stream, it should be at the end).

The answer – the serializer.WriteObject() method is probably not resetting it to be at the beginning after it completes its job.

Not quite sure who is responsible for resetting the stream – the caller or the callee, we can have a long discussion about this.

Anyway – just wanted to share this with you. I know it is very old thing but I am trying to write for much of the things that made an impression on me.

May 9, 2011

Very old stuff – convert System.Drawing.Color to HTML.

When I was with Delphi, I wrote utility to do this myself (I mean Delphi in its early days) as I wasn’t aware for built-in mechanism to achieve that.

Now with .NET there is utility to do this for you but I often forgot which class has static method to convert System.Drawing.Color to Html string.

Here it is:

System.Drawing.ColorTranslator.ToHtml(color)

I think wouldn’t it be smarter to have an extender attached to System.Drawing.Color to do this for me from now on … :).

Jul 19, 2010

Site is recycled + ThreadPool.QueueUserWorkItem


Just to let you know that you may experience this issue.

If you have an unhandled exception it may be because of a job queued using ThreadPool.QueueUserWorkItem method.

Actually it appears that although the exception is not affecting the main thread (it was in another one) - the .NET will kill the running process (w3wp in our case).

As far as I read this behavior is implemented since .NET Framework 2.0.
It is because you may miss unhandled exceptions in a child thread (if it's in the main thread the process will be terminated in a windows application).

I imagine each request to a web server as a separate thread so I am kind of worried why unhandled exceptions aren't killing the W3WP process as well.
It only seems to be killed if you create another thread in the request thread and this another child thread exits with unhandled exception.

Will need to investigate a bit more about that.

Jul 14, 2010

Thread was being aborted. Exception on Response.Redirect()

This is actually an old issue but I try to make sure I have most of the things that stopped me at some point so I can easilly search.

So if you experience the "Thread was being aborted." exception on Response.Redirect(string Url), it is because this method internally calls Response.End().

In order to prevent it to do so, you will need to use the overloaded version that requires another boolean parameter (something like "bool endResponse", don't remember it exactly).

So the thing that should solve it is:

Response.Redirect("http://donchevp.blogspot.com", false);
This way you should avoid "Thread was being aborted.".

Hope I helped a bit ...

Apr 15, 2010

String.Format

Although very convenient for example for making templates and then replace with some values, it may cause tricky problems.

I've reached a code where it was used to add some data to a javascript.
So javascript uses a lot fo curly braces, which are at the same time used for string format to know where to put its values.

There is a way to escape those braces, buy double them, so for example :
function {}
will become
function {{}}

Recently I had the "String was not in a correct format." exception. I tried to debug and found nothing wrong with the string and the values that will be placed.
That was because I was expecting the values to be wrong (null references, something like this) and not the string itself.

Guess what did I discovered after a few minutes of debugging?
It was the string that was corrupted. The problem was that this particullar javascript was in the .ascx file. Someone probably hit the CTRL + K + D so the source was re-formatted, addding some white spaces in between the doubled curly braces.

Be warned that string.Format is kind of fragile when it comes to files that may involve curly braces.

Feb 18, 2010

How do you unit test encryption?


I wrote two extenders to be used on the string type.
So you can say "test".Encrypt() and "encrpyptedtest".Decrypt().

I was wondering how to unit test this thing.
The first approach which was really obvious was to encrypt the string and then decrypt it, if you get the string before you do those two actions on it - the encryption is correct.

The question in this approach is : What do you test? do you test the encryption or the decryption?
You can get a false positive result this way. For example if the Encrypt method returns the same string without changing it and the Decrypt method returns the same string without changing it - the test will be passed (the string after encrypt -> decrypt will be the same as it was before).

The other approach is to hardcode encrypted and decrypted string, then pass the decrypted string to the Encrypt() method and see if the result matches the encrypted string.
Then pass the encrypted string to the Decrypt test method and see if it matches the decrypted string.
The question here is "What happens if someone change either the key or the initialization vector for the encryption?".
The test will fail as the strings no longer match (unless you are very very lucky to hit some collision case :), but the methods are valid and they work correctly.

For now - I will use the second method. I will hardcode an encrypted / decrypted pair as well as the key and the IV used for the encryption / decryption.
I will first check if the key and the vector match the one that were used to generate the pair and if they don't - the test will be inconclusive so the guy who changed either the key or the vector can easilly find the problem and resolve it.

Anyway I am really interested in how would you do such a task?

Aug 25, 2009

Determine if your site is running under IIS or Casini

For the people who don't know - Casini is the built in Visual Studio server which is by default running when you debug ASP.NET project.
You can use the following to check if the site is running under IIS:

bool isUnderIIS = this.Request.ServerVariables["SERVER_SOFTWARE"].IndexOf("IIS") > -1;

Not the most clever thing I've ever done but seems to be the only way at the moment.
If you know a better way - let us know...

To clarify - IIS will return something like : "Microsoft-IIS/5.1", while Casini will let you bump your head with
string.Empty.
Anyway, this covers my scenarios 100% so I can use it.

Aug 2, 2009

Work in a cooperation with your compiler. Always tell the truth about your datatypes!

When you have a cast you should avoid asking the compiler to figure out what kind of data should be returned. This is more about objects and the string data type.
For example I've seen (and to be honest I myself did the following mistake when getting data from DataSet. Don't laugh in .NET 1.1 and 2.0 there was no Linq :)

foreach (DataRow row in dsReportSource.Tables[0].Rows)
{
string strName = row["name"].ToString();
}

The compiler will let you think that it knows how to do the fastest transformation from object (in the dataset all the columns in a row are objects) to the destination type (in our case - string). But it will only let you think so. What will actually happen is that the compiler will slow down the data acquisition due to some internal checks and conversions ...

Instead do the following (as you are more than sure that this column is string):

foreach (DataRow row in dsReportSource.Tables[0].Rows)
{
string strName = (string)row["name"];
}

This way you tell the compiler - "Look, don't even bother to try to infer the type for me. I am telling you this is a string, so please just treat it as string). And it makes sense - after you have a column of type NVARCHAR in the database it will always come to you as a string (masked as object :).

Now there are even worse cases like:

foreach (DataRow row in dsReportSource.Tables[0].Rows)
{
int iId = int.Parse(row["id"].ToString());
}

As you can see you are parsing the id column. You know it's int. But int.Parse can only work on strings, so you are required to turn this object to string, calling its .ToString() method. This will slow down your code insignifficantly on few rows and signifficantly on more rows (few thousands for example).

The following is also wrong:

foreach (DataRow row in dsReportSource.Tables[0].Rows)
{
int iId = Convert.ToInt32(row["id"]);
}

Here you are telling your compiller that you are sure the parameter (row[
"id"]) will cast to Int32 without problems.
But here you are telling your compiller only 50% of the truth. After you KNOW it is int, just be honest and say:

foreach (DataRow row in dsReportSource.Tables[0].Rows)
{
int iId = (int)row["id"];
}

This applies not only to DataSets but everywhere you have a fixed datatype and you know what the type is.


Don't let your compiler do the work.
Just look at your project and try to find places where you are uncertain about fixed datatype.
Change it to be a direct cast and I am pretty sure you will experience speed improvements.

May 19, 2009

5 minutes C# - get windows special folders path

Just a two lines of code in order to retrieve the path to a special folder of your choice (My Documents, ApplicationData etc):

string strAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
DirectoryInfo diMyDocuments = new DirectoryInfo(strAppData);

Here is the Environment.SpecialFolder Enum:

Environment.SpecialFolder.ApplicationData - the data which some applications store about the current user (mostly used for personalized settings), this is for roaming users.
Environment.SpecialFolder.CommonApplicationData - Application data which is common for all users.
Environment.SpecialFolder.CommonProgramFiles - "The directory for components that are shared across applications"
Environment.SpecialFolder.Cookies - The folder where the user cookies are stored.
Environment.SpecialFolder.Desktop - The logical desktop.
Environment.SpecialFolder.DesktopDirectory - this is the directory which is used to store file objects on the desktop.
Environment.SpecialFolder.Favorites - Current user favorite items.
Environment.SpecialFolder.History - Current user Internet History.
Environment.SpecialFolder.InternetCache - Internet cached files.
Environment.SpecialFolder.LocalApplicationData - As the first one but for non-roaming users.
Environment.SpecialFolder.MyComputer - I'll let you figure this out ...
Environment.SpecialFolder.MyDocuments - I'll let you figure this out too ...
Environment.SpecialFolder.MyMusic - ... and this one ...
Environment.SpecialFolder.MyPictures - ... and this one ...
Environment.SpecialFolder.Personal - this one is more important, this directory is used as a repository for documents...
Environment.SpecialFolder.ProgramFiles - this one is clear, I believe
Environment.SpecialFolder.Programs - Contains user program groups.
Environment.SpecialFolder.Recent - Users most recently used documetns.
Environment.SpecialFolder.SendTo - This directory is used for the Send To > item in the context menu. By adding folders here, they will appear in the Send To -> item when you click on a file or folder.
Environment.SpecialFolder.StartMenu - Contains the Start Menu items.
Environment.SpecialFolder.Startup - The Startup group (Start Menu -> All Programs -> Startup).
Environment.SpecialFolder.System - The system directory (%Windir%\System32)
Environment.SpecialFolder.Templates - Document templates repository.


Hope I helped a bit.
Have a great coding.

Apr 6, 2009

Using JavaScript functions over a string in C#.

I needed to find a way to invoke the real escape / unescape javascript function over a string in C#.
It appeared to be easy to do so by using the Microsoft.JScript namespace classes.

Add a reference to it and then you can simply do something like this:

string escaped = Microsoft.JScript.GlobalObject.escape("This will be JS escaped for sure!");

"escaped" variable will then have the value of :

This%20will%20be%20JS%20escaped%20for%20sure%21

I am still very new to this namespace, there were a lot of things to find out there ...

Check it out. It may appear to be very useful for those of you who are processing JS on-the-fly...

Dec 14, 2008

Nice little tool to help me with twitter.

I created a small twitter software what it does is to check each hour if I am listening to Winamp, get the song if possible and write it in twitter for me and the people who are eventually interested in what I am listening.
Here is the deal:



You can download it from here:
TWamp - Nice little twitter software

You can follow me on twitter from the following url:
My twitter profile

Please note: TWamp isn't very user friendly. You need to get along with it in order to use it ;).