Tag Archive: C#


When working with WCF services, as well as, Azure storage, you sometimes will need to install support packages for certain dependencies, as well as, SDK’s. These sometimes can make some configuration alterations to cause issues with current services you already have configured. Such was the case with a set of WCF services that I had written, and began debugging.  The error stated:

 

image

Ultimately, this issue really is more of an annoyance for testing, more than anything. The important thing is, it is easy to fix! Depending on what version of .NET you are writing against, differentiates where this will need to be fixed. In my case, it was:

\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config

You can search the file for the <client> block. Once found, just comment out the entire block, so if in the future you need it, you can uncomment it. After saving the file, go ahead and try retesting your WCF file, and you should no longer get the error!

Advertisements

Within WCF services, there are many different return types that you can use in order to get data from different dependencies within a service. One problem, though, when working with WCF you might run into, is that WCF doesn’t support usage of a Dictionary as a return type. Although there are many reasons for this (which I will not go into now), but there are also different ways of dealing with this. All the examples I found online seemed like reasonable options, but I also had a different idea.

Since WCF allows for a List return type, why not convert a dictionary to a list, return it, and convert it back into a dictionary? Having two methods, one that converts from a dictionary to a list, and another that converts back, would make this very easy to do.

Below are two methods for doing just this!

Code Snippet
  1. private static string separationString = "!|!";
  2.  
  3.     public static List<String> fromDictionaryToList(Dictionary<Int32, String> dictionaryToConvert)
  4.     {
  5.         List<String> returnList = new List<String>();
  6.  
  7.         foreach (KeyValuePair<Int32, String> i in dictionaryToConvert)
  8.         {
  9.             String stringToAdd = i.Key.ToString() + separationString + i.Value.ToString();
  10.             returnList.Add(stringToAdd);
  11.         }
  12.  
  13.         return returnList;
  14.     }
  15.  
  16.     public static Dictionary<Int32, String> fromListToDictionary(List<String> listToConvert)
  17.     {
  18.         Dictionary<Int32, String> returnDictionary = new Dictionary<Int32, String>();
  19.         string[] separator = new string[] { separationString };
  20.  
  21.         foreach (String i in listToConvert)
  22.         {
  23.             string[] convertedStrings = i.Split(separator, StringSplitOptions.None);
  24.             Int32 itemInt;
  25.             Int32.TryParse(convertedStrings[0].ToString(), out itemInt);
  26.             String itemString = convertedStrings[1].ToString();
  27.             returnDictionary.Add(itemInt, itemString);
  28.         }
  29.  
  30.         return returnDictionary;
  31.     }

So, how do we test this? Pretty simple… just create a new dictionary with some values, send it through both, and debug to see what the values are. Here is an example of the pageload of an aspx form:

Code Snippet
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         Dictionary<Int32, String> dictionaryTest = new Dictionary<Int32, String>();
  4.  
  5.         dictionaryTest.Add(1, "Clearly The First Dictionary Item");
  6.         dictionaryTest.Add(2, "The Second Dictionary Item");
  7.  
  8.         List<String> convertedDictionary = fromDictionaryToList(dictionaryTest);
  9.  
  10.         Dictionary<Int32, String> convertedList = fromListToDictionary(convertedDictionary);
  11.  
  12.     }

 

So, once we setup a debug point, and run our project, it should stop at the end of the Page_load function. Once running, we can see that the values come through, no problem.

debug

 

Although I implemented this with a string and integer, since Dictionary is a generic class, you can specify whatever types you need to implement. You will then need to modify the functions to support your desired data-type.