Silverlight & WCF projects in different solutions

Getting my WCF Service to talk to my Silverlight app in Visual Studio
 
I have been developing a Silverlight application to get data from a pre-existing WCF Service. The code for the service was already in production and was available in its own Visual Studio solution. In order to debug my Silverlight application, which was running from within a different solution, I needed to add a crossdomain.xml file to the root of the folder where the service was running from. This is necessary even though both solutions were using the ASP.Net Development Server (localhost) on different ports. A full explaination is available on MSDN for Making a Service Available Across Domain Boundaries. Tim Heuer also has a very good explaination of the problem and solution here.

Using different Endpoints in a WCF Service

Avoiding confusion when referencing a Service?
 
Whilst playing with a WCF Service for my Silverlight application I encountered the following error:
 
An endpoint configuration section for contract could not be loaded because more than one endpoint configuration for that contract was found
 
It seems that becuase my client config file contained multiple configuration endpoints, I needed to specify which endpoint to access during the service initialisation. Tim Heuer has a very good blog post on Managing service references and endpoint configurations for Silverlight applications which helped me to figure out this problem.

IE Silverlight vanishing trick!

Not really sure what caused this to happen?
 

I had just configured the VS 2008 development environment on a machine I dont normally use to use the November 2009 version of the Silverlight Toolkit. Having created a new C# Silverlight Navigation Application and put together some basic code I hit F5 and waited for my RIA to fire up so that I could debug it. What I was presented with was dialog box with the error message – ‘Unable to start debugging. Cannot locate Microsoft Internet Explorer.

I hadn’t changed any of my browers settings so I made sure that IE was set as the default browser, I checked that I could open up IE and do some general surfing and I even made sure that the Browse With… worked from within VS. Everything was OK except my ability to run from F5. A few minutes of research pointed me to a (translated) page which highlighted the problem and potential solution.

Navigating to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths I found that the iexplore.exe key was missing. So here’s what I did:

  1. Added the iexplore.exe key to the  registry.
  2. Changed the (Default) REG_SZ value to C:\Program Files\Internet Explorer\iexplore.exe
  3. Added a new REG_SZ value called Path and set it to C:\Program Files\Internet Explorer
  4. Restarted my Visual Studio.

After making these changes, opening my project and hitting F5 it was working again. I have no idea what caused the Key to be removed in the first place but I can only think it was the Silverlight Toolkit installation process that did it???

The virtual path ‘/App_Resources/dev/mycontrol.ascx’ maps to another application which is not allowed

Getting to the root of the problem
 

I had been trying to set up a development environment for Visual Studio so that instead of using the in-built Development Server (Cassini) I could use IIS. Since I had to develop on a Windows XP machine my first problem is that I could not create more that one Web Site under IIS 5.1 so I had to resort to using Virtual Directories for the applications (A Web app and a WCF app). This differed from the live setup where each was configured under its own web site and this is where my problems started. When I fired up the web application I was greeted with this error – The virtual path ‘/App_Resources/dev/mycontrol.ascx’ maps to another application which is not allowed.

The web application used the concept of loading controls from embedded resources using the LoadControl() method and this was causing the error. In the live environment the application is always at the root for the website because of the deployment configuration, however in my scenario the web app was located in a virtual directory off the root web site. The issue occurs because the virtual path ‘/App_Resources/’ assumes that it is at the web site root rather than the application root. To solve the problem I just need to add a tilde (~) to the front of the virtual path in the managed code so it became ‘~/App_Resources/dev/mycontrol.ascx’. I also found instance in the JavaScript code where this situation occurred but this time we just need to use the dot notation to indicate the root folder. The virtual path in the script became ‘./App_Resources/dev/mycontrol.ascx’

For information on loading webforms and usercontrols from embedded resources take a look at http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx

Skype interfering with IIS

I have been working at a client who is using Windows XP on their development boxes and came across this issue the other day when I tried to start IIS – Unexpected error 0x8ffe2740 occurred. I had a look in the System Event log and found an Error for the W3SVC service – The service could not bind instance 1. I like to have a quick look at http://www.eventid.net in these situations as the Event ID (115) will usually have an explanation for what caused the error along with some helpful responses from people who have had the same issue.

My first clue pointed me to this article about the problem which suggested that another service/application was already using port 80. Doing a bit more searching I came across another blog which provided me with a better solution… Skype was getting in the way! Since Skype will use port 80, if its free, it was stopping IIS from using the port. Fortunately this is an option in Skype you can turn off. Just deselect the check box (highlighted below) and then restart IIS.

UPDATE : I have also encountered this issue on a Windows 7 machine I was using for development which was highlighted by this article. The fix to the problem was the same as detailed above.

EntLibConfig.exe

The Enterprise Libarary Configuration Tool
 

If you use Microsoft’s Enterprise Libraries at all then you may have seen this tool before…

I found this tool while doing some research on the Enterprise Library entries in an app.config file and realised it could be very useful for the application I was working on. My problem came when I tried loading the app.config file into the EntLibConfig.exe as it reported that one or more errors had occurred during the loading process. The errors I were getting appeared as "…The located assembly’s manifest definition does not match the assembly reference." at the bottom of the screen. The app.config file was valid as my application had been running without any issues.

My search for an answer took me to this page – http://blogs.msdn.com/tomholl/archive/2007/04/19/avoiding-configuration-pitfalls-with-incompatible-copies-of-enterprise-library.aspx – which explained that I was trying to load an app.config file referencing assemblies that were not signed. Taking a look at my entries in the app.config file for the Microsoft.Practices.EnterpriseLibrary  I found that all the PublicKeyToken values were null. Since the EntLibConfig.exe was using signed assemblies this was the reason for the errors.

My next task was to find out what the Public Key Token value was for the Enterprise Library assemblies. For that I used the Strong Name Utility (sn.exe). This link gave me some useful information about using SN.exe – http://blogs.msdn.com/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named-assembly.aspx

Having entered the correct Public Key Token values into the app.config my configuration file now loaded into the EntLibConfig.exe without any errors and I was able to do the necessary editing.

ForeignKeyReferenceAlreadyHasValueException

Updating a Foreign Key value using LINQ
 

Table A has a Foreign Key (integer) field that is referencing a Primary Key (integer) field in Table B. I wanted to be able to set the value of the integer in Table A to a new value using my entity object property for Table A. When I tried to set the value of the integer field I got a ForeignKeyReferenceAlreadyHasValueException and the message ‘Operation is not valid due to the current state of the object.’

The solution is not to try and set the integer value of the field but define the value for the whole entity. For example;

// Dont assign just the ID value

EntityName.ForeignKeyFieldID = myIntegerValue;

// Assign the entity

EntityName.ForeignKeyField = myEntity;

MSBuild Error 5004

MSB5004 Build Error for VS2008 Solution
 

During an automated build process I came across an error that MSBuild had thrown – ‘Error parsing the nested project section in solution file’. The problem stems from a change to a GUID for one of the Nested Projects within the Solution File. When comparing a Solution File for a successful build to the failed build I found the GUID had changed. To rectify the error I added a blank text file to the solution, saved the changes to the solution file, removed the text file and re-saved the solution file. This resolved the issues as the next build was successful.

GAC & ZAP

Whats the difference between GAC and ZAP?.
 

The GAC holds assemblies installed on the local host that are available for shared use by other code. The Zap Cache is a sub-cache of the GAC that holds assemblies that have been precompiled into native machine code. Zap Cache assemblies are typically Fully Trusted libraries that are used frequently by the CLR itself, so precompilation boosts performance.

If you would like to know more, take a look at http://www.nsa.gov/ia/_files/app/oldFiles/NET_Framework_Sec1.pdf

Unable to find manifest certificate in the certificate store

Addind a Strong Name Key file to an existing project .
 
Whilst trying to add a Strong Name Key file (.snk) to an existing Visual Studio 2008 project I came across this error – Unable to find manifest certificate in the certificate store. Since this was the first time that I had tried adding a key file to the project I couldnt understand why it wasn’t letting me do it? It turns out that someone else had previously been experimenting with Signed Assemblies and there were some entries in the .csproj file that were left over:
 
<manifestcertificatethumbprint>…</manifestcertificatethumbprint>
<manifestkeyfile>…</manifestkeyfile>
<generatemanifests>…</generatemanifests>
<signmanifests>…</signmanifests>
 
After I removed these lines and reloaded the project within Visual Studio I was able to add my Strong Name Key file without any further issues.