Tag Archive: WCF


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!

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.

This error came up today, when I was testing my WCF services for a new website that I am writing. The ultimate issue here is the mapping of the interface to the service in question, and usually points to a possible mistype or rename of something within your service architecture. The primary message received here can be:

“The type ‘OdatServiceAzure.SiteService’, provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found. “

So, how do we conquer this error? Well, if you are debugging a program with a bunch of different services, it’s probably best to debug each service, one by one. Select the service you want to test and run it. If you can run it completely, there shouldn’t be an issue with pairing the interface and service. If there is, you may see an error like that shown above. The primary place you want to look is at the markup for the service itself.

Right click on your service, and click ‘View Markup’.

image

Make sure your namespace and service name here match your service name itself. In my case, I had renamed my namespace due to an issue with it, and my services for some reason, did not rename within the .svc markup.

I have been working on several projects lately that utilize the cloud for many different things, including service, session, database and client hosting. The ultimate goal is to have the flexibility to host solutions in the cloud in a fully available manner that is flexible to allocate more resources when needed. I have run into a case where I would like to pass more information into WCF functions that allows me to confirm caller identity, and manage some other things behind the scenes, without passing this information as parameters.

My original thinking was to somehow extend the functionality of WCF services itself to allow for adding parameters that would be set at initial session start.

An example being:

 

Code Snippet
  1. Service1Client myService = new Service1Client();
  2. myService.itemToPass = "Hello World!";

 

I didn’t really find anything that could meet my needs! It amazes me that there wasn’t more information out there regarding this type of data passing, since it seems a lot more reasonable to include such a offering, instead of passing the same value to all methods within a service. So, I asked on a couple forums and one post suggested possibly using a static variable for this, so I finally found a solution for it!

So on the service level I declared a private static variable for storing the value globally to be available for other methods within the service. I also created a setter method to enable, on setting up a connection, the availability to set this variable.

Code Snippet
  1. public class Service1 : IService1 {
  2. private static int customVar { get; set; }
  3. public void setCustomVar(int CustomVar) {
  4.     customVar = CustomVar;
  5. }
  6. // …

 

You ultimately could pass multiple things through a service, but remember to limit this to variables that MUST be provided globally. So from here, it is just a matter of (on the client) connecting to the service and setting this variable.

 

Code Snippet
  1. Service1Client myService = new Service1Client();
  2. myService.setCustomVar(12345);

 

Now that we have that variable set, it can be utilized for the lifetime of the connection to the service. This is very useful for those variables you want to have available globally, but don’t want to insert this parameter into every call!