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
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 !)
If Microsoft recommends WordPress …
Hi, thanks for the article, but I’m a little confused as to what your resolution was. Did you create an alternate method that doesn’t use the FileSystemWatcher? If so, how do you handle if the template file changes?
Thanks
Ian
Simple. Download source. Remove FileSystemWatcher. Re-compile.
Thanks for the article. Could you please post a link to download the source for StringTemplate? If I search in Google, I get only Java version and not C# version.
Thanks for your help.
http://www.stringtemplate.org/download.html
C#ST4 port
http://www.antlr.org/wiki/display/ANTLR3/Antlr3CSharpReleases
Thank you so much Andrei for the response.
Currently I’m using VS2008. Here’s the background info. I’m trying to use StringTemplate via codeplex project (http://exporter.codeplex.com/). This one uses StringTemplate library extensively. I’ve been using this wrapped around WCF services. It works fine on my local however when I deployed this on a test box, I got this error from StringTemplate:
Antlr.StringTemplate.TemplateLoadException: Cannot close template file: D:/templates/Collection/Excel2003XML.st —> System.IO.FileNotFoundException: Error reading the D:\Sungard.GlobalPlus\FTB\Server\bin\templates\Collection directory.
at System.IO.FileSystemWatcher.StartRaisingEvents()
at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
at Antlr.StringTemplate.FileSystemTemplateLoader.InternalLoadTemplateContents(String templateName)
— End of inner exception stack trace —
at Antlr.StringTemplate.FileSystemTemplateLoader.InternalLoadTemplateContents(String templateName)
at Antlr.StringTemplate.StringTemplateLoader.LoadTemplate(String templateName)
at Antlr.StringTemplate.StringTemplateGroup.LoadTemplate(String templateName)
at Antlr.StringTemplate.StringTemplateGroup.LookupTemplate(StringTemplate enclosingInstance, String name)
at Antlr.StringTemplate.StringTemplateGroup.GetInstanceOf(StringTemplate enclosingInstance, String name)
at Antlr.StringTemplate.StringTemplateGroup.GetInstanceOf(String name)
Then I googled for “Cannot close template file”, I landed on this page. So based on your research, I thought of 2 options.
A) Give full trust to WCF in IIS which might be not desirable.
or
B) Change the source code for StringTemplate to drop the FileWatcher code (since it uses FullTrust).
As part of codeplex project, it uses v2.0.50727 for StringTemplate.
The link you showed above has only VS2010 version of the source code and not able to open in VS2008.
Any help here is appreciated.
Thanks again, JK
Ok, I solved the issue after find the source file for StringTemplate and commenting your findings and after that it works fine.
Thanks for this post anyway, it pointed me to the right direction.
Great!