Category: solved problems

Generating Word XML – and error

When you generate Word XML file ( with StringTemplate ) or another tool, and you miss some content, Word does not display the error – instead it puts on a specific folder. And it is troublesome to find that file between more files.

So I create this small powershell program to delete all files from that folder – minus the error file, because the word is keep opening:

$folder = [environment]::getfolderpath(“LocalApplicationData “)
$folder = $folder + “\Microsoft\Windows\Temporary Internet Files\Content.MSO”
get-childitem $folder -include *.log -recurse | foreach ($_) {remove-item $_.fullname} # it will not remove the word error file
get-childitem $folder -include *.log -recurse | foreach ($_) { [System.Diagnostics.Process]::Start($_.fullname) }

A programmer day with “That assembly does not allow partially trusted callers”

I make a website, works on local IIS. Performs user registration  + uploading some zip files + generating custom feeds for those zip files.

Using SharpZipLib_0860_Bin ( to unzip file ) , StringTemplate.dll ( to perform custom feed generation ) and NUnit-2.5.7.10213 ( to perform tests).

So far ,so good. Moving into production . User registration works, upload works, trying feeds

“That assembly does not allow partially trusted callers”
Feeds were generated with StringTemplate- it was custom feeds ;-).

Searching , talking with hosting – seeing that this happens if your asp.net does not run under full trust , but under “Medium trust”.

Normally the provider does not want to change and send me advice to put AllowPartiallyTrustedCallersAttribute (APTCA) on the class:
http://msdn.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx

This does not work without signing with a strong name – so I generate the snk file , sign mine assemblies, re-deploy. Same error:

“That assembly does not allow partially trusted callers”

That is normally and I have suspected – because I have to sign the stringtemplate, not mine dll.

Trying to see if the stringtemplate is signed  -it is! When put AllowPartiallyTrustedCallersAttribute  and trying to re-build – failed because I do not have his snk

Then, in disperation, I remove the snk from stringtemplate and re-make my project.

And now –

System.Security.SecurityException: Request failed.

Antlr.StringTemplate.FileSystemTemplateLoader.InternalLoadTemplateContents(String templateName) +0

That means that the files to read contents to generate feeds are not accesible to read. But, oh my dear .NET framework :could you not tell from the beginning ?

So, possible solutions to identify causes :  identify the assembly that  causes the harm and either

1. put a snk to this assembly and for all assemblies in superior chaining

2. remove the snk from this assembly

In either case, you should see what the error is.
So how I managed to solve ?
1. Put Reflector and dissasemble the source.
2. Re-compile without snk.
3. Put in Web.Config 4. Compile and run with debug
Now it stops at :
string templateText = InternalLoadTemplateContents(templateName);

This , in StringTemplate class, was

  protected override string InternalLoadTemplateContents(string templateName)
        {
            string templateText = null;
            string templateLocation = null;

            try
            {
                //templateLocation = Path.Combine(LocationRoot, GetLocationFromTemplateName(templateName));
                templateLocation = string.Format("{0}/{1}", LocationRoot, GetLocationFromTemplateName(templateName)).Replace('\\', '/');
                StreamReader br;
                try
                {
                    br = new StreamReader(templateLocation, encoding);
                }
                catch(FileNotFoundException)
                {
                    return null;
                }
                catch(DirectoryNotFoundException)
                {
                    return null;
                }
                catch(Exception ex)
                {
                    throw new TemplateLoadException("Cannot open template file: " + templateLocation, ex);
                }

                try 
                {
                    templateText = br.ReadToEnd();
                    if ((templateText != null) && (templateText.Length > 0))
                    {
                        //templateText = templateText.Trim();

                        if (filesWatcher == null)
                        {
                            filesWatcher = new FileSystemWatcher(LocationRoot, "*.st");
                            //filesWatcher.InternalBufferSize *= 2;
                            filesWatcher.NotifyFilter = 
                                NotifyFilters.LastWrite 
                                | NotifyFilters.Attributes
                                | NotifyFilters.Security 
                                | NotifyFilters.Size 
                                | NotifyFilters.CreationTime 
                                | NotifyFilters.DirectoryName 
                                | NotifyFilters.FileName;
                            filesWatcher.IncludeSubdirectories = true;
                            filesWatcher.Changed += new FileSystemEventHandler(OnChanged);
                            filesWatcher.Deleted += new FileSystemEventHandler(OnChanged);
                            filesWatcher.Created += new FileSystemEventHandler(OnChanged);
                            filesWatcher.Renamed += new RenamedEventHandler(OnRenamed);
                            filesWatcher.EnableRaisingEvents = true;
                        }
                    }
                    fileSet.Remove(templateLocation);
                }
                finally
                {
                    if (br != null) ((IDisposable)br).Dispose();
                    br = null;
                }
            }
            catch (ArgumentException ex) 
            {
                string message;
                if (templateText == null)
                    message = string.Format("Invalid file character encoding: {0}", encoding);
                else
                    message = string.Format("The location root '{0}' and/or the template name '{1}' is invalid.", LocationRoot, templateName);

                throw new TemplateLoadException(message, ex);
            }
            catch (IOException ex) 
            {
                throw new TemplateLoadException("Cannot close template file: " + templateLocation, ex);
            }
            return templateText;
        }

Does something ring a bell to you ?
Wait for an answer
Wait for an answer
Wait for an answer
Wait for an answer
Wait for an answer
Wait for an answer
Wait for an answer
Wait for an answer

Yes – FileSystemWatcher .
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
[IODescriptionAttribute(“FileSystemWatcherDesc”)]
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = “FullTrust”)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = “FullTrust”)]
public class FileSystemWatcher : Component,

Solution : Once defined my FileSystemTemplateLoader_MT – without FileSystemWatcher – all works well
So – the idea is : reflector, sources, find something strange. Remove, rebuild, re-test
(Thanks to OrcsWeb team for helping me on this problem !)