<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Programmer with Microsoft tools &#187; solved problems</title>
	<atom:link href="http://msprogrammer.serviciipeweb.ro/category/solved-problems/feed/" rel="self" type="application/rss+xml" />
	<link>http://msprogrammer.serviciipeweb.ro</link>
	<description>A programmer journey through code, books and tools</description>
	<lastBuildDate>Mon, 14 May 2012 06:59:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A programmer day with &#8220;That assembly does not allow partially trusted callers&#8221;</title>
		<link>http://msprogrammer.serviciipeweb.ro/2010/10/25/a-programmer-day/</link>
		<comments>http://msprogrammer.serviciipeweb.ro/2010/10/25/a-programmer-day/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 04:29:00 +0000</pubDate>
		<dc:creator>Andrei Ignat</dc:creator>
				<category><![CDATA[solved problems]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[medium trust]]></category>

		<guid isPermaLink="false">http://msprogrammer.serviciipeweb.ro/?p=492</guid>
		<description><![CDATA[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, [...]]]></description>
			<content:encoded><![CDATA[<p>I make a website, works on local IIS. Performs user registration  + uploading some zip files + generating custom feeds for those zip files.</p>
<p>Using SharpZipLib_0860_Bin ( to unzip file ) , StringTemplate.dll ( to perform custom feed generation ) and NUnit-2.5.7.10213 ( to perform tests).</p>
<p>So far ,so good. Moving into production . User registration works, upload works, trying feeds </p>
<p>&#8220;That assembly does not allow partially trusted callers&#8221;<br />
Feeds were generated with StringTemplate- it was custom feeds <img src='http://msprogrammer.serviciipeweb.ro/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>Searching , talking with hosting – seeing that this happens if your asp.net does not run under full trust , but under “Medium trust”.</p>
<p>Normally the provider does not want to change and send me advice to put AllowPartiallyTrustedCallersAttribute (APTCA) on the class:<br />
<a href="http://msdn.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx">http://msdn.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx</a></p>
<p>This does not work without signing with a strong name – so I generate the snk file , sign mine assemblies, re-deploy. Same error:</p>
<p>&#8220;That assembly does not allow partially trusted callers&#8221;</p>
<p>That is normally and I have suspected – because I have to sign the stringtemplate, not mine dll.</p>
<p>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</p>
<p>Then, in disperation, I remove the snk from stringtemplate and re-make my project.</p>
<p>And now -</p>
<p>System.Security.SecurityException: Request failed.</p>
<pre>Antlr.StringTemplate.FileSystemTemplateLoader.InternalLoadTemplateContents(String templateName) +0</pre>
<p>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 ?</p>
<p>So, possible solutions to identify causes :  identify the assembly that  causes the harm and either</p>
<p>1. put a snk to this assembly and for all assemblies in superior chaining</p>
<p>2. remove the snk from this assembly</p>
<p>In either case, you should see what the error is.<br />
So how I managed to solve ?<br />
1. Put Reflector and dissasemble the source.<br />
2. Re-compile without snk.<br />
3. Put in Web.Config<br />
<trust level="Medium" />
4. Compile and run with debug<br />
Now it stops at :<br />
string templateText = InternalLoadTemplateContents(templateName);</p>
<p>This , in StringTemplate class, was </p>
<pre class="brush: csharp;">
  protected override string InternalLoadTemplateContents(string templateName)
        {
            string templateText = null;
            string templateLocation = null;

            try
            {
                //templateLocation = Path.Combine(LocationRoot, GetLocationFromTemplateName(templateName));
                templateLocation = string.Format(&quot;{0}/{1}&quot;, 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(&quot;Cannot open template file: &quot; + templateLocation, ex);
                }

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

                        if (filesWatcher == null)
                        {
                            filesWatcher = new FileSystemWatcher(LocationRoot, &quot;*.st&quot;);
                            //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(&quot;Invalid file character encoding: {0}&quot;, encoding);
                else
                    message = string.Format(&quot;The location root '{0}' and/or the template name '{1}' is invalid.&quot;, LocationRoot, templateName);

                throw new TemplateLoadException(message, ex);
            }
            catch (IOException ex)
            {
                throw new TemplateLoadException(&quot;Cannot close template file: &quot; + templateLocation, ex);
            }
            return templateText;
        }
</pre>
<p>Does something ring a bell to you ?<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer<br />
Wait for an answer</p>
<p>Yes &#8211; FileSystemWatcher .<br />
<a href="http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx</a><br />
[IODescriptionAttribute("FileSystemWatcherDesc")]<br />
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]<br />
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]<br />
public class FileSystemWatcher : Component, </p>
<p>Solution : Once defined my FileSystemTemplateLoader_MT   &#8211; without FileSystemWatcher &#8211; all works well<br />
So &#8211; the idea is : reflector, sources, find something strange. Remove, rebuild, re-test<br />
(Thanks to OrcsWeb team for helping me on this problem !)</p>
]]></content:encoded>
			<wfw:commentRss>http://msprogrammer.serviciipeweb.ro/2010/10/25/a-programmer-day/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

