InfoPath 2007 / WCF Nasty Gotcha
I've spent the better part of two days tracking down an extremely nasty bug in InfoPath that corrupts an InfoPath form completely and almost terminally after connecting to a WCF (Windows Communication Foundation) web service. Each time you try to open the form in design mode, InfoPath kicks out the error below and then refuses to open the form.
"InfoPath cannot open the following form: C:longpath oform.xsn. The XML Schema file specified in the form definition (.xsf) file for dataObject 'xyz' cannot be used"
Once this happens, there is no way to get InfoPath to edit the form again. However, there is a fix.
The Fix – Recovering your work
First, we have to get you so you can edit your form again. Since the actual error hides itself completely during the initial design session it is possible to lose a lot of work, depending on how much you worked on the form during the session where you connected it to the web service. Either way, you have to fix both problems in order to get the form working properly anyway.
- Create a new folder and copy your "corrupt" .XSN file to that folder.
- Rename it from .XSN to .CAB (Yes, .XSN is really just a .CAB)
- Use a compression utility that can read CAB files (like WinZIP) to unpack the contents of the file into that folder.
- Open the Manifest.xsf file in a text editor (NOT in InfoPath) and look for "<xsf:dataObjects>" under that node, you will see several <xsf:dataObject> nodes.
- Delete all <xsf:dataObject> nodes that point to WCF web services. You can spot these easily because they usually have ".svc" somewhere in the wsdlUrl attribute of the <xsf:webServiceAdapter…> child node.
- Save the Manifest.xsf file.
- Now right-click on the manifest.xsf and select 'Design'. This should open InfoPath and will give you a warning that the form has been moved.
- Click ok. At this point, the form should open in design properly. You should also see "[Read-Only]" in the title bar.
- Go through all views in your form and disconnect any controls that were bound to the WCF data sources. Sometimes this is as simple as converting them from whatever type they are to text boxes.
- Click File -> Save As and save the file to the folder where you want the file to reside. This will automatically save it as an .XSN file again.
- Close InfoPath (very important- don't just close the form)
- Browse to the the new folder and find the new .XSN file
- Right-click on it and select 'Design'.
- Click 'Preview' to preview the form. If you get the error "InfoPath cannot open the selected form", it means that one of your controls is still bound to one of the data sources you removed earlier. Check your controls again and also check any Rules you may have defined under Tools -> Form Options -> Open and Save -> Open behavior.
At this point, you should now be back editing your form as normal, just without the web service data. Warning: do NOT reconnect to the web service until the next part of the fix is completed. Doing so immediately lands you back at step 1!
The Fix – Fixing the Web Service
This will require the help of the author of the web service who will probably deny that there is an error and try to blame InfoPath because it works in all of his loosely typed test applications. You must convince them that the problem really is with the way the service is defined. Perhaps this entry will help, perhaps not. Either way, he will have to do something with his web service if he wants that data exposed in your form because it won't work as it is right now. I was lucky that the developer of the web service was every bit as curious about this error as I was so we were able to troubleshoot this from both ends.
The root of the problem is that one or more of the underlying data types is defined with an improper Namespace in the [DataContract] attribute or one that doesn't match all of the others. InfoPath is extremely particular and if the namespaces don't match precisely, it will cause the error mentioned at the start of the article. It is case-sensitive.
To track down exactly where the problem lies involves a little bit of detective work and some of the files extracted from Step 3. When you created the Data Connection to the web service call in InfoPath you gave it a name like 'GetSomeDataFromJoe'. If you look in the folder from Step 3, you should see 4 or 5 files called GetSomeDataFromJoeXX.xsd where XX is a number between 1 and 99. These are the actual schema files that InfoPath pulled down when you connected to the web service. Send these to the developer. If he looks through them, he will find one (usually the second in the series) that shows the data types found in the web service. One or more of the types he is using should be missing completely from the XSD files which means that those are the ones with the broken namespace.
After fixing this, the developer will need to build and then publish the web service before you can attempt to connect to it again.
Putting it All Back Together
Now that the developer has assured you that the fix has been made you will need to reconnect to the web service. To reconnect safely, try it this way:
- Backup your current working form (.xsn) from the first part of the fix – just in case the developer hasn't gotten the fix quite right yet.
- Open the form for design in InfoPath
- Reconnect to the web services
- Save the form and close InfoPath
- Reopen the form in InfoPath.
- Switch to Design mode.
- If it opens properly then the developer cleaned up his namespaces and it is safe to reconnect your controls to the web service data.
- If you still get the "… XML Schema file … cannot be used" error, then the problem is still in the web service. Delete the .XSN (because it is now corrupted again) and restore it from your backup. If you jumped ahead and reconnected without backing up, then you get to do this whole thing all over again starting with Step 1 under "Recovering Your Work".
Behind the Scenes (IMO)
The reason for the error is because of the way that InfoPath pulls the schema for a WCF web service. When it connects and pulls down the schema info from the WSDL, it also pulls down all of the schemas for the data types used in the service. In this case, the namespace of one of those schemas didn't match precisely so it couldn't be found later on, which is logical and expected behavior. However, InfoPath itself ignores that error during the session in which you originally connect to the web service. It is not until you close InfoPath and then go back in that you get bitten by it. What would be nice is if InfoPath kicked out the error when you connected to the service rather than laying a trap for you later on.
The truly curious part of all of this is that despite having an invalid schema, you can still open the "corrupt" .xsn file for data entry in InfoPath and it works perfectly. One would think that something as substantial as a broken schema would kill the form no matter what.