<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/mfnd1yll/frog.png?width=480&amp;height=480&amp;v=1dd623e87367390&amp;hmac=3212421971b6bc5dbb1d8a857578868e34ac5181f40480f1a3a5beb16927f102</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1254</guid>
      <link>https://shazwazza.com/post/writing-a-docfx-markdown-plugin/</link>
      <category>Web Development</category>
      <title>Writing a DocFx markdown plugin</title>
      <description>&lt;p&gt;What is &lt;a rel="noopener" href="https://dotnet.github.io/docfx/" target="_blank"&gt;DocFx&lt;/a&gt;? It’s a static site generator mainly used for creating API documentation for your code. But it can be used for any static sites. We use this for the Lucene.Net project’s &lt;a rel="noopener" href="https://lucenenet.apache.org/" target="_blank"&gt;website&lt;/a&gt; and &lt;a rel="noopener" href="https://lucenenet.apache.org/docs/4.8.0-beta00007/" target="_blank"&gt;documentation&lt;/a&gt;. The end result is API docs that look and feel a little bit familiar, kind of like Microsoft’s own API documentation website. I’m not entirely sure if their docs are built with DocFx but I suspect it is but with some highly customized builds and plugins … but that’s just my own assumption.&lt;/p&gt;
&lt;p&gt;Speaking of customizing DocFx, it is certainly possible. That said the ironic part about DocFx is that it’s own documentation is not great. One of the markdown customizations we needed for the Lucene.Net project was to add a customized note that some APIs are experimental. This tag is based on the converted Java Lucene docs and looks like: &lt;a&gt;“@ &lt;em&gt;lucene&lt;/em&gt;.&lt;/a&gt;&lt;em&gt;experimental&lt;/em&gt;&lt;em&gt; ”. &lt;/em&gt;So we wanted to detect that string and convert it to a nice looking note similar to the &lt;a rel="noopener" href="https://dotnet.github.io/docfx/spec/docfx_flavored_markdown.html#note-warningtipimportant" target="_blank"&gt;DocFx markdown note&lt;/a&gt;. Luckily there is &lt;a rel="noopener" href="https://dotnet.github.io/docfx/tutorial/howto_customize_docfx_flavored_markdown.html" target="_blank"&gt;some docs on how to do that&lt;/a&gt; although they’re not at all succinct but the example pretty much covers exactly what we wanted to do.&lt;/p&gt;
&lt;h2&gt;Block markdown token&lt;/h2&gt;
&lt;p&gt;This example is a block level token since it exists on it’s own line and not within other text. This is also the example DocFx provides in it’s docs. It’s relatively easy to do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Register a IDfmEngineCustomizer to insert/add a “Block Rule”&lt;/li&gt;
&lt;li&gt;Create a new “Block Rule” which in it’s simplistic form is a regex that parses the current text block and if it matches it returns an instance of a custom “Token” class&lt;/li&gt;
&lt;li&gt;Create a custom “Token” class to store the information about what you’ve parsed&lt;/li&gt;
&lt;li&gt;Create a custom “Renderer” to write out actual HTML result you want&lt;/li&gt;
&lt;li&gt;Register a &lt;em&gt;IDfmCustomizedRendererPartProvider&lt;/em&gt; to expose your “Renderer”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This all uses &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/dotnet/framework/mef/" target="_blank"&gt;MEF&lt;/a&gt; to wire everything up. You can see the Lucene.Net implementation of a custom markdown block token here: &lt;a href="https://github.com/apache/lucenenet/tree/master/src/docs/LuceneDocsPlugins"&gt;https://github.com/apache/lucenenet/tree/master/src/docs/LuceneDocsPlugins&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Inline markdown token&lt;/h2&gt;
&lt;p&gt;The above was ‘easy’ because it’s more or less following the DocFx documentation example. So the next challenge is that I wanted to be able to render an Environment Variable value within the markdown… sounds easy enough? Well the code result is actually super simple but my journey to get there was absolutely not!&lt;/p&gt;
&lt;p&gt;There’s zero documentation about customizing the markdown engine for inline markdown and there’s almost zero documentation in the codebase about what is going on too which makes things a little interesting. I tried following the same steps above for the block markdown token and realized in the code that it’s using a &lt;em&gt;MarkdownBlockContext&lt;/em&gt; instance so I discovered there’s a &lt;em&gt;MarkdownInlineContext&lt;/em&gt; so thought, we’ll just swap that out … but that doesn’t work. I tried inserting my inline rule at the beginning, end, middle, etc… of the &lt;em&gt;DfmEngineBuilder.InlineInlineRules&lt;/em&gt; within my &lt;em&gt;IDfmEngineCustomizer&lt;/em&gt; but nothing seemed to happen. Hrm. So I cloned the DocFx repo and started diving into the tests and breaking pointing, etc…&lt;/p&gt;
&lt;p&gt;So here’s what I discovered:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Depending on the token and if a token can contain other tokens, its the tokens responsibility to recurse the parsing&lt;/li&gt;
&lt;li&gt;There’s a sort of ‘catch all’ rule called &lt;a rel="noopener" href="https://github.com/dotnet/docfx/blob/dev/src/Microsoft.DocAsCode.MarkdownLite/Basic/InlineRules/MarkdownTextInlineRule.cs" target="_blank"&gt;MarkdownTextInlineRule&lt;/a&gt; and that will ‘eat’ characters that don’t match the very specific markdown chars that it’s not looking for.&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;This means that if you have an inline token that is delimited by chars that this doesn’t ‘eat’, then your rule will not match. So your rule can only begin with certain chars: &lt;a rel="noopener" href="https://github.com/dotnet/docfx/blob/dev/src/Microsoft.DocAsCode.MarkdownLite/Regexes.cs#L114" target="_blank"&gt;&lt;strong&gt;\&amp;lt;!\[*`&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Your rule must run before this one&lt;/li&gt;
&lt;li&gt;For inline rules you don’t need a “Renderer” (i.e. &lt;em&gt;IDfmCustomizedRendererPartProvider)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Inline rule regex needs to match at the beginning of the string with the hat ^ symbol. This is a pretty critical part of how DocFx parses it’s inline content.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now that I know that, making this extension is super simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I’ll make a Markdown token: &lt;strong&gt;[EnvVar:MyEnvironmentVar] &lt;/strong&gt;which will parse to just render the value of the environment variable with that name, in this example: MyEnvironmentVariable.&lt;/li&gt;
&lt;li&gt;I’ll insert my rule to the top of the list so it doesn’t come after the catch-all rule&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;// customize the engine
[Export(typeof(IDfmEngineCustomizer))]
public class LuceneDfmEngineCustomizer : IDfmEngineCustomizer
{
    public void Customize(DfmEngineBuilder builder, IReadOnlyDictionary&amp;lt;string, object&amp;gt; parameters)
        {
        // insert inline rule at the top
        builder.InlineRules = builder.InlineRules.Insert(0, new EnvironmentVariableInlineRule());
    }
}

// define the rule
public class EnvironmentVariableInlineRule : IMarkdownRule
{
    // give it a name
    public string Name =&amp;gt; "EnvVarToken";

    // define my regex to match
    private static readonly Regex _envVarRegex = new Regex(@"^\[EnvVar:(\w+?)\]", RegexOptions.Compiled);

    // process the match
    public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
    {
        var match = _envVarRegex.Match(context.CurrentMarkdown);
        if (match.Length == 0) return null;

        var envVar = match.Groups[1].Value;
        var text = Environment.GetEnvironmentVariable(envVar);
        if (text == null) return null;

        // 'eat' the characters of the current markdown token so they aren't re-processed
        var sourceInfo = context.Consume(match.Length);

        // return a docfx token that just returns the text passed to it
        return new MarkdownTextToken(this, parser.Context, text, sourceInfo);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the end, that’s actually pretty simple! But don’t go trying to create a fancy token that doesn’t start with those magic characters since it’s not going to work.&lt;/p&gt;</description>
      <pubDate>Thu, 21 May 2020 07:28:00 Z</pubDate>
      <a10:updated>2020-05-21T07:28:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1196</guid>
      <link>https://shazwazza.com/post/finding-success-in-oss/</link>
      <category>Web Development</category>
      <title>Finding success in OSS</title>
      <description>&lt;p&gt;I’ve been working in open source for quite some time and wanted to share some of my experiences in regards to managing OSS projects and communities. I’ve been employed by &lt;a href="https://umbraco.com/" target="_blank"&gt;Umbraco&lt;/a&gt;, an open source .Net based CMS, for over 7 years (wow! where has the time gone!?) and have been working with the Umbraco project for even longer. In the past 10 years I’ve also started a number of my own OSS projects, seen many other colleagues and companies do the same, seen many get stalled, sometimes fail, sometimes succeed and more rarely, actually thrive. We’ve seen an enormous rise in the OSS take-up within the .Net world in the past few years thanks to Microsoft really embracing and encouraging open source and the communities that make it work. But OSS is not an easy or straightforward task. So how does an open source project succeed? … I unfortunately don’t have a straightforward answer for that either but I would like to share some insight based on my own experience.&lt;/p&gt;&lt;p&gt;The main reason I’ve mentioned Umbraco is because it is one of these rarer open source projects that has actually thrived and it’s quite exciting to be part of such a large worldwide community. So how have they made it work? Like many other Umbraco community members, I started using it many moons ago, working in agency land and wanting a .Net based open source CMS. At that time, it turned out that this was really the only one out there that seemed to do the job. For me, what made this project so exciting back then was how easy it was to help make the software better. It was easy to submit patches, it was easy to get feedback on issues and proposed features, it was easy to get in touch with other community members and what was really great was that it didn’t take long for your fixes or features that you submitted to be part of a new release. This was all back before GitHub or even Git existed so all of this communication was done in email or on custom built forums, but it still worked. Skip ahead to today and the underlying principles that still make this project exciting haven’t changed, it’s now just infinitely easier to do all of these things with better tools. The community of Umbraco is what makes this project tick, it has always been the main focus of the project and with community growth, the software and ecosystem thrive.&lt;/p&gt;&lt;h2&gt;But it’s free! … so how am I employed??&lt;/h2&gt;&lt;p&gt;This is the most common question I get asked about working for an open source project. When I started, I had no idea how to make an open source project sustainable along with having paid employees and I was equally intrigued. Umbraco offered a few services and products that were paid in order to be able to pay full time staff members and keep the project growing. These were things like various levels of certified training courses, add-on licensed products and paid support plans. Today the story for Umbraco is similar and includes all of these things but now also includes subscription models for Software as a Service called Umbraco Cloud which helps to continue the cycle of re-investing into growing the community and the project.&lt;/p&gt;&lt;h2&gt;Most OSS projects are made by individuals&lt;/h2&gt;&lt;p&gt;But … most OSS projects are tiny and are run by a single person who is the sole developer on a project who dedicate their own time to writing and maintaining it. A lot of this time will be used to help people: investigating issues, answering questions, writing documentation, fixing bugs, releasing new versions, etc… and all of this is done for &lt;strong&gt;Free&lt;/strong&gt; but these are the things that make a project successful. It is actually quite difficult to create a community that re-invests their time into helping write your software rather than just using your software. If you can convince one or more developers to get on board, you’ll then need to invest more of your time in reviewing code, documentation updates, bug fixes and features created by your community. This time is critical, you want to be able to provide feedback and to integrate your communities changes as fast as possible… this is what will help keep your community re-investing their own time in growing the project and hopefully drumming up support for even more developers to chip in.&lt;/p&gt;&lt;p&gt;In my experience, this is where most OSS projects plateau because the project’s founder doesn’t have enough time to manage it all. This generally leaves projects in a semi-stalled state and may end up losing active developers and community members since their pull requests and submitted issues will remain pending for too long. These projects will ebb and flow, developers get busy and then may be re-inspired to find some more time. This doesn’t mean these projects are unsuccessful and it is probably the state of most OSS projects out there.&lt;/p&gt;&lt;h2&gt;So how do you keep a project’s momentum?&lt;/h2&gt;&lt;p&gt;I try to keep my own projects ‘active’ by answering questions, reviewing PRs and releasing new versions as often as I can… but in reality, this doesn’t happen as often as I would like. Sometimes I get re-inspired and may invest my evenings or weekends on some features but ultimately, I don’t think this is sustainable if you do want your OSS project to become community driven. One of the first OSS projects I created was called “uComponents” which was a collection of plugins for Umbraco and I did something with that project that I haven’t done with any of my other ones – I asked another community member to take over the project. This meant providing write access to the code but more importantly &lt;strong&gt;trusting another developer&lt;/strong&gt; to help move the project forward. Eventually there was a few devs with write access and helping to manage the project and it ended up being very successful. I think if you can find people to help manage your project and allow yourself to trust other devs with that task, it will massively help your chances of not stalling the project.&lt;/p&gt;&lt;p&gt;I think the key to keeping a project’s momentum is to get really good at setting up the fundamentals for establishing a community:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Make it as easy as possible to contribute and discuss&lt;/li&gt;&lt;li&gt;Respond to your community in a timely fashion – doesn’t need to be a solution, but some form of acknowledgement is needed&lt;/li&gt;&lt;li&gt;Have good documentation and make sure the community can contribute to it&lt;/li&gt;&lt;li&gt;Have small achievable tasks on your tracker and use tags like &lt;a href="https://up-for-grabs.net/" target="_blank"&gt;“up-for-grabs”&lt;/a&gt; or “help-needed” so it’s easy for the community to know where they can ship in&lt;/li&gt;&lt;li&gt;Have CI/CD setup with tests, feedback and build outputs for the community to use&lt;/li&gt;&lt;li&gt;Trust other people in helping you to manage your project&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;How do larger OSS projects do it?&lt;/h2&gt;&lt;p&gt;I can’t answer this question for all larger OSS projects, but I can provide some insight with what we do at Umbraco. The more popular a project becomes, the more people there will be that that need help. &lt;strong&gt;Ideally, you’ll want to try to convert the people that are needing help, into people that are providing help&lt;/strong&gt;. To do that you should have all of the above points working well to make it easy for your community to start helping each other instead of just relying on you. Trusting the community is certainly at the heart of it all and this allows for the community to manage several aspects of itself.&lt;/p&gt;&lt;p&gt;At Umbraco, we have a &lt;a href="https://umbraco.com/blog/introducing-the-new-pr-team/" target="_blank"&gt;community PR team&lt;/a&gt; that dedicates their own time to helping out with the project and assisting with communication between the community and dev team and ensuring that contributors are receiving a timely response. We have a &lt;a href="https://umbraco.com/blog/the-umbraco-documentation-curators/" target="_blank"&gt;community documentation team&lt;/a&gt; that dedicates their own time to generate missing documentation, assessing documentation PRs and to help encourage more contributors to get involved. Our community forum is also open source and is where community members can ask and answer questions. We use &lt;a href="https://github.com/" target="_blank"&gt;GitHub&lt;/a&gt; for all of our code and our public issue tracker and use tags like up-for-grabs along with some bots to help automate the interaction with contributors on GitHub. We’ve also established some policies regarding issues and PRs to try to keep their numbers to a minimum, as one example: only allowing an up-for-grabs issue to be open for a certain amount of time and if it doesn’t get picked up it just gets closed. The premise for this is that it isn’t important enough for the community at the moment since nobody wanted to pick it up, so it gets closed with the option of being reopened if it becomes important again.&lt;/p&gt;&lt;p&gt;In order to manage all of this interaction we have a couple of full-time staff members that spend a significant amount of time working with the community and these community teams, on our forums and on our issue trackers. Several years ago, this was not the case and it’s now clear that managing a large community is a full time role and what quickly became obvious was that the more you invest in the community the more you get back.&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-managing-open-source-projects_a7b3-crop_34591114033_7b2c3e0869_o-1-_2.jpg"&gt;&lt;img width="1028" height="281" title="CodeGarden conference" style="margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" alt="CodeGarden conference" src="https://shazwazza.com/media/articulate/open-live-writer-managing-open-source-projects_a7b3-crop_34591114033_7b2c3e0869_o-1-_thumb.jpg" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Are my own projects successful?&lt;/h2&gt;&lt;p&gt;Sort of, yes and no. Some projects get old and I’d rather they didn’t exist anymore but people still use them so they need to be maintained. Some projects are newer and I’m excited about them but then lack the time to keep their momentum going. Then I have a problem of having too many projects! The important thing to me is that I keep learning and I’m generally happy to keep investing the time I can find into all of my projects. I think some of my projects would be more successful if I actually did all of the things I mentioned above :)&lt;/p&gt;&lt;p&gt;The fact is, unless you have a lot of spare time or you are getting paid to work on OSS (so that you can make time), establishing and fostering an active community is difficult and requires putting in a lot of time and effort. For me it’s entirely worth it, I think starting and managing your own OSS project is hugely rewarding and it’s a wonderful platform for learning and collaborating regardless of how big or small the project becomes. &lt;/p&gt;&lt;h2&gt;Being a part of a community&lt;/h2&gt;&lt;p&gt;You don’t need to be the founder of a project to reap the rewards of OSS. You can learn an enormous amount by collaborating on an OSS project. Of course there’s plenty of technical knowledge to gain but there’s a lot of other skills you can gain too and in my opinion the most important of all is communication skills. It’s easy to get frustrated by software or by other community members especially if you’ve run into some annoying bugs, somebody is being a jerk or just being difficult to communicate with. But, it’s important to not let these types of things get the better of you and that in itself is a difficult skill to master! &lt;/p&gt;&lt;p&gt;Communication is a 2 way street and it’s important for both community members and project owners to be &lt;strong&gt;friendly&lt;/strong&gt;, &lt;strong&gt;patient &lt;/strong&gt;and &lt;strong&gt;helpful&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;A great example of this is in bug reports. Some reports are unhelpful, aggressive or even angry. When submitting bug reports or asking questions, keep in mind that you are generally asking people for help for free. Please be helpful, please be friendly, please be patient. Please think about how you would like to receive a bug report or be asked a question, include as much information as you can and remember that most people can’t read minds ;) And vice-versa, if you are responding to questions where people aren’t being as helpful as they should be, you’ll typically you’ll be able to turn a conversation into a friendly, patient and helpful one by just communicating that way yourself.&lt;/p&gt;&lt;p&gt;English is typically the language used in OSS projects and some people might not speak English as a first language and that’s ok! In this case, those 3 points are extremely important and it’s quite rewarding to see international community members who come from all sorts of backgrounds, countries and languages collaborating on a single project. &lt;/p&gt;&lt;p&gt;When both community members and project owners adopt these values, great things will happen and everyone wins!&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-managing-open-source-projects_a7b3-crop_47992054518_f04761e447_o_4.jpg"&gt;&lt;img width="1028" height="388" title="Umbraco Friendly Hoodie" style="margin-right: auto; margin-left: auto; float: none; display: block; background-image: none;" alt="Umbraco Friendly Hoodie" src="https://shazwazza.com/media/articulate/open-live-writer-managing-open-source-projects_a7b3-crop_47992054518_f04761e447_o_thumb_1.jpg" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;hr&gt;&lt;p&gt;… now I need to go find some time to update my OSS projects :) &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;This blog is powered by &lt;a href="https://github.com/shazwazza/articulate" target="_blank"&gt;Articulate&lt;/a&gt;, an OSS project that I maintain which is running on &lt;a href="https://umbraco.com/" target="_blank"&gt;Umbraco&lt;/a&gt;, an OSS project that I work for and this post was written using &lt;a href="https://github.com/OpenLiveWriter/OpenLiveWriter" target="_blank"&gt;Open Live Writer&lt;/a&gt;, an OSS project that could use some love. &lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;</description>
      <pubDate>Thu, 26 Sep 2019 09:30:00 Z</pubDate>
      <a10:updated>2019-09-26T09:30:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1269</guid>
      <link>https://shazwazza.com/post/configuring-azure-active-directory-login-with-umbraco-members/</link>
      <category>Umbraco</category>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Configuring Azure Active Directory login with Umbraco Members</title>
      <description>&lt;p&gt;This post is about configuring Azure Active Directory with Umbraco &lt;u&gt;Members&lt;/u&gt; (not &lt;em&gt;Users&lt;/em&gt;), meaning this is for your front-end website, not the Umbraco back office. I did write up a post about Azure AD with back office users though, so if that is what you are looking for then &lt;a href="https://shazwazza.com/post/configuring-azure-active-directory-login-with-umbraco/" target="_blank"&gt;this is the link&lt;/a&gt;. &lt;/p&gt;&lt;h2&gt;Install the Nuget packages&lt;/h2&gt;&lt;p&gt;First thing to do is get the &lt;a href="https://www.nuget.org/packages/UmbracoIdentity" target="_blank"&gt;UmbracoIdentity&lt;/a&gt; package installed. &lt;/p&gt;

&lt;div style="width: 100%;"&gt;
&lt;div style="padding: 10px; color: rgb(255, 255, 255); line-height: 1.5; font-family: consolas, menlo, monaco,&amp;quot;Courier New&amp;quot;, monospace; background-color: rgb(0, 36, 64);"&gt;PM &amp;gt; Install-Package UmbracoIdentity&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;(This will also install the UmbracoIdentity.Core base package)&lt;/em&gt;&lt;/p&gt;&lt;p&gt;This package installs some code snippets and updates your web.config to enable ASP.Net Identity for Umbraco members. Umbraco ships with the old and deprecated ASP.Net Membership Providers for members and not ASP.Net Identity so this package extends the Umbraco CMS and the Umbraco members implementation to use ASP.Net Identity APIs to interact with the built in members data store. Installing this package will remove the (deprecated) FormsAuthentication module from your web.config and it will no longer be used to authenticate members, so the typical members snippets built into Umbraco macros will not work. Instead use the supplied snippets shipped with this package.&lt;/p&gt;&lt;p&gt;To read more about this package see the &lt;a href="https://github.com/Shazwazza/UmbracoIdentity" target="_blank"&gt;GitHub repo here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next, the OpenIdConnect package needs to be installed&lt;/p&gt;

&lt;div style="width: 100%;"&gt;
&lt;div style="padding: 10px; color: rgb(255, 255, 255); line-height: 1.5; font-family: consolas, menlo, monaco,&amp;quot;Courier New&amp;quot;, monospace; background-color: rgb(0, 36, 64);"&gt;PM &amp;gt; Install-Package Microsoft.Owin.Security.OpenIdConnect&lt;/div&gt;
&lt;/div&gt;

&lt;h2&gt;Configure Azure Active Directory&lt;/h2&gt;&lt;p&gt;Head over to the &lt;em&gt;Azure Active Directory &lt;/em&gt;section on the Azure portal, choose &lt;em&gt;App Registrations &lt;/em&gt;(I’m using the Preview functionality for this) and create a &lt;em&gt;New registration&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_4.png"&gt;&lt;img width="802" height="362" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_1.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;Next fill out the app details&lt;/h3&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_22.png"&gt;&lt;img width="802" height="805" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_10.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;You may also need to enter other redirect URLs depending on how many different environments you have. All of these URLs can be added in the Authentication section of your app in the Azure portal.&lt;/p&gt;&lt;p&gt;For AAD configuration for front-end members, the redirect Urls are just your website’s root URL and it is advised to keep the trailing slash. &lt;/p&gt;&lt;h3&gt;Next you will need to enable Id Tokens&lt;/h3&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_12.png"&gt;&lt;img width="802" height="559" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_5.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Configure OpenIdConnect&lt;/h2&gt;&lt;p&gt;The UmbracoIdentity package will have installed an OWIN startup class in &lt;em&gt;~/App_Start/UmbracoIdentityStartup.cs &lt;/em&gt;(or it could be in App_Code if you are using a website project)&lt;em&gt;. &lt;/em&gt;This is how ASP.Net Identity is configured for front-end members and where you can specify the configuration for different OAuth providers. There’s a few things you’ll need to do:&lt;/p&gt;&lt;h3&gt;Allow external sign in cookies&lt;/h3&gt;&lt;p&gt;If you scroll down to the &lt;em&gt;ConfigureMiddleware &lt;/em&gt;method, there will be a link of code to uncomment: &lt;em&gt;app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); &lt;/em&gt;this is required for any OAuth providers to work.&lt;/p&gt;&lt;h3&gt;Enable OpenIdConnect OAuth for AAD&lt;/h3&gt;&lt;p&gt;You’ll need to add this extension method class to your code which is some boiler plate code to configure OpenIdConnect with AAD:&lt;/p&gt;

&lt;pre class="lang-csharp"&gt;&lt;code&gt;public static class UmbracoADAuthExtensions
{
    public static void ConfigureAzureActiveDirectoryAuth(this IAppBuilder app,
        string tenant, string clientId, string postLoginRedirectUri, Guid issuerId,
        string caption = "Active Directory")
    {
        var authority = string.Format(
            System.Globalization.CultureInfo.InvariantCulture,
            "https://login.windows.net/{0}",
            tenant);

        var adOptions = new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = authority,
            RedirectUri = postLoginRedirectUri
        };

        adOptions.Caption = caption;
        //Need to set the auth type as the issuer path
        adOptions.AuthenticationType = string.Format(
            System.Globalization.CultureInfo.InvariantCulture,
            "https://sts.windows.net/{0}/",
            issuerId);
        app.UseOpenIdConnectAuthentication(adOptions);
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next you’ll need to call this code, add the following line underneath the &lt;em&gt;app.UseExternalSignInCookie&lt;/em&gt; method call: &lt;/p&gt;

&lt;pre class="lang-csharp"&gt;&lt;code&gt;app.ConfigureAzureActiveDirectoryAuth(
    ConfigurationManager.AppSettings["azureAd:tenantId"],
    ConfigurationManager.AppSettings["azureAd:clientId"],
    //The value of this will need to change depending on your current environment
    postLoginRedirectUri: ConfigurationManager.AppSettings["azureAd:redirectUrl"],
    //This is the same as the TenantId
    issuerId: new Guid(ConfigurationManager.AppSettings["azureAd:tenantId"]));&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you’ll need to add a few appSettings to your web.config (based on your AAD info):&lt;/p&gt;

&lt;pre class="lang-csharp"&gt;&lt;code&gt;&amp;lt;add key="azureAd:tenantId" value="YOUR-TENANT-ID-GUID" /&amp;gt;
&amp;lt;add key="azureAd:clientId" value="YOUR-CLIENT-ID-GUID" /&amp;gt;
&amp;lt;add key="azureAd:redirectUrl" value="http://my-test-website/" /&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Configure your Umbraco data&lt;/h2&gt;&lt;p&gt;The UmbracoIdentity repository has the &lt;a href="https://github.com/Shazwazza/UmbracoIdentity#installation" target="_blank"&gt;installation documentation&lt;/a&gt; and you must follow these 2 instructions, and they are very simple:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;a href="https://github.com/Shazwazza/UmbracoIdentity/wiki#2-member-type-updates" target="_blank"&gt;You need to update your member type with the securityStamp property&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="https://github.com/Shazwazza/UmbracoIdentity/wiki#3-creating-the-account-page" target="_blank"&gt;Create the Account document type&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Once that is done you will have an Member account management page which is based off of the installed views and snippets of the UmbracoIdentity package. This account page will look like this:&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_8.png"&gt;&lt;img width="802" height="305" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_3.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;As you can see the button text under the “Use another service to log in” is the login provider name which is a bit ugly. The good news is that this is easy to change since this is just a partial view that was installed with the UmbracoIdentity package. You can edit the file: &lt;em&gt;~/Views/UmbracoIdentityAccount/ExternalLoginsList.cshtml&lt;/em&gt;, the code to render that button text is using &lt;em&gt;@p.Authentication&lt;/em&gt; provider but we can easily change this to &lt;em&gt;@p.Caption&lt;/em&gt; which is actually the same caption text used in the extension method we created. So the whole button code can look like this instead:&lt;/p&gt;
&lt;pre class="lang-html"&gt;&lt;code&gt;
&amp;lt;button type="submit" class="btn btn-default"
        id="@p.AuthenticationType"
        name="provider"
        value="@p.AuthenticationType"
        title="Log in using your @p.Caption account"&amp;gt;
    @p.Caption
&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a bit nicer, now the button looks like:&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_10.png"&gt;&lt;img width="802" height="192" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_4.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;The purpose of all of these snippets and views installed with UmbracoIdentity is for you to customize how the whole flow looks and works so you’ll most likely end up customizing a number of views found in this folder to suit your needs.&lt;/p&gt;&lt;h2&gt;That’s it!&lt;/h2&gt;&lt;p&gt;Once that’s all configured, if you click on the Active Directory button to log in as a member, you’ll be brought to the standard AAD permission screen:&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_18.png"&gt;&lt;img width="402" height="434" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_8.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Once you accept you’ll be redirect back to your Account page:&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_20.png"&gt;&lt;img width="802" height="930" title="image" style="display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-configuring-azure-active-directory-login_c313-image_thumb_9.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Any customizations is then up to you. You can modify how the flow works, whether or not you accepting auto-linking accounts (like in the above example), or if you require a member to exist locally before being able to link an OAuth account, etc… All of the views and controller code in UmbracoIdentity is there for you to manipulate. The main files are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;~/Views/Account.cshtml&lt;/li&gt;&lt;li&gt;~/Views/UmbracoIdentityAccount/*&lt;/li&gt;&lt;li&gt;~/Controllers/UmbracoIdentityAccountController.cs&lt;/li&gt;&lt;li&gt;~/App_Start/UmbracoIdentityStartup.cs&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Happy coding!&lt;/p&gt;</description>
      <pubDate>Mon, 18 Feb 2019 02:09:00 Z</pubDate>
      <a10:updated>2019-02-18T02:09:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1129</guid>
      <link>https://shazwazza.com/post/deploying-to-azure-from-vsts-using-publish-profiles-and-msdeploy/</link>
      <category>Web Development</category>
      <category>etc...</category>
      <category>Hosting</category>
      <category>Servers</category>
      <title>Deploying to Azure from VSTS using publish profiles and msdeploy</title>
      <description>&lt;p&gt;In almost all of the examples online about how to deploy various services to Azure, they always list the super easy way to do it and that is to authenticate your current account to your Azure subscription which then grants your VSTS build to do all sorts of things… The problem is that not everyone has the security clearance to use the super easy tools in VSTS.&lt;/p&gt;&lt;p&gt;When you attempt to use these nice tools in VSTS you might get an error like this: &lt;font color="#f79646"&gt;“Failed to set Azure permission ‘RoleAssignmentId: some-guid-goes-here’ for the service principal … does not have authorizationto perform action ‘Microsoft.Authorization/roleAssignments/write’ over scope”&lt;/font&gt; This is because these nice VSTS tools actually creates a custom user behind the scenes in your azure subscription to use but your account doesn’t have access to authorize that.&lt;/p&gt;&lt;p&gt;Luckily there’s a work around&lt;/p&gt;&lt;h2&gt;MS Deploy … sigh&lt;/h2&gt;&lt;p&gt;Maybe there are other work arounds but this works, however it’s not the most elegant. I thought I’d post my findings here because it was a bit of a pain in the ass to get this all correct.&lt;/p&gt;&lt;p&gt;So here’s the steps:&lt;/p&gt;&lt;h3&gt;1. Download the publish profile&lt;/h3&gt;&lt;p&gt;You need to get the publish profile from your app service that you want to deploy to. This can be a website, a staging slot, an Azure function, (probably a bunch of others)&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_2.png"&gt;&lt;img width="600" height="185" title="image" style="border: 0px currentcolor; border-image: none; display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_thumb.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;The file downloaded is an XML file containing a bunch of info you’ll need&lt;/p&gt;&lt;h3&gt;2. Create a release definition and environment for your deployment&lt;/h3&gt;&lt;p&gt;&lt;em&gt;This assumes that you are pretty familiar with VSTS&lt;/em&gt;&lt;/p&gt;&lt;p&gt;You’ll want to create an empty environment in your release definition. Normally this is where you could choose the built in fancy VSTS deployment templates like “Azure App Service Deployment” … but as above, this doesn’t work if you don’t have security clearance. Instead, choose ‘Empty’&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_4.png"&gt;&lt;img width="600" height="330" title="image" style="border: 0px currentcolor; border-image: none; display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_thumb_1.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Then in your environment tasks, add Batch Script&lt;/p&gt;&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_6.png"&gt;&lt;img width="600" height="312" title="image" style="border: 0px currentcolor; border-image: none; display: inline; background-image: none;" alt="image" src="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-image_thumb_2.png" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;3. Setup your batch script&lt;/h3&gt;&lt;p&gt;There’s 2 ways to go about this and both depend on a msdeploy build output. This build output is generated by your build in VSTS if you are using a standard VSTS Visual Studio solution build. This will create msdeploy packages for you and will have put them in your artifacts folder. Along with msdeploy packages this will also generate a cmd batch file that executes msdeploy and a readme file to tell you how to execute it which contains some important info that you should read.&lt;/p&gt;&lt;p&gt;So here’s 2 options: Execute the cmd file, or execute msdeploy.exe directly&lt;/p&gt;&lt;h2&gt;Execute the cmd file&lt;/h2&gt;&lt;p&gt;There’s a bit of documentation about this online but most of it is based on using the SetParameters.xml file to adjust settings… but i just don’t want to use that. &lt;/p&gt;&lt;p&gt;Here’s the Path and Arguments that you need to run:&lt;/p&gt;


&lt;pre class="language-batch"&gt;&lt;code&gt;$(System.DefaultWorkingDirectory)/YOUR_BUILD_NAME/drop/YOUR_MSBUILD_PACKAGE.deploy.cmd&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=" language-batch"&gt;&lt;code&gt;/y "/m:https://${publishUrl}/MSDeploy.axd?site=${msdeploySite}" /u:$(userName) /p:$(userPWD) /a:Basic -enableRule:DoNotDeleteRule "-setParam:name='IIS Web Application Name',value='${msdeploySite}'"

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The parameters should be added to your VSTS Variables: ${msdeploySite}, $(userName), $(userPWD) and these variables correspond exactly to what is in your publish profile XML file that you downloaded. These parameters need to be pretty much exact, any misplaced quote or if you don’t include https, etc… will cause this to fail.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: the use of &lt;strong&gt;-enableRule:DoNotDeleteRule&lt;/strong&gt; is totally optional, if you want to reset your site to exactly what is in the msdeploy package you do not want this. If however, you have user generated images, content or custom config files that exist on your site and you don’t want them deleted when you deploy, then you need to set this.&lt;/p&gt;&lt;p&gt;I’m unsure if this will work for Azure Functions deployment (it might!) … but I used the next option to do that:&lt;/p&gt;&lt;h2&gt;Execute msdeploy.exe directly&lt;/h2&gt;&lt;p&gt;If you execute the CMD file, you’ll see in the VSTS logs the exact msdeploy signature used which is:&lt;/p&gt;
&lt;pre class="language-batch"&gt;&lt;code&gt;"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package='d:\a\r1\a\YOUR_PROJECT_NAME\drop\YOUR_MSDEPLOY_PACKAGE_FILE.zip' -dest:auto,computerName="https://YOUR_PUBLISH_URL/MSDeploy.axd?site=YOUR_PROFILE_NAME",userName=********,password=********,authtype="Basic",includeAcls="False" -verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"d:\a\r1\a\YOUR_PROJECT_NAME\drop\YOUR_MSDEPLOY_PACKAGE_FILE.SetParameters.xml" -enableRule:DoNotDeleteRule -setParam:name='IIS Web Application Name',value='YOUR_PROFILE_NAME'&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So if you wanted, you could take this and execute that directly instead of the CMD file. I use this method to deploy Azure Functions but the script is a little simpler since that deployment doesn’t require all of these parameters. For that I use this for the Path and Arguments:&lt;/p&gt;
&lt;pre class="language-batch"&gt;&lt;code&gt;C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe&lt;/code&gt;&lt;/pre&gt;


&lt;pre class="language-batch"&gt;&lt;code&gt;-verb:sync -source:package='$(System.DefaultWorkingDirectory)/YOUR_BUILD_NAME/drop/YOUR_MSDEPLOY_PACKAGE.zip' -dest:auto,computerName="https://$(publishUrl)/msdeploy.axd?site=$(msdeploySite)",UserName='$(userName)',Password='$(userPWD)',AuthType='Basic' -setParam:name='IIS Web Application Name',value='$(msdeploySite)'&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Hopefully this comes in handy for someone &lt;img class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="https://shazwazza.com/media/articulate/open-live-writer-deploying-to-azure_ead7-wlemoticon-winkingsmile_2.png"&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 26 Oct 2017 06:39:00 Z</pubDate>
      <a10:updated>2017-10-26T06:39:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1157</guid>
      <link>https://shazwazza.com/post/how-to-lazily-set-the-multi-term-rewrite-method-on-queries-in-lucene/</link>
      <category>Web Development</category>
      <title>How to lazily set the multi-term rewrite method on queries in Lucene</title>
      <description>&lt;p&gt;For wildcard queries in Lucene that you would like to have the results ordered by Score, there’s a trick that you need to do otherwise all of your scores will come back the same. The reason for this is because the default behavior of wildcard queries uses &lt;em&gt;CONSTANT_SCORE_AUTO_REWRITE_DEFAULT&lt;/em&gt; which as the name describes is going to give a constant score. The code comments describe why this is the default:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;a) Runs faster &lt;/p&gt;&lt;p&gt;b) Does not have the scarcity of terms unduly influence score&lt;/p&gt;&lt;p&gt;c) Avoids any "TooManyBooleanClauses" exceptions&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Without fully understanding Lucene that doesn’t really mean a whole lot but the Lucene docs give a little more info&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;NOTE: if setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod) is either CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE or SCORING_BOOLEAN_QUERY_REWRITE, you may encounter a BooleanQuery.TooManyClauses exception during searching, which happens when the number of terms to be searched exceeds BooleanQuery.getMaxClauseCount(). Setting setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod) to CONSTANT_SCORE_FILTER_REWRITE prevents this.&lt;/p&gt;&lt;p&gt;The recommended rewrite method is CONSTANT_SCORE_AUTO_REWRITE_DEFAULT: it doesn't spend CPU computing unhelpful scores, and it tries to pick the most performant rewrite method given the query. If you need scoring (like FuzzyQuery, use MultiTermQuery.TopTermsScoringBooleanQueryRewrite which uses a priority queue to only collect competitive terms and not hit this limitation. Note that org.apache.lucene.queryparser.classic.QueryParser produces MultiTermQueries using CONSTANT_SCORE_AUTO_REWRITE_DEFAULT by default.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;So the gist is, unless you are ordering by Score this shouldn’t be changed because it will consume more CPU and depending on how many terms you are querying against you might get an exception (though I think that is rare).&lt;/p&gt;&lt;h2&gt;So how do you change the default?&lt;/h2&gt;&lt;p&gt;That’s super easy, it’s just this line of code:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp"&gt;QueryParser.SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But there’s a catch! You &lt;strong&gt;must&lt;/strong&gt; set this flag before you parse any queries with the query parser otherwise it won’t work. All this really does is instruct the query parser to apply this scoring method to any &lt;em&gt;MultiTermQuery&lt;/em&gt; or &lt;em&gt;FuzzyQuery&lt;/em&gt; implementations it creates. So what if you don’t know if this change should be made before you use the query parser? One scenario might be: At the time of using the query parser, you are unsure if the user constructing the query is going to be sorting by score. In this case you want to change the scoring mechanism just before executing the search but after creating your query. &lt;/p&gt;&lt;h2&gt;Setting the value lazily&lt;/h2&gt;&lt;p&gt;The good news is that you can set this value lazily just before you execute the search even after you’ve used the query parser to create parts of your query. There’s only 1 class type that we need to check for that has this API: &lt;em&gt;MultiTermQuery &lt;/em&gt;however not all implementations of it support rewriting so we have to check for that. So given an instance of a Query we can recursively update every query contained within it and manually apply the rewrite method like:&lt;/p&gt;



&lt;pre&gt;&lt;code class="language-csharp"&gt;protected void SetScoringBooleanQueryRewriteMethod(Query query)
{
	if (query is MultiTermQuery mtq)
	{
		try
		{
			mtq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
		}
		catch (NotSupportedException)
		{
			//swallow this, some implementations of MultiTermQuery don't support this like FuzzyQuery
		}
	}
	if (query is BooleanQuery bq)
	{
		foreach (BooleanClause clause in bq.Clauses())
		{
			var q = clause.GetQuery();
			//recurse
			SetScoringBooleanQueryRewriteMethod(q);
		}
	}
}

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So you can call this method just before you execute your search and it will still work without having to eagerly use &lt;em&gt;QueryParser.SetMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);&lt;/em&gt; before you use the query parser methods.&lt;/p&gt;&lt;p&gt;Happy searching!&lt;/p&gt;</description>
      <pubDate>Fri, 15 Sep 2017 02:21:00 Z</pubDate>
      <a10:updated>2017-09-15T02:21:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1279</guid>
      <link>https://shazwazza.com/post/smidge-plus-nuglify-bundling-minification-and-source-maps/</link>
      <category>Web Development</category>
      <title>Smidge + Nuglify bundling, minification and source maps</title>
      <description>&lt;p&gt;Smidge &lt;strong&gt;&lt;a rel="noopener noreferrer" href="https://github.com/Shazwazza/Smidge/releases/tag/v2.0.0-rtm" target="_blank"&gt;2.0.0 RTM&lt;/a&gt;&lt;/strong&gt; is finally here!&lt;/p&gt;
&lt;p&gt;What is Smidge? Smidge is a lightweight &lt;u&gt;runtime &lt;/u&gt;bundling library (&lt;em&gt;CSS/JavaScript file minification, combination, compression&lt;/em&gt;) for ASP.NET Core -  &lt;a rel="noopener noreferrer" href="/post/smidge-20-alpha-is-out/" target="_blank"&gt;&lt;em&gt;Here’s some more background info on Smidge&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There’s plenty of great &lt;a rel="noopener noreferrer" href="https://github.com/Shazwazza/Smidge/milestone/5" target="_blank"&gt;new features in the version 2.0.0&lt;/a&gt; , but in this post I’ll just highlight a couple of fun ones….&lt;/p&gt;
&lt;h2&gt;Nuglify and source maps&lt;/h2&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/xoofx/NUglify" target="_blank"&gt;Nuglify&lt;/a&gt; if an ASP.NET Core port of the old &lt;a rel="noopener noreferrer" href="http://ajaxmin.codeplex.com/" target="_blank"&gt;AjaxMin&lt;/a&gt; library and this works very nicely with Smidge too. By default Smidge uses an updated version of &lt;a rel="noopener noreferrer" href="http://www.crockford.com/javascript/jsmin.html" target="_blank"&gt;JsMin&lt;/a&gt; that I maintain called &lt;a rel="noopener noreferrer" href="https://github.com/Shazwazza/JsMinSharp" target="_blank"&gt;JsMinSharp&lt;/a&gt; for it’s JavaScript minification engine. JsMin works fairly well, it’s primary feature is that it’s &lt;em&gt;fast &lt;/em&gt;because it doesn’t come with any bells and whistles and doesn’t produce a &lt;a rel="noopener noreferrer" href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" target="_blank"&gt;Syntax Tree&lt;/a&gt;. Nuglify on the other hand does produce a Syntax Tree which gives it loads of control over how the minification is done so that it can produce much smaller files, has the ability to rename/reduce parts of the JS and to produce &lt;a rel="noopener noreferrer" href="https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/" target="_blank"&gt;source maps&lt;/a&gt;. The downfall of all this control is that it becomes much slower but Smidge is smart enough to cache processed files on the server in a persistent state so it won’t re-process your files if it doesn’t need to.&lt;/p&gt;
&lt;p&gt;Here’s a quick benchmark of JsMin vs Nuglify to minify jQuery using the default options (and no source map). As you can see JsMin is substantially faster and is far better for memory but Nuglify can reduce the file size quite a lot more &lt;em&gt;(smaller Minified % is better)&lt;/em&gt;&lt;/p&gt;
&lt;table border="0" width="700" style="box-sizing: border-box; overflow: auto; margin-bottom: 16px; white-space: normal; word-spacing: 0px; margin-top: 0px; border-collapse: collapse; text-transform: none; border-spacing: 0px; orphans: 2; widows: 2; display: block; letter-spacing: normal; background-color: #ffffff; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px;"&gt;
&lt;thead&gt;
&lt;tr style="box-sizing: border-box; border-top: #cccccc 1px solid; background-color: #ffffff;"&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Method&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Median&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;StdDev&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Scaled&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Scaled-SD&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Minified %&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Gen 0&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Gen 1&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Gen 2&lt;/th&gt;
&lt;th style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Bytes Allocated/Op&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr style="box-sizing: border-box; border-top: #cccccc 1px solid; background-color: #ffffff;"&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;JsMin&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;10.2008 ms&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;0.3102 ms&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;1.00&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;0.00&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;51.75%&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;-&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;-&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;-&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;155,624.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="box-sizing: border-box; border-top: #cccccc 1px solid; background-color: #f8f8f8;"&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;Nuglify&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;69.0778 ms&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;0.0180 ms&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;6.72&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;0.16&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;32.71%&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;53.00&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;22.00&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;15.00&lt;/td&gt;
&lt;td style="box-sizing: border-box; border: #dddddd 1px solid; padding: 6px 13px 6px 13px;"&gt;4,837,313.07&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Source maps you say? JsMin isn’t all that smart so it can’t produce source maps (&lt;em&gt;&lt;a rel="noopener noreferrer" href="https://github.com/Shazwazza/Smidge/issues/57" target="_blank"&gt;yet&lt;/a&gt;&lt;/em&gt;) but Nuglify certainly can and by default the &lt;em&gt;Smidge.Nuglify&lt;/em&gt; package has this turned on by default. Here’s how you can get this working:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Follow the super easy Smidge &lt;a rel="noopener noreferrer" href="https://github.com/Shazwazza/Smidge#quick-start" target="_blank"&gt;quick start installation guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Install the Smidge.Nuglify package
&lt;div&gt;
&lt;div class="nuget-badge"&gt;
&lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package Smidge.Nuglify&lt;/code&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Add Smidge Nuglify to your services: &lt;code&gt;services.AddSmidgeNuglify();&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Use Smidge Nuglify in your Configure method: &lt;code&gt;app.UseSmidgeNuglify();&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Replace JsMin  with Nuglify for the default pipeline:
&lt;pre&gt;&lt;code class="language-csharp"&gt;services.Configure(opt =&amp;gt;
{
    opt.PipelineFactory.OnCreateDefault = (type, pipeline) =&amp;gt; pipeline.Replace(opt.PipelineFactory);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;That’s it! Nuglify is now the JS minifier for the default pipeline and will produce source maps. You can configure the Nuglify settings in the &lt;em&gt;AddSmidgeNuglify&lt;/em&gt; method.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now you can debug via the source maps in dev tools:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate/windows-live-writer-smidge-nuglify_9f43-image_4.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate/windows-live-writer-smidge-nuglify_9f43-image_thumb_1.png" border="0" alt="image" title="image" width="600" height="350" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;File watching&lt;/h2&gt;
&lt;p&gt;Smidge can now watch your source files and if any changes are detected it can auto cache bust the bundle and flag for re-processing.&lt;/p&gt;
&lt;p&gt;Previous to version 2.0.0 if you were in release mode all of the bundling/minification would be active but you really just wanted to test this behavior but unfortunately it meant that if you updated a js/css file you would have to go bump the Smidge version in the config file to clear out the server side persistent cache. In 2.0.0, you can configure your bundle with different environment options for both Debug and Production. For example, perhaps in Debug mode you still want your bundles to be active so that the minification, combination, compression, etc… is still happening but you want to still actively edit files … you can easily do this! Here’s a nice fluent syntax to achieve this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp"&gt;bundles.CreateJs("my-application", "~/js/site.js", "~/js/app")
    .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
        .ForDebug(builder =&amp;gt; builder
            .EnableCompositeProcessing()
            .EnableFileWatcher()
            .SetCacheBusterType())
        .Build()
    );
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For this example the “my-application” bundle will do the following when being rendered in debug:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Still process the files in the bundle and minify, combine, compress into a composite file&lt;/li&gt;
&lt;li&gt;Attach file watchers to all files in the bundle&lt;/li&gt;
&lt;li&gt;Change the cache buster for this bundle to only survive the lifespan of the current app domain&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And what’s cool is that if you edit any of the source files in this bundle it will auto cache bust to a new value and re-process the bundle when it’s next requested.&lt;/p&gt;
&lt;h2&gt;Debug vs Production?&lt;/h2&gt;
&lt;p&gt;There’s a few ways that you could render assets in either debug or production mode but this is probably the easiest and uses ASP.NET Core’s environment tag helpers. Notice the debug attribute on the script/link tags and that the src/href attributes just include the bundle name … this is using Smidge’s tag helpers to define which bundle to render and if it’s in debug mode.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-html"&gt;&amp;lt;environment names="Development"&amp;gt;
	&amp;lt;link rel="stylesheet" href="my-css" debug="true" /&amp;gt;
	&amp;lt;script src="my-js" debug="true"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/environment&amp;gt;

&amp;lt;environment names="Staging,Production"&amp;gt;
	&amp;lt;link rel="stylesheet" href="my-css" /&amp;gt;
	&amp;lt;script src="my-js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/environment&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Thanks!&lt;/h2&gt;
&lt;p&gt;Big thanks to &lt;a rel="noopener noreferrer" href="https://github.com/dazinator" target="_blank"&gt;@dazinator&lt;/a&gt; for all the help, recommendations, testing, feedback, etc… and for the rest of the community for filing bugs, questions, and comments. Much appreciated :)&lt;/p&gt;</description>
      <pubDate>Mon, 08 May 2017 23:23:00 Z</pubDate>
      <a10:updated>2017-05-08T23:23:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1209</guid>
      <link>https://shazwazza.com/post/using-aspnet5-optionsmodel/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Using AspNet5 OptionsModel</title>
      <description>&lt;p&gt;If you’ve used AspNet5 then you’ve probably been using some MVC, in which case you’ve probably seen something like this in your Startup class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// Add MVC services to the services container&lt;/span&gt;
services.AddMvc(configuration)
    .Configure&amp;lt;MvcOptions&amp;gt;(options =&amp;gt;
    {
        &lt;span class="rem"&gt;//Configure some MVC options like customizing the &lt;/span&gt;
        &lt;span class="rem"&gt;// view engines, etc...&lt;/span&gt;
        options.ViewEngines.Insert(0, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TestViewEngine));
    });&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;It turns out this syntax for specifying ‘options’ for a given service is a generic pattern that you can use in your own code. In fact the OptionsModel framework is it’s own code repository: &lt;a title="https://github.com/aspnet/Options" href="https://github.com/aspnet/Options"&gt;https://github.com/aspnet/Options&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I’ve implemented custom options in my AspNet5 project called &lt;a href="https://github.com/Shazwazza/smidge" target="_blank"&gt;Smidge&lt;/a&gt; (a runtime JavaScript/CSS pre-processing engine) and wanted to share the details since as far as I’ve seen there isn’t any documentation about this.&lt;/p&gt;
&lt;h2&gt;What are options?&lt;/h2&gt;
&lt;p&gt;Probably the simplest way to describe the options framework is that: Options allow you to configure your application via code during startup.&lt;/p&gt;
&lt;p&gt;Options are just a POCO class that can contain configuration options to customize the behavior of your library. These option classes can be injected into any of your services with IoC using an interface called &lt;em&gt;&lt;a href="https://github.com/aspnet/Options/blob/dev/src/Microsoft.Framework.OptionsModel/IOptions.cs" target="_blank"&gt;Microsoft.Framework.OptionsModel.IOptions&lt;/a&gt;&lt;/em&gt;. There’s a caveat to this POCO class however: It must contain an parameter-less/empty constructor which means you cannot have services injected into the your options class via it’s constructor.&amp;nbsp; This options framework also allows for ‘named’ options. So for example, perhaps you have a single options class that you would like to have configured in 2 different ways, one for ‘staging’ and one for your ‘live’ website.&lt;/p&gt;
&lt;h2&gt;Creating options&lt;/h2&gt;
&lt;p&gt;Here’s a really simple example of a POCO options class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomMessageOptions
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; CustomMessage()
    {
        Message = &lt;span class="str"&gt;""&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Message { get; set; }
}&lt;/pre&gt;
&lt;p&gt;In order to use this options class you need to create an options configuration class. For example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomMessageOptionsSetup : ConfigureOptions&amp;lt;CustomMessageOptions&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; CustomMessageOptionsSetup() 
    : &lt;span class="kwrd"&gt;base&lt;/span&gt;(ConfigureMessageOptions)
    {
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Set the default options&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ConfigureMessageOptions(CustomMessageOptions options)
    {
        options.Message = &lt;span class="str"&gt;"Hello world"&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;p&gt;Then you need to add this class to your IoC container of type &lt;a href="https://github.com/aspnet/Options/blob/dev/src/Microsoft.Framework.OptionsModel/IConfigureOptions.cs" target="_blank"&gt;&lt;em&gt;Microsoft.Framework.OptionsModel.IConfigureOptions&lt;/em&gt;&lt;/a&gt;:&lt;/p&gt;&lt;pre class="csharpcode"&gt;services.AddTransient&amp;lt;IConfigureOptions&amp;lt;CustomMessageOptions&amp;gt;, CustomMessageOptionsSetup&amp;gt;();&lt;/pre&gt;
&lt;h2&gt;Using options&lt;/h2&gt;
&lt;p&gt;To configure your options during startup, you do so in the &lt;em&gt;ConfigureServices &lt;/em&gt;method like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;services.Configure&amp;lt;CustomMessageOptions&amp;gt;(options =&amp;gt;
{
    options.Message = &lt;span class="str"&gt;"Hello there!"&lt;/span&gt;;
});&lt;/pre&gt;
&lt;p&gt;Now you can have these options injected into any of your services using the &lt;em&gt;IOptions&lt;/em&gt; interface noted previously:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyCoolService 
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyCoolService(IOptions&amp;lt;CustomMessageOptions&amp;gt; messageOptions)
    {
        &lt;span class="rem"&gt;//IOptions exposes an 'Options' property which resolves an instance&lt;/span&gt;
        &lt;span class="rem"&gt;//of CustomMessageOptions&lt;/span&gt;
        ConfiguredMessage = messageOptions.Options.Message;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConfiguredMessage {get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set;}
}&lt;/pre&gt;
&lt;h2&gt;Named options&lt;/h2&gt;
&lt;p&gt;As an example, lets say that you want a different message configured for your ‘staging’ and ‘live’ websites. This can be done with named options, here’s an example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;services
    .Configure&amp;lt;CustomMessageOptions&amp;gt;(options =&amp;gt;
    {
        options.Message = &lt;span class="str"&gt;"Hi! This is the staging site"&lt;/span&gt;;
    }, &lt;span class="str"&gt;"staging"&lt;/span&gt;)
    .Configure&amp;lt;CustomMessageOptions&amp;gt;(options =&amp;gt;
    {
        options.Message = &lt;span class="str"&gt;"Hi! This is the live site"&lt;/span&gt;;
    }, &lt;span class="str"&gt;"live"&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;Then in your service you can resolve the option instance by name:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyCoolService 
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyCoolService(IOptions&amp;lt;CustomMessageOptions&amp;gt; messageOptions)
    {
        &lt;span class="rem"&gt;//IRL This value would probably be set via some environment variable&lt;/span&gt;
        var configEnvironment = &lt;span class="str"&gt;"staging"&lt;/span&gt;;

        &lt;span class="rem"&gt;//IOptions exposes an 'GetNamedOptions' method which resolves an instance&lt;/span&gt;
        &lt;span class="rem"&gt;//of CustomMessageOptions based on a defined named configuration&lt;/span&gt;
        ConfiguredMessage = messageOptions.GetNamedOptions(configEnvironment);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConfiguredMessage {get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set;}
}&lt;/pre&gt;
&lt;h2&gt;Configuring options with other services&lt;/h2&gt;
&lt;p&gt;Since your options class is just a POCO object and must have a parameter-less/empty constructor, you cannot inject services into the options class. However, there is a way to use IoC services in your options classes by customizing the &lt;em&gt;ConfigureOptions&lt;/em&gt; class created above.&amp;nbsp; In many cases this won’t be necessary but this really depends on how you are using options.&amp;nbsp; As a (bad) example, lets say we wanted to expose a custom helper service called SiteHelper on the &lt;em&gt;CustomMessageOptions&lt;/em&gt; class that can be used by a developer to create the message. The end result syntax might look like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;services.Configure&amp;lt;CustomMessageOptions&amp;gt;(options =&amp;gt;
    {
        var siteId = options.SiteHelper.GetSiteId();
        options.Message = &lt;span class="str"&gt;"Hi! This is the staging site with id: "&lt;/span&gt; + siteId;
    });&lt;/pre&gt;
&lt;p&gt;In order for that to work the &lt;em&gt;options.SiteHelper&lt;/em&gt; property needs to be initialized. This is done with the &lt;em&gt;CustomMessageOptionsSetup &lt;/em&gt;class (created above) which has been added to the IoC container, this means it can have other services injected into it. The resulting class would look like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomMessageOptionsSetup : ConfigureOptions&amp;lt;CustomMessageOptions&amp;gt;
{
    &lt;span class="rem"&gt;//SiteHelper gets injected via IoC&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; CustomMessageOptionsSetup(SiteHelper siteHelper) 
    : &lt;span class="kwrd"&gt;base&lt;/span&gt;(ConfigureMessageOptions)
    {
        SiteHelper = siteHelper;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SiteHelper SiteHelper { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Set the default options&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ConfigureMessageOptions(CustomMessageOptions options)
    {
        options.Message = &lt;span class="str"&gt;"Hello world"&lt;/span&gt;;
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Allows for configuring the options instance before options are set&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Configure(Bundles options, &lt;span class="kwrd"&gt;string&lt;/span&gt; name = &lt;span class="str"&gt;""&lt;/span&gt;)
    {
        &lt;span class="rem"&gt;//Assign the site helper instance&lt;/span&gt;
        options.SiteHelper = SiteHelper;

        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Configure(options, name);
    }
}&lt;/pre&gt;
&lt;p&gt;IRL to give you an example of why this might be useful, in my &lt;a href="https://github.com/Shazwazza/smidge" target="_blank"&gt;Smidge&lt;/a&gt; project I allow developers to create named JavaScript/CSS bundles during startup using options. In some cases a developer might want to manipulate the file processing pipeline for a given bundle and in that case they need access to a service called PreProcessPipelineFactory which needs to come from IoC. The usage might look like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;services.AddSmidge()
    .Configure&amp;lt;Bundles&amp;gt;(bundles =&amp;gt;
    {                   
        bundles.Create(&lt;span class="str"&gt;"test-bundle-3"&lt;/span&gt;, 
            bundles.PipelineFactory.GetPipeline(
                &lt;span class="rem"&gt;//add as many processor types as you want&lt;/span&gt;
                &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(DotLess), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(JsMin)), 
            WebFileType.Js, 
            &lt;span class="str"&gt;"~/Js/Bundle2"&lt;/span&gt;);
    });&lt;/pre&gt;
&lt;p&gt;In the above, the bundles.PipelineFactory is a property on the bundles (options) class which gets initialized in my own &lt;em&gt;ConfigureOptions&lt;/em&gt; class.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hopefully this helps anyone looking to use custom options in their AspNet5 libraries!&lt;/p&gt;</description>
      <pubDate>Mon, 12 Jan 2015 04:21:00 Z</pubDate>
      <a10:updated>2015-01-12T04:21:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1177</guid>
      <link>https://shazwazza.com/post/introducing-smidge-an-aspnet-5-runtime-jscss-pre-processor/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Introducing ‘Smidge’ – an ASP.NET 5 runtime JS/CSS pre-processor</title>
      <description>&lt;p&gt;During the past month I decided to dive deep into learning ASP.NET 5, and what better way to learn than to start a new OSS project :)&lt;/p&gt;
&lt;p&gt;I chose to make a new new simple and extensible Javascript/CSS &lt;span style="text-decoration: underline;"&gt;runtime&lt;/span&gt; pre-processor for ASP.NET 5. It does file minification, combination and compression, has a nice file caching layer and it’s all done in async operations. I ported over a few ideas and code snippets from &lt;a href="https://github.com/Shazwazza/ClientDependency/" target="_blank"&gt;CDF (client dependency framework)&lt;/a&gt; but with a more modern approach. I’ve called it ‘Smidge’ = &lt;em&gt;something really small&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/Shazwazza/Smidge" target="_blank"&gt;The project is on GitHub&lt;/a&gt;, it’s still a work in progress but its functional and there’s even some documentation! In the next few weeks I’ll get more of the code and docs updated and hopefully have a beta release out. In the meantime, you can clone the source, browse the code, build it and of course use it if you like.&lt;/p&gt;
&lt;h2&gt;Project details&lt;/h2&gt;
&lt;p&gt;It’s currently only targeting &lt;em&gt;aspnet50 &lt;/em&gt;and not the Core CLR… I didn’t start with Core CLR because there was some legacy code I had to port over and I wanted to get something up and working relatively quickly. It shouldn’t be too much work to convert to Core CLR and Mono, hopefully I’ll find time to do that soon. It’s referencing all of the beta-* libraries from the ASP.NET 5 nightly myget feeds since there’s some code I’m using that isn’t available in the current beta1 release (&lt;em&gt;like Microsoft.AspNet.WebUtilities.UriHelper&lt;/em&gt;). The target KRE version is currently &lt;em&gt;KRE-CLR-amd64 1.0.0-beta2-10760.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Installation&lt;/h2&gt;
&lt;p&gt;I’ve put up an Alpha 1 release on Nuget, so you can install it from there:&lt;/p&gt;
&lt;div class="nuget-badge"&gt;
&lt;p&gt;PM&amp;gt; Install-Package Smidge -Pre&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;There’s some &lt;a href="https://github.com/Shazwazza/Smidge/blob/master/README.md#install" target="_blank"&gt;installation instructions here&lt;/a&gt;, you’ll need to add the smidge.json file yourself for now, can’t figure out how to get VS 2015 (kpm pack) to package that up … more learning required!&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;There’s certainly a lot of detective work involved in learning ASP.NET 5 but with the code being open source and browse-able/searchable on GitHub, it makes finding what you need fairly easy.&lt;/p&gt;</description>
      <pubDate>Thu, 11 Dec 2014 23:19:00 Z</pubDate>
      <a10:updated>2014-12-11T23:19:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1174</guid>
      <link>https://shazwazza.com/post/petapoco-may-cause-high-memory-usage-with-certain-queries/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>PetaPoco may cause high memory usage with certain queries</title>
      <description>&lt;p&gt;If you are using &lt;a href="https://github.com/toptensoftware/PetaPoco" target="_blank"&gt;PetaPoco&lt;/a&gt;, or &lt;a href="https://github.com/schotime/NPoco" target="_blank"&gt;NPoco&lt;/a&gt; (&lt;em&gt;which seams to be the most up-to-date fork of the project&lt;/em&gt;), the title of this post might be a bit scary… but hopefully you won’t have to worry. This really depends on how you create your queries and how many different query structures you are executing.&lt;/p&gt;
&lt;h2&gt;High memory usage&lt;/h2&gt;
&lt;p&gt;Here is the code in relation to the memory growth when using PetaPoco:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L1836" title="https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L1836"&gt;https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L1836&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What is happening here is that every time a POCO needs to be mapped from a data source, this will add more values to a static cache, specifically this one:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L2126" title="https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L2126"&gt;https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs#L2126&lt;/a&gt;  (&lt;em&gt;m_PocoDatas&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;This isn’t a bad thing… but it can be if you are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;using non-parameterized where clauses&lt;/li&gt;
&lt;li&gt;you have dynamically generated where clauses&lt;/li&gt;
&lt;li&gt;you use a lot of sql ‘IN’ clauses – since the items in the array being passed to the ‘IN’ clauses is dynamic&lt;/li&gt;
&lt;li&gt;you have tons of differently statically unique where clauses&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each time a unique SQL query is sent to PetaPoco it will store this SQL string and associate it to a delegate (which is also cached). Over time, as these unique SQL queries are executed, the internal static cache will grow. In some cases this could consume quite a lot of memory.&lt;/p&gt;
&lt;p&gt;The other thing to note is how large the ‘key’ that PetaPoco/NPoco uses:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;var key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;"{0}:{1}:{2}:{3}:{4}"&lt;/span&gt;, sql, connString, ForceDateTimesToUtc, firstColumn, countColumns);&lt;/pre&gt;
&lt;p&gt;Considering how many queries might be executing in your application, the storage for these keys alone could take up quite a lot of memory! An SQL statement combined with a connection string could be very long, and each of these combinations gets stored in memory for every unique SQL query executed that returns mapped POCO objects.&lt;/p&gt;
&lt;h2&gt;Parameterized queries vs. non-parameterized&lt;/h2&gt;
&lt;p&gt;Here’s some examples of why non-parameterized queries will cause lots of memory consumption. Lets say we have a simple query like:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;db.Query&amp;lt;MyTable&amp;gt;(&lt;span class="str"&gt;"WHERE MyColumn=@myValue"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; {myValue = &lt;span class="str"&gt;"test"&lt;/span&gt;})&lt;/pre&gt;
&lt;p&gt;Which results in this SQL:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = @myValue&lt;/pre&gt;
&lt;p&gt;This query can be used over and over again with a different value and PetaPoco will simply store a single SQL key in it’s internal cache. However, if you are executing queries without real parameters such as:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;db.Query&amp;lt;MyTable&amp;gt;(&lt;span class="str"&gt;"WHERE MyColumn='hello'"&lt;/span&gt;);
db.Query&amp;lt;MyTable&amp;gt;(&lt;span class="str"&gt;"WHERE MyColumn='world'"&lt;/span&gt;);
db.Query&amp;lt;MyTable&amp;gt;(&lt;span class="str"&gt;"WHERE MyColumn='hello world'"&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;Which results in this SQL:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = &lt;span class="str"&gt;'hello'&lt;/span&gt;;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = &lt;span class="str"&gt;'world'&lt;/span&gt;;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = &lt;span class="str"&gt;'hello world'&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;Then PetaPoco will store each of these statements against a delegate in it’s internal cache since each of these string statements are not equal to each other.&lt;/p&gt;
&lt;p&gt;Depending on your application you still might have a very large number of unique parameterized queries, though I’d assume you’d have to have a terrifically huge amount for it to be a worry.&lt;/p&gt;
&lt;h2&gt;Order by queries&lt;/h2&gt;
&lt;p&gt;Unfortunately even if you use parameterized queries, PetaPoco will store the SQL query key with it’s &lt;em&gt;Order By&lt;/em&gt; clause which isn’t necessary and will again mean more duplicate SQL keys and delegates being tracked. For example if you have these resulting queries:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = @myValue &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; SomeField;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; * &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; MyColumn = @myValue &lt;span class="kwrd"&gt;ORDER&lt;/span&gt; &lt;span class="kwrd"&gt;BY&lt;/span&gt; AnotherField;&lt;/pre&gt;
&lt;p&gt;PetaPoco will store each of these statements in it’s internal cache separately since the strings don’t match, however the delegate that PetaPoco is storing against these SQL statements isn’t concerned about the ordering output, it’s only concerned about the column and table selection so in theory it should be stripping off the last &lt;em&gt;Order By &lt;/em&gt;clause&lt;em&gt; (and other irrelevant clauses) &lt;/em&gt;to avoid this duplication.&lt;/p&gt;
&lt;h2&gt;A slightly better implementation&lt;/h2&gt;
&lt;p&gt;First, if you are using PetaPoco/NPoco, you shouldn’t use dynamic queries for the point’s mentioned above. If you need this functionality then I suppose it might be worth these libraries adding some sort of property on the Database object or a parameter in either the Fetch or Query methods to specify that you don’t want to use the internal cache (this will be slower, but you won’t get unwanted memory growth). I’d really just suggest not using dynamically created where clauses ;-)&lt;/p&gt;
&lt;p&gt;Next, there’s a few things that could be fixed in the PetaPoco/NPoco core to manage memory a little better:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The size the the key that is stored in memory doesn’t need to be that big. A better implementation would be to use a hash combiner class to combine the &lt;em&gt;GetHashCode&lt;/em&gt; result of each of those parameters that make up the key. This is a very fast way to create a hash of some strings that will result in a much smaller key. An example of a hash combiner class is here (which is actually inspired by the various internal hash code combiner classes in .Net): &lt;a href="https://github.com/umbraco/Umbraco-CMS/blob/7.2.0/src/Umbraco.Core/HashCodeCombiner.cs" title="https://github.com/umbraco/Umbraco-CMS/blob/7.2.0/src/Umbraco.Core/HashCodeCombiner.cs"&gt;&lt;em&gt;https://github.com/umbraco/Umbraco-CMS/blob/7.2.0/src/Umbraco.Core/HashCodeCombiner.cs&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Instead of storing all of this cache in static variables, have them stored in an &lt;em&gt;ObjectCache/MemoryCache (&lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.objectcache(v=vs.110).aspx" title="http://msdn.microsoft.com/en-us/library/system.runtime.caching.objectcache(v=vs.110).aspx"&gt;&lt;em&gt;http://msdn.microsoft.com/en-us/library/system.runtime.caching.objectcache(v=vs.110).aspx&lt;/em&gt;&lt;/a&gt;&lt;em&gt;) with &lt;/em&gt;a sliding expiration so the memory can get collected when it’s unused&lt;/li&gt;
&lt;li&gt;The Order By clause should be ignored based on the point mentioned above&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’ve created a PR for NPoco &lt;a href="https://github.com/schotime/NPoco/pull/134" target="_blank"&gt;here&lt;/a&gt;, and also created an issue on the original PetaPoco source &lt;a href="https://github.com/toptensoftware/PetaPoco/issues/185" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 25 Sep 2014 09:00:00 Z</pubDate>
      <a10:updated>2014-09-25T09:00:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1223</guid>
      <link>https://shazwazza.com/post/get-jquery-requests-to-play-nicely-with-angularjs-csrf-convention/</link>
      <category>Web Development</category>
      <title>Get JQuery requests to play nicely with AngularJS CSRF convention</title>
      <description>&lt;p&gt;Decided to write this quick post for anyone searching on this topic. AngularJS has it’s own convention for CSRF (Cross Site Request Forgery) protection but in some cases you’ll be calling these same server side services via JQuery so you might need to get JQuery requests to also follow Angular’s convention. &lt;/p&gt; &lt;p&gt;&lt;em&gt;For information about Angular’s CSRF protection see the “Security Considerations” part of &lt;/em&gt;&lt;a href="http://docs.angularjs.org/api/ng.$http" target="_blank"&gt;&lt;em&gt;Angular’s $http documentation&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;Luckily it’s pretty darn easy to get JQuery to follow this convention too and this will also work with 3rd party plugins that use JQuery for requests like &lt;a href="https://github.com/blueimp/jQuery-File-Upload" target="_blank"&gt;Blueimp file uploader&lt;/a&gt;. The easiest way to get this done is to set the global JQuery $.ajax rules. Probably the best place to do this is in your &lt;a href="http://docs.angularjs.org/guide/module" target="_blank"&gt;Angular app.run statement&lt;/a&gt;:&lt;/p&gt;&lt;pre class="csharpcode"&gt;app.run(&lt;span class="kwrd"&gt;function&lt;/span&gt; ($cookies) {

    &lt;span class="rem"&gt;//This sets the default jquery ajax headers to include our csrf token, we&lt;/span&gt;
    &lt;span class="rem"&gt;// need to user the beforeSend method because the token might change &lt;/span&gt;
    &lt;span class="rem"&gt;// (different for each logged in user)&lt;/span&gt;
    $.ajaxSetup({
        beforeSend: &lt;span class="kwrd"&gt;function&lt;/span&gt; (xhr) {
            xhr.setRequestHeader(&lt;span class="str"&gt;"X-XSRF-TOKEN"&lt;/span&gt;, $cookies[&lt;span class="str"&gt;"XSRF-TOKEN"&lt;/span&gt;]);
        }
    }); 
});&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
That’s it! &lt;/p&gt;
&lt;p&gt;&lt;em&gt;It’s important to note to set the header using beforeSend, if you just set $.ajax options ‘headers’ section directly that means the header cannot be dynamic – which you’ll probably want if you have users logging in/out.&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 06 Dec 2013 19:06:00 Z</pubDate>
      <a10:updated>2013-12-06T19:06:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1220</guid>
      <link>https://shazwazza.com/post/how-to-inspect-assemblies-with-reflection-before-including-them-in-your-application/</link>
      <category>Web Development</category>
      <title>How to inspect assemblies with reflection before including them in your application</title>
      <description>&lt;p&gt;If you application supports plugins or extensions in some cases it might be useful to scan a packages assemblies before importing them in to your app. Some reasons for this might be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Checking if the package has missing assembly references&lt;/li&gt;
&lt;li&gt;Checking if the assembly references obsolete types that might make the package unstable&lt;/li&gt;
&lt;li&gt;Checking the .Net targeted framework of the assembly&lt;/li&gt;
&lt;li&gt;Any other assembly inspection to determine it is compatible with your app&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To do this you can load assemblies using &lt;em&gt;Assembly.ReflectionOnlyLoadFrom &lt;/em&gt;and &lt;em&gt;Assembly.ReflectionOnlyLoad &lt;/em&gt;methods which load assemblies into a special &lt;a rel="noopener" href="http://msdn.microsoft.com/en-us/library/dd153782.aspx" target="_blank"&gt;assembly load context&lt;/a&gt; called “reflection-only context” which will safely let you inspect these assemblies.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Further reading:&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A great article on &lt;/em&gt;&lt;a rel="noopener" href="http://blogs.msdn.com/b/junfeng/archive/2004/08/24/219691.aspx" target="_blank"&gt;&lt;em&gt;Reflection Only Assembly Loading&lt;/em&gt;&lt;/a&gt;&lt;em&gt; and if you want to know more about assembly load contexts, here’s an &lt;/em&gt;&lt;a rel="noopener" href="http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx" target="_blank"&gt;&lt;em&gt;explanation&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Loading in assemblies&lt;/h2&gt;
&lt;p&gt;For this example, we’ll assume that all of the assemblies for a package are in some folder outside of the normal /bin folder (not loaded in to the current app) and each assembly for the package will need to be inspected for type references that are not supported.&lt;/p&gt;
&lt;p&gt;A common mistake when loading in assemblies with reflection (especially in the LoadFrom context) is to load them in one at a time, whereas they generally will need to be all loaded in before inspecting them since they probably have references to each other. Another thing that generally must be done is adding an event listener on &lt;em&gt;AppDomain.ReflectionOnlyAssemblyResolve &lt;/em&gt;because even though all known referenced assemblies are loading into the context some assemblies might not be explicitly referenced but are need to load the assembly. This handler provides a way to resolve those missing references.&lt;/p&gt;
&lt;p&gt;The first thing to do is setup the event handler&lt;/p&gt;
&lt;pre class="csharpcode"&gt;AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (s, e) =&amp;gt;
{
    var a = Assembly.ReflectionOnlyLoad(e.Name);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (a == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; TypeLoadException(&lt;span class="str"&gt;"Could not load assembly "&lt;/span&gt; + e.Name);
    &lt;span class="kwrd"&gt;return&lt;/span&gt; a;
};&lt;/pre&gt;
&lt;p&gt;Next we need to load all of the assembly files in the folder&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var f &lt;span class="kwrd"&gt;in&lt;/span&gt; files) Assembly.ReflectionOnlyLoadFrom(f);&lt;/pre&gt;
&lt;p&gt;Then load all of their referenced assemblies in to the reflection context&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//Then load each referenced assembly into the context&lt;/span&gt;
var assembliesWithErrors = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var f &lt;span class="kwrd"&gt;in&lt;/span&gt; files)
{
    var reflectedAssembly = Assembly.ReflectionOnlyLoadFrom(f);
    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var assemblyName &lt;span class="kwrd"&gt;in&lt;/span&gt; reflectedAssembly.GetReferencedAssemblies())
    {
        &lt;span class="kwrd"&gt;try&lt;/span&gt;
        {
            Assembly.ReflectionOnlyLoad(assemblyName.FullName);
        }
        &lt;span class="kwrd"&gt;catch&lt;/span&gt; (FileNotFoundException)
        {
            &lt;span class="rem"&gt;//if an exception occurs it means that a referenced assembly could not be found                        &lt;/span&gt;
            errors.Add(
                &lt;span class="kwrd"&gt;string&lt;/span&gt;.Concat(&lt;span class="str"&gt;"This package references the assembly '"&lt;/span&gt;,
                    assemblyName.Name,
                    &lt;span class="str"&gt;"' which was not found, this package may have problems running"&lt;/span&gt;));
            assembliesWithErrors.Add(f);
        }
    }
}&lt;/pre&gt;
&lt;p&gt;In the &lt;em&gt;catch&lt;/em&gt;, I’m detecting assembly reference errors and adding an error message to the outgoing method response and also adding that assembly to the assembliesWithErrors list which is used later to ensure we’re not inspecting assemblies that couldn’t be loaded.&lt;/p&gt;
&lt;p&gt;Now that all the assemblies are loaded we can inspect them (ignoring ones with errors). This example is looking for any assemblies that have types implementing ‘MyType’. If they do implement this type, add the assembly to a list to return from the current method.&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//now that we have all referenced types into the context we can look up stuff&lt;/span&gt;
&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var f &lt;span class="kwrd"&gt;in&lt;/span&gt; files.Except(assembliesWithErrors))
{
    &lt;span class="rem"&gt;//now we need to see if they contain any type 'MyType'&lt;/span&gt;
    var reflectedAssembly = Assembly.ReflectionOnlyLoadFrom(f);
    var found = reflectedAssembly.GetExportedTypes()
        .Where(TypeHelper.IsTypeAssignableFrom&amp;lt;MyType&amp;gt;);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (found.Any())
    {
        dllsWithReference.Add(reflectedAssembly.FullName);
    }
}&lt;/pre&gt;
&lt;h2&gt;Separate AppDomain&lt;/h2&gt;
&lt;p&gt;It’s best to execute all of this logic in a separate AppDomain because once assemblies are loaded in to a context, they cannot be unloaded and since we are loading in from files, those files will remain locked until the AppDomain is shutdown. Explaining how to create a separate AppDomain is outside the scope of this article but the code is included in the source below.&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;Here’s a class the encapsulates all of this logic and of course if you can do much more when inspecting assemblies for various types.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/Shazwazza/7147978"&gt;https://gist.github.com/Shazwazza/7147978&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 25 Oct 2013 22:13:00 Z</pubDate>
      <a10:updated>2013-10-25T22:13:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1278</guid>
      <link>https://shazwazza.com/post/reference-the-current-form-controller-in-angularjs/</link>
      <category>Web Development</category>
      <title>Reference the current form controller in AngularJS</title>
      <description>&lt;p&gt;I previously wrote a post about &lt;a href="http://shazwazza.com/post/Listening-for-validation-changes-in-AngularJS"&gt;Listening for validation changes in AngularJS&lt;/a&gt; which with my knowledge at that time required a handy hack to get a reference to the currently scoped form controller (ngForm) for a given input control. I also complained a bit that it seemed that angular didn’t really provide a way to reference the current form controller without this little hack… well, it turns out I was wrong! :)&lt;/p&gt; &lt;p&gt;AngularJS seems kind of like ASP.Net MVC in the early days when there wasn’t much documentation…&amp;nbsp; It definitely pays off to read through the source code to figure out how to do more complicated things. I had a bit of a ‘light bulb’ moment when I realized that ngForm was itself a directive/controller and had recently noticed that the ‘require’ parameter of setting up a directive allows you to search for controllers in the current directives ancestry (i.e. prefix the required controller with a hat: ^ )&lt;/p&gt; &lt;h2&gt;What does the require parameter of a directive do?&lt;/h2&gt; &lt;p&gt;Lets face it, the &lt;a href="http://docs.angularjs.org/guide/directive" target="_blank"&gt;directive documentation&lt;/a&gt; for AngularJS is in desperate need of being updated so that human beings can understand it (as noted by the many comments at the bottom). So I’ll try to explain what the ‘require’ parameter actually does and how to use it. &lt;/p&gt; &lt;p&gt;We’ll create a simple custom validation directive which will invalidate a field if the value is “blah”&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;function&lt;/span&gt; blahValidator() {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; {
        require: &lt;span class="str"&gt;'ngModel'&lt;/span&gt;,
        link: &lt;span class="kwrd"&gt;function&lt;/span&gt;(scope, elm, attr, ctrl) {
            
            &lt;span class="kwrd"&gt;var&lt;/span&gt; validator = &lt;span class="kwrd"&gt;function&lt;/span&gt;(value) {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (ctrl.$viewValue == &lt;span class="str"&gt;"blah"&lt;/span&gt;) {
                    ctrl.$setValidity(&lt;span class="str"&gt;'blah'&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);
                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
                }
                &lt;span class="kwrd"&gt;else&lt;/span&gt; {
                    ctrl.$setValidity(&lt;span class="str"&gt;'blah'&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
                    &lt;span class="kwrd"&gt;return&lt;/span&gt; value;
                }
            };

            ctrl.$formatters.push(validator);
            ctrl.$parsers.push(validator);
        }
    };
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;You’ll notice that we have a ‘require’ parameter specified for ‘ngModel’. What is happening here is that when we assign this directive to an input field, angular will ensure that the input field also has a defined ng-model attribute on it as well. Then angular will pass in the instance of the ng-model controller to the ‘ctrl’ parameter of the link function.&lt;/p&gt;
&lt;p&gt;So, the ‘require’ parameter dictates what the ‘ctrl’ parameter of the link function equals. &lt;/p&gt;
&lt;p&gt;You can also require multiple controllers:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_34.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_32.png" width="462" height="138"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;NOTE: the ctrl/ctrls parameter in the above 2 examples can be called whatever you want&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Special prefixes&lt;/h2&gt;
&lt;p&gt;Angular has 2 special prefixes for the ‘require’ parameter: &lt;/p&gt;
&lt;p&gt;^ = search the current directives ancestry for the controller&lt;/p&gt;
&lt;p&gt;? = don’t throw an exception if the required controller is not found, making it ‘optional’ not a requirement&lt;/p&gt;
&lt;p&gt;You can also combine them so angular will search the ancestry but it can be optional too such as: ^?ngController'&lt;/p&gt;
&lt;p&gt;In the above example, the blahValidator will only work if the directive is declared inside of an ng-controller block.&lt;/p&gt;
&lt;h2&gt;Referencing the current ng-form&lt;/h2&gt;
&lt;p&gt;Given the above examples, and knowing the ngForm itself is a controller we should be able to just make a requirement on ngForm and have it injected into the directive. BUT, it wont work the way you expect. For some reason angular references the ngForm controller by the name “form” which i discovered by browsing the source of angular.&lt;/p&gt;
&lt;p&gt;So now its easy to get a reference to the containing ngForm controller, all you need to do is add a ‘require’ parameter to your directive that looks like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;require: '^form'&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
and it will be injected into your ctrl parameter of your link function.</description>
      <pubDate>Wed, 26 Jun 2013 23:08:00 Z</pubDate>
      <a10:updated>2013-06-26T23:08:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1275</guid>
      <link>https://shazwazza.com/post/ndepend-review-pretty-cool/</link>
      <category>Web Development</category>
      <title>NDepend review = pretty cool!</title>
      <description>&lt;p&gt;For a while I’ve been wanting to automate some reporting from our build process to show some fairly simple statistical information such as &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Obsoleted code  &lt;li&gt;Internal code planned to be made public  &lt;li&gt;Code flagged to be obsoleted  &lt;li&gt;Code flagged as experimental&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;em&gt;NOTE: some of the above are based on our own internal c# attributes&lt;/em&gt;&lt;/p&gt; &lt;p&gt;Originally I was just going to write some code to show me this which would have been fairly straight forward but I knew that &lt;a href="http://www.ndepend.com/"&gt;NDepend&lt;/a&gt; had a code query language and natively outputs reports and can be integrated into the build process, so I figured I’d give it a go.&lt;/p&gt; &lt;p&gt;So I went and installed NDepend and ran the analysis against my current project … I wasn’t quite prepared for the amount of information it gave me! :)&lt;/p&gt; &lt;h2&gt;Reporting&lt;/h2&gt; &lt;p&gt;I quickly found out that the reports that I wanted to create were insanely simple to write. I then lost a large part of my day exploring the reports it generates OOTB and I realized I hadn’t even touched the surface of the information this tool could give me. I was mostly interested in statistical analysis but this will also happily tell you about how bad or good your code is, how much test coverage you’ve got, what backwards compatibility you broke with your latest release, what code is most used in your project and what code isn’t used at all… and the list goes on and on. &lt;/p&gt; &lt;p&gt;To give you an example of the first report I wanted to write, here’s all I wrote to give me what methods in my codebase were obsoleted:&lt;/p&gt;&lt;pre class="csharpcode"&gt;from m &lt;span class="kwrd"&gt;in&lt;/span&gt; JustMyCode.Methods &lt;span class="kwrd"&gt;where&lt;/span&gt; m.HasAttribute(&lt;span class="str"&gt;"System.ObsoleteAttribute"&lt;/span&gt;) select m&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;What’s cool about the report output is that you can output to HTML or XML, so if you want to publish it on your site you can just write some XSLT to make it look the way you want.&lt;/p&gt;
&lt;h2&gt;Project comparison&lt;/h2&gt;
&lt;p&gt;Another really great feature that I thought would be fun to automate is the code analysis against previous builds so you know straight away if you are breaking compatibility with previous releases. I think it has great potential but what I did discover is that in some cases the OOTB queries for this aren’t perfect. It will tell you what public types, methods, and fields have been removed or changed and the query is pretty straightforward but… In many cases when we’re trying to maintain backwards compatibility while creating new APIs we’ll remove a type’s methods/properties and just make the legacy type inherit from the new API class (or some other trickery similar to that). This might break compatibility if people are using reflection or specific type checking but it wont break the API usage which I’m mostly interested in. I haven’t figured it out yet but I’m sure there’s some zany NDepend query I can write to check that.&lt;/p&gt;
&lt;h2&gt;Automation&lt;/h2&gt;
&lt;p&gt;So now that I know I can extract all this info, it’s time to decide what to export. Thankfully NDepend comes fully equipped with it’s own MSBuild integration so automating the report output should be pretty straightforward. I also noticed that if you wanted to be ultra careful you can integrate it with your build server and have your builds fail if some of your queries cause errors, like the above: If you break compatibility the build will fail. Or anything you want really, like failing the build if we detect you’ve written a method that has too many lines of code.&lt;/p&gt;
&lt;h2&gt;Pretty cool indeed!&lt;/h2&gt;
&lt;p&gt;I was going to use NDepend just to output reports which it clearly does without issue. What I didn’t realize was how much detail you can get about your code with this tool. There’s some tools in there that were a bit over my head but I suppose if you are working on really huge projects and need anything from just a snapshot or insane details into what dependencies your code has on other code it’ll do that easily as well. &lt;/p&gt;
&lt;p&gt;The UI is a bit overwhelming at first and could probably do with some work but TBH I’m not sure how else you’d display that much information nicely. Once I figured out how the whole query and reporting structures work then it was dead simple.&lt;/p&gt;
&lt;p&gt;Happy reporting :)&lt;/p&gt;</description>
      <pubDate>Fri, 21 Jun 2013 23:47:00 Z</pubDate>
      <a10:updated>2013-06-21T23:47:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1126</guid>
      <link>https://shazwazza.com/post/listening-for-validation-changes-in-angularjs/</link>
      <category>Web Development</category>
      <title>Listening for validation changes in AngularJS</title>
      <description>&lt;p&gt;In some applications it can be really useful to have controllers listen for validation changes especially in more complicated AngularJS apps where ‘ng-repeat’ is used to render form controls. There’s plenty of cases where a parent scope might need to know about validation changes based on child scopes… one such case is a validation summary. There’s a couple ways to implement this (and probably more) but they all seem a bit hacky such as:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Apply a $watch to the current form object’s $valid property in the parent scope, then use jQuery to look for elements that have a class like ‘invalid’  &lt;ul&gt; &lt;li&gt;You could then use the scope() function on the DOM element that ng-repeat is used on to get any model information about the invalid item&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;In child scopes you could apply a $watch to individual form elements’ $valid property then change the $parent scope’s model values to indicate validation changes&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Instead what I wanted to achieve was a re-usable way to ‘bubble’ up validation changes from any scope’s form element to ancestor scopes without having to do any of the following:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;No jquery DOM selection  &lt;li&gt;No hard coding of form names to access the validation objects  &lt;li&gt;No requirement to modifying other scopes’ values&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Implementation&lt;/h2&gt; &lt;p&gt;The way I went about this was to create a very simple custom directive which I’ve called ‘val-bubble’ since it has to do with validation and it ‘bubbles’ up a message to any listening scopes. An input element might then look like this:&lt;/p&gt; &lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;input&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="FirstName"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="text"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;required&lt;/span&gt; &lt;span style="color: #ff0000"&gt;val-bubble&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;Then in an outer scope I can then listen for validation changes and do whatever I want with the result:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;scope.$on(&lt;span style="color: #006080"&gt;"valBubble"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;(evt, args) {&lt;br&gt;    alert(&lt;span style="color: #006080"&gt;"Validation changed for field "&lt;/span&gt; + args.ctrl.$name + &lt;span style="color: #006080"&gt;". Valid? "&lt;/span&gt; + args.isValid);&lt;br&gt;});&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The args object contains these properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;isValid = is the field valid 
&lt;li&gt;ctrl = the current form controller object for the field 
&lt;li&gt;scope = the scope bound to the field being validated 
&lt;li&gt;element = the DOM element of the field being validated 
&lt;li&gt;expression = the current $watch expression used to watch this fields validation changes&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;With all of that information you can easily adds some additional functionality to your app based on the current validating inputs such as a validation summary or whatever.&lt;/p&gt;
&lt;h2&gt;Custom directive&lt;/h2&gt;
&lt;p&gt;The val-bubble custom directive is pretty simple, here’s the code and an explanation below:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 500px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;app.directive(&lt;span style="color: #006080"&gt;'valBubble'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (formHelper) {&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; {&lt;br&gt;        require: &lt;span style="color: #006080"&gt;'ngModel'&lt;/span&gt;,&lt;br&gt;        restrict: &lt;span style="color: #006080"&gt;"A"&lt;/span&gt;,&lt;br&gt;        link: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (scope, element, attr, ctrl) {&lt;br&gt;&lt;br&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!attr.name) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #006080"&gt;"valBubble must be set on an input element that has a 'name' attribute"&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;                &lt;br&gt;            &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; currentForm = formHelper.getCurrentForm(scope);&lt;br&gt;            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!currentForm || !currentForm.$name)&lt;br&gt;                &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #006080"&gt;"valBubble requires that a name is assigned to the ng-form containing the validated input"&lt;/span&gt;;&lt;br&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;//watch the current form's validation for the current field name&lt;/span&gt;&lt;br&gt;            scope.$watch(currentForm.$name + &lt;span style="color: #006080"&gt;"."&lt;/span&gt; + ctrl.$name + &lt;span style="color: #006080"&gt;".$valid"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (isValid, lastValue) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (isValid != undefined) {&lt;br&gt;                    &lt;span style="color: #008000"&gt;//emit an event upwards &lt;/span&gt;&lt;br&gt;                    scope.$emit(&lt;span style="color: #006080"&gt;"valBubble"&lt;/span&gt;, {&lt;br&gt;                        isValid: isValid,       &lt;span style="color: #008000"&gt;// if the field is valid&lt;/span&gt;&lt;br&gt;                        element: element,       &lt;span style="color: #008000"&gt;// the element that the validation applies to&lt;/span&gt;&lt;br&gt;                        expression: &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.exp,   &lt;span style="color: #008000"&gt;// the expression that was watched to check validity&lt;/span&gt;&lt;br&gt;                        scope: scope,           &lt;span style="color: #008000"&gt;// the current scope&lt;/span&gt;&lt;br&gt;                        ctrl: ctrl              &lt;span style="color: #008000"&gt;// the current controller&lt;/span&gt;&lt;br&gt;                    });&lt;br&gt;                }&lt;br&gt;            });&lt;br&gt;        }&lt;br&gt;    };&lt;br&gt;});&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The first thing we’re doing here is limiting this directive to be used only as an attribute and ensuring the element has a model applied to it. Then we make sure that the element has a ‘name’ value applied. After that we are getting a reference to the current form object that this field is contained within using a custom method: &lt;em&gt;formHelper.getCurrentForm&lt;/em&gt; … more on this below. Lastly we are applying a $watch to the current element’s $valid property and when this value changes we $emit an event upwards to parent/ancestor scopes to listen for. &lt;/p&gt;
&lt;h2&gt;formHelper&lt;/h2&gt;
&lt;p&gt;Above I mentioned that I wanted a re-usable solution where I didn’t need to hard code things like the current form name. &lt;span style="text-decoration: line-through"&gt;Unfortunately Angular doesn’t really provide a way to do this OOTB (as far as I can tell!)&lt;/span&gt; &lt;em&gt;(Update! see here on how to access the current form: &lt;/em&gt;&lt;a href="http://shazwazza.com/post/Reference-the-current-form-controller-in-AngularJS"&gt;&lt;em&gt;http://shazwazza.com/post/Reference-the-current-form-controller-in-AngularJS&lt;/em&gt;&lt;/a&gt;&lt;em&gt;)&lt;/em&gt; so I’ve just created a simple factory object that finds the current form object applied to the current scope. The type check is fairly rudimentary but it works, it’s simply checking each property that exists on the scope object and tries to detect the object that matches the definition of an Angular form object:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 500px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;app.factory(&lt;span style="color: #006080"&gt;'formHelper'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; {&lt;br&gt;        getCurrentForm: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;(scope) {&lt;br&gt;            &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; form = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br&gt;            &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; requiredFormProps = [&lt;span style="color: #006080"&gt;"$error"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$name"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$dirty"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$pristine"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$valid"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$invalid"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$addControl"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$removeControl"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$setValidity"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"$setDirty"&lt;/span&gt;];&lt;br&gt;            &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; scope) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_.isObject(scope[p]) &amp;amp;&amp;amp; !_.isFunction(scope[p]) &amp;amp;&amp;amp; !_.isArray(scope[p]) &amp;amp;&amp;amp; p.substr(0, 1) != &lt;span style="color: #006080"&gt;"$"&lt;/span&gt;) {&lt;br&gt;                    &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; props = _.keys(scope[p]);&lt;br&gt;                    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (props.length &amp;lt; requiredFormProps.length) &lt;span style="color: #0000ff"&gt;continue&lt;/span&gt;;&lt;br&gt;                    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_.every(requiredFormProps, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;(item) {&lt;br&gt;                        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _.contains(props, item);&lt;br&gt;                    })) {&lt;br&gt;                        form = scope[p];&lt;br&gt;                        &lt;span style="color: #0000ff"&gt;break&lt;/span&gt;;&lt;br&gt;                    }&lt;br&gt;                }&lt;br&gt;            }&lt;br&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; form;&lt;br&gt;        }  &lt;br&gt;    };&lt;br&gt;});&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;NOTE: the above code has a dependency on UnderscoreJS&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So now you can just apply the val-bubble attribute to any input element to ensure it’s validation changes are published to listening scopes!&lt;/p&gt;</description>
      <pubDate>Tue, 28 May 2013 06:18:00 Z</pubDate>
      <a10:updated>2013-05-28T06:18:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1127</guid>
      <link>https://shazwazza.com/post/uploading-files-and-json-data-in-the-same-request-with-angular-js/</link>
      <category>Web Development</category>
      <title>Uploading files and JSON data in the same request with Angular JS</title>
      <description>&lt;p&gt;I decided to write a quick blog post about this because much of the documentation and examples about this seems to be a bit scattered. What this achieves is the ability to upload any number of files with any other type of data in &lt;strong&gt;one&lt;/strong&gt; request. For this example we’ll send up JSON data along with some files.&lt;/p&gt; &lt;h2&gt;File upload directive&lt;/h2&gt; &lt;p&gt;First we’ll create a simple custom file upload angular directive&lt;/p&gt; &lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; height: 261px; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 420px; width: 97.99%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; height: 243px; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;app.directive(&lt;span style="color: #006080"&gt;'fileUpload'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; () {&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; {&lt;br&gt;        scope: &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;,        &lt;span style="color: #008000"&gt;//create a new scope&lt;/span&gt;&lt;br&gt;        link: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (scope, el, attrs) {&lt;br&gt;            el.bind(&lt;span style="color: #006080"&gt;'change'&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;event&lt;/span&gt;) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; files = &lt;span style="color: #0000ff"&gt;event&lt;/span&gt;.target.files;&lt;br&gt;                &lt;span style="color: #008000"&gt;//iterate files since 'multiple' may be specified on the element&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; i = 0;i&amp;lt;files.length;i++) {&lt;br&gt;                    &lt;span style="color: #008000"&gt;//emit event upward&lt;/span&gt;&lt;br&gt;                    scope.$emit(&lt;span style="color: #006080"&gt;"fileSelected"&lt;/span&gt;, { file: files[i] });&lt;br&gt;                }                                       &lt;br&gt;            });&lt;br&gt;        }&lt;br&gt;    };&lt;br&gt;});&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The usage of this is simple:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;input&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;file-upload&lt;/span&gt; &lt;span style="color: #ff0000"&gt;multiple&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The ‘multiple’ parameter indicates that the user can select multiple files to upload which this example fully supports.&lt;/p&gt;
&lt;p&gt;In the directive we ensure a new scope is created and then listen for changes made to the file input element. When changes are detected with emit an event to all ancestor scopes (upward) with the file object as a parameter.&lt;/p&gt;
&lt;h2&gt;Mark-up &amp;amp; the controller&lt;/h2&gt;
&lt;p&gt;Next we’ll create a controller to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a model to bind to 
&lt;li&gt;Create a collection of files 
&lt;li&gt;Consume this event so we can assign the files to&amp;nbsp; the collection 
&lt;li&gt;Create a method to post it all to the server&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;NOTE: I’ve put all this functionality in this controller for brevity, in most cases you’d have a separate factory to handle posting the data&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;With the controller in place, the mark-up might look like this (and will display the file names of all of the files selected):&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;div&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ng-controller&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="Ctrl"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;input&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file"&lt;/span&gt; &lt;span style="color: #ff0000"&gt;file-upload&lt;/span&gt; &lt;span style="color: #ff0000"&gt;multiple&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ul&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;li&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ng-repeat&lt;/span&gt;&lt;span style="color: #0000ff"&gt;="file in files"&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;{{file.name}}&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;li&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ul&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;div&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;The controller code below contains some important comments relating to how the data gets posted up to the server, namely the ‘Content-Type’ header as the value that needs to be set is a bit quirky. &lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 1000px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; Ctrl($scope, $http) {&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//a simple model to bind to and send to the server&lt;/span&gt;&lt;br&gt;    $scope.model = {&lt;br&gt;        name: &lt;span style="color: #006080"&gt;""&lt;/span&gt;,&lt;br&gt;        comments: &lt;span style="color: #006080"&gt;""&lt;/span&gt;&lt;br&gt;    };&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//an array of files selected&lt;/span&gt;&lt;br&gt;    $scope.files = [];&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//listen for the file selected event&lt;/span&gt;&lt;br&gt;    $scope.$on(&lt;span style="color: #006080"&gt;"fileSelected"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;event&lt;/span&gt;, args) {&lt;br&gt;        $scope.$apply(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; () {            &lt;br&gt;            &lt;span style="color: #008000"&gt;//add the file object to the scope's files collection&lt;/span&gt;&lt;br&gt;            $scope.files.push(args.file);&lt;br&gt;        });&lt;br&gt;    });&lt;br&gt;    &lt;br&gt;    &lt;span style="color: #008000"&gt;//the save method&lt;/span&gt;&lt;br&gt;    $scope.save = &lt;span style="color: #0000ff"&gt;function&lt;/span&gt;() {&lt;br&gt;        $http({&lt;br&gt;            method: &lt;span style="color: #006080"&gt;'POST'&lt;/span&gt;,&lt;br&gt;            url: &lt;span style="color: #006080"&gt;"/Api/PostStuff"&lt;/span&gt;,&lt;br&gt;            &lt;span style="color: #008000"&gt;//IMPORTANT!!! You might think this should be set to 'multipart/form-data' &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// but this is not true because when we are sending up files the request &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// needs to include a 'boundary' parameter which identifies the boundary &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// name between parts in this multi-part request and setting the Content-type &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// manually will not set this boundary parameter. For whatever reason, &lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// setting the Content-type to 'false' will force the request to automatically&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// populate the headers properly including the boundary parameter.&lt;/span&gt;&lt;br&gt;            headers: { &lt;span style="color: #006080"&gt;'Content-Type'&lt;/span&gt;: &lt;span style="color: #0000ff"&gt;false&lt;/span&gt; },&lt;br&gt;            &lt;span style="color: #008000"&gt;//This method will allow us to change how the data is sent up to the server&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// for which we'll need to encapsulate the model data in 'FormData'&lt;/span&gt;&lt;br&gt;            transformRequest: &lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data) {&lt;br&gt;                &lt;span style="color: #0000ff"&gt;var&lt;/span&gt; formData = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FormData();&lt;br&gt;                &lt;span style="color: #008000"&gt;//need to convert our json object to a string version of json otherwise&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #008000"&gt;// the browser will do a 'toString()' on the object which will result &lt;/span&gt;&lt;br&gt;                &lt;span style="color: #008000"&gt;// in the value '[Object object]' on the server.&lt;/span&gt;&lt;br&gt;                formData.append(&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;, angular.toJson(data.model));&lt;br&gt;                &lt;span style="color: #008000"&gt;//now add all of the assigned files&lt;/span&gt;&lt;br&gt;                &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;var&lt;/span&gt; i = 0; i &amp;lt; data.files; i++) {&lt;br&gt;                    &lt;span style="color: #008000"&gt;//add each file to the form data and iteratively name them&lt;/span&gt;&lt;br&gt;                    formData.append(&lt;span style="color: #006080"&gt;"file"&lt;/span&gt; + i, data.files[i]);&lt;br&gt;                }&lt;br&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; formData;&lt;br&gt;            },&lt;br&gt;            &lt;span style="color: #008000"&gt;//Create an object that contains the model and files which will be transformed&lt;/span&gt;&lt;br&gt;            &lt;span style="color: #008000"&gt;// in the above transformRequest method&lt;/span&gt;&lt;br&gt;            data: { model: $scope.model, files: $scope.files }&lt;br&gt;        }).&lt;br&gt;        success(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data, status, headers, config) {&lt;br&gt;            alert(&lt;span style="color: #006080"&gt;"success!"&lt;/span&gt;);&lt;br&gt;        }).&lt;br&gt;        error(&lt;span style="color: #0000ff"&gt;function&lt;/span&gt; (data, status, headers, config) {&lt;br&gt;            alert(&lt;span style="color: #006080"&gt;"failed!"&lt;/span&gt;);&lt;br&gt;        });&lt;br&gt;    };&lt;br&gt;};&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;h2&gt;Handling the data server-side&lt;/h2&gt;
&lt;p&gt;This example shows how to handle the data on the server side using ASP.Net WebAPI, I’m sure it’s reasonably easy to do on other server-side platforms too.&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 500px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; async Task&amp;lt;HttpResponseMessage&amp;gt; PostStuff()&lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!Request.Content.IsMimeMultipartContent())&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.UnsupportedMediaType);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    var root = HttpContext.Current.Server.MapPath(&lt;span style="color: #006080"&gt;"~/App_Data/Temp/FileUploads"&lt;/span&gt;);&lt;br&gt;    Directory.CreateDirectory(root);&lt;br&gt;    var provider = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MultipartFormDataStreamProvider(root);&lt;br&gt;    var result = await Request.Content.ReadAsMultipartAsync(provider);&lt;br&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (result.FormData[&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;] == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; HttpResponseException(HttpStatusCode.BadRequest);&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    var model = result.FormData[&lt;span style="color: #006080"&gt;"model"&lt;/span&gt;];&lt;br&gt;    &lt;span style="color: #008000"&gt;//TODO: Do something with the json model which is currently a string&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #008000"&gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="color: #008000"&gt;//get the files&lt;/span&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var file &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; result.FileData)&lt;br&gt;    {                &lt;br&gt;        &lt;span style="color: #008000"&gt;//TODO: Do something with each uploaded file&lt;/span&gt;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Request.CreateResponse(HttpStatusCode.OK, &lt;span style="color: #006080"&gt;"success!"&lt;/span&gt;);&lt;br&gt;}&lt;/p&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 25 May 2013 04:01:00 Z</pubDate>
      <a10:updated>2013-05-25T04:01:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1165</guid>
      <link>https://shazwazza.com/post/razor-plus-dynamic-plus-internal-plus-interface-the-object-does-not-contain-a-definition-for-xxxx-exception/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Razor + dynamic + internal + interface &amp; the 'object' does not contain a definition for 'xxxx' exception</title>
      <description>&lt;p&gt;I’ve come across this strange issue and decided to blog about it since I can’t figure out exactly why this is happening it is clearly something to do with the DLR and interfaces.&lt;/p&gt; &lt;p&gt;First, if you are getting the exception:&lt;em&gt; 'object' does not contain a definition for 'xxxx'&lt;/em&gt; it is related to either an object you have being marked internal or you are are using an anonymous object type for your model (which .Net will always mark as internal). &lt;/p&gt; &lt;p&gt;Here’s a really easy way to replicate this:&lt;/p&gt; &lt;p&gt;1. Create an internal model&lt;/p&gt; &lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; height: 80px; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 38.17%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel&lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;2. Return this model in your MVC action&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br&gt;{            &lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InternalTestModel(&lt;span style="color: #006080"&gt;"Shannon"&lt;/span&gt;));&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;3. Make your view have a dynamic model and then try to render the model’s property&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;@model dynamic&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;@Model.Name&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;You’ll get the exception:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Server Error in '/' Application. &lt;/em&gt;
&lt;p&gt;&lt;em&gt;'object' does not contain a definition for 'Name'&lt;/em&gt; 
&lt;p&gt;So even though the error is not very informative, it makes sense since Razor is trying to access an internal class. &lt;/p&gt;
&lt;h2&gt;Try using a public interface&lt;/h2&gt;
&lt;p&gt;Ok so if we want to keep our class internal, we could expose it via a public interface. Our code might then look like this:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IModel &lt;br&gt;{    &lt;br&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel : IModel &lt;br&gt;{        &lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;Then we can change our view to be strongly typed like:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;@model IModel&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;@Model.Name&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;And it will work, however if you change your view back to be &lt;em&gt;@model dynamic&lt;/em&gt; you &lt;strong&gt;will still get the exception&lt;/strong&gt;. Pretty sure it’s because the DLR is just binding to the instance object and doesn’t really care about the interface… this makes sense.&lt;/p&gt;
&lt;h2&gt;Try using an abstract public class&lt;/h2&gt;
&lt;p&gt;For some reason if you make your internal class inherit from a public abstract class that implements the same interface you will not get this exception even though the object instance you are passing to razor is internal. For example with this structure:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IModel &lt;br&gt;{        &lt;br&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ModelBase : IModel &lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel : IModel &lt;br&gt;{            &lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;You &lt;strong&gt;will not&lt;/strong&gt; get this error if your view is &lt;em&gt;@model dynamic. &lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Strange!&lt;/h2&gt;
&lt;p&gt;I’m sure there’s something written in the DLR spec about this but still seems strange to me! If you are getting this error and you aren’t using anonymous objects for your model, hopefully this might help you figure out what is going on.&lt;/p&gt;</description>
      <pubDate>Thu, 11 Apr 2013 16:49:00 Z</pubDate>
      <a10:updated>2013-04-11T16:49:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1256</guid>
      <link>https://shazwazza.com/post/taming-the-buildmanager-aspnet-temp-files-and-appdomain-restarts/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Taming the BuildManager, ASP.Net Temp files and AppDomain restarts</title>
      <description>&lt;p&gt;I’ve recently had to do a bunch of research in to the BuildManager and how it deals with caching assemblies since my involvement with creating an &lt;a href="http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx"&gt;ASP.Net plugin engine&lt;/a&gt;. Many times people will attempt to restart their AppDomain by ‘bumping’ their web.config files, meaning adding a space character or carriage return and then saving it. Sometimes you may have noticed that this does not always restart your AppDomain the way you’d expect and some of the new types you’d expect to have loaded in your AppDomain simply aren’t there. This situation has cropped up a few times when using the plugin engine that we have built in to &lt;a href="http://umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; v5 and some people have resorted to manually clearing out their ASP.Net temp files and then forcing an IIS Restart, but this is hardly ideal. The good news is that we do have control over how these assemblies get refreshed, in fact the BuildManager reloads/refreshes/clears assemblies in different ways depending on how the AppDomain is restarted.&lt;/p&gt; &lt;h2&gt;The hash.web file&lt;/h2&gt; &lt;p&gt;An important step during the BuildManager initialization phase is a method call to &lt;em&gt;BuildManager.CheckTopLevelFilesUpToDate&lt;/em&gt; which does some interesting things. First it checks if a special hash value is on disk and is not zero. You may have noticed a file in your ASP.Net temp files: /hash/hash.web and it will contain a value such as: 4f34227133de5346. This value represents a unique hash of many different combined objects that the BuildManager monitors. If this file exists and the value can be parsed properly then the BuildManager will call: &lt;em&gt;diskCache.RemoveOldTempFiles(); &lt;/em&gt;What this does is the following:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;removes the CodeGen temp resource folder&lt;/li&gt; &lt;li&gt;removes temp files that have been saved during CodeGen such as *.cs &lt;/li&gt; &lt;li&gt;removes the special &lt;em&gt;.delete &lt;/em&gt;files and their associated original files which have been created when shutting down the app pool and when the .dll files cannot be removed (due to file locking)&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Creating the hash value&lt;/h2&gt; &lt;p&gt;The next step is to create this hash value based on the current objects in the AppDomain. This is done using an internal utility class in the .Net Framework called: &lt;em&gt;HashCodeCombiner&lt;/em&gt; (pretty much everything that the BuildManager references is marked Internal! ). The process combines the following object values to create the hash (I’ve added in parenthesis the actual properties the BuildManager references):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;the app's physical path, in case it changes &lt;em&gt;(HttpRuntime.AppDomainAppPathInternal)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;System.Web.dll &lt;em&gt;(typeof(HttpRuntime).Module.FullyQualifiedName)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;machine.config file name &lt;em&gt;(HttpConfigurationSystem.MachineConfigurationFilePath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;strong&gt;root &lt;/strong&gt;web.config file name, please note that this is not your web apps web.config &lt;em&gt;(HttpConfigurationSystem.RootWebConfigurationFilePath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the hash of the &amp;lt;compilation&amp;gt; section &lt;em&gt;(compConfig.RecompilationHash)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the hash of the system.web/profile section &lt;em&gt;(profileSection.RecompilationHash)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the file encoding set in config &lt;em&gt;(appConfig.Globalization.FileEncoding)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the &amp;lt;trust&amp;gt; config section &lt;em&gt;(appConfig.Trust.Level &amp;amp; appConfig.Trust.OriginUrl)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;whether profile is enabled &lt;em&gt;(ProfileManager.Enabled)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;whether we are precompiling with debug info (but who precompiles :) &lt;em&gt;(PrecompilingWithDebugInfo)&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Then we do a check for something I didn’t know existed called &lt;a href="http://blogs.msdn.com/b/davidebb/archive/2009/04/15/a-new-flag-to-optimize-asp-net-compilation-behavior.aspx" target="_blank"&gt;&lt;em&gt;Optimize Compilations&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;which will not actual affect the hash file value for the following if it is set to true (by default is is false):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;the ‘bin’ folder &lt;em&gt;(HttpRuntime.BinDirectoryInternal)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_WebReferences &lt;em&gt;(HttpRuntime.WebRefDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_GlobalResources&amp;nbsp; &lt;em&gt;(HttpRuntime.ResourcesDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_Code &lt;em&gt;(HttpRuntime.CodeDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;Global.asax &lt;em&gt;(GlobalAsaxVirtualPath)&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Refreshing the ASP.Net temp files (CodeGen files)&lt;/h2&gt; &lt;p&gt;The last step of this process is to check if the persisted hash value in the hash.web file equals the generated hash value from the above process. If they do not match then a call is made to &lt;em&gt;diskCache.RemoveAllCodegenFiles(); &lt;/em&gt;which will:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;clear all codegen files, removes all files in folders but not the folders themselves, &lt;/li&gt; &lt;li&gt;removes all root level files except for temp files that are generated such as .cs files, etc...&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This essentially clears your ASP.Net temp files completely, including the MVC controller cache file, etc…&lt;/p&gt; &lt;p&gt;Then the BuildManager simply resaves this calculated has back to the hash.web file.&lt;/p&gt; &lt;h2&gt;What is the best way to restart your AppDomain?&lt;/h2&gt; &lt;p&gt;There is really no ‘best’ way, it just depends on your needs. If you simply want to restart your AppDomain and not have the overhead of having your app recompile all of your views and other CodeGen classes then it’s best that you simply ‘bump’ your web.config by just adding a space, carriage return or whatever. As you can see above the hash value is not dependant on your local web.config file’s definition (timestamp, etc…). However, the hash value is dependent on some other stuff in your apps configuration such as the &amp;lt;compilation&amp;gt; section, system.web/profile section, the file encoding configured, and the &amp;lt;trust&amp;gt; section. If you update any value in any of these sections in your web.config it will force the BuildManager to clear all cached CodeGen files which is the same as clearing your ASP.Net temp files.&lt;/p&gt; &lt;p&gt;So long as you don’t have optimizeCompilations set to true, then the easiest way to clear your CodeGen files is to simply add a space or carriage return to your global.asax file or modify something in the 4 other places that the BuildManager checks locally: App_Code, App_GlobalResources, App_WebResources, or modify/add/remove a file in your bin folder.&lt;/p&gt; &lt;h2&gt;How does this affect the &lt;a href="http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx"&gt;ASP.Net plugin engine&lt;/a&gt;?&lt;/h2&gt; &lt;p&gt;Firstly, i need to update that post as the code in the plugin engine has changed quite a bit but you can find the latest in the &lt;a href="http://umbraco.codeplex.com/SourceControl/list/changesets" target="_blank"&gt;Umbraco source code on CodePlex&lt;/a&gt;. With the above knowledge its easy to clear out any stale plugins by perhaps bumping your global.asax file, however this is still not an ideal process. With the research I’ve done in to the BuildManager I’ve borrowed some concepts from it and learned of a special “.delete” file extension that the BuildManager looks for during AppDomain restarts. With this new info, I’ve already committed some new changes to the PluginManager so that you shouldn’t need to worry about stale plugin DLLs.&lt;/p&gt;</description>
      <pubDate>Wed, 28 Mar 2012 08:14:00 Z</pubDate>
      <a10:updated>2012-03-28T08:14:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1178</guid>
      <link>https://shazwazza.com/post/consume-json-rest-service-with-wcf-and-dynamic-object-response/</link>
      <category>Web Development</category>
      <title>Consume Json REST service with WCF and dynamic object response</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;This was my first shot at WCF and though I saw how nice it could be, i failed miserably… well, I’m actually not too sure. I tried all sorts of things but no matter what couldn’t get WCF to behave and return the actual response you would get if you just consumed the service with the WebClient object. Here’s what I tried and assumed that this ‘should’ work but the result was always an empty dictionary:  &lt;p&gt;Note: this demo is based off of the @Task project management tool’s REST API.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Define the contract:&lt;/strong&gt;&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:5ae3512e-d26b-4035-8b78-8ebdffa69f1e" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 221px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ServiceContract]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IAtTaskService
{        
    [OperationContract]
    [WebGet(
        UriTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;/api/login?username={username}&amp;amp;password={password}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, 
        RequestFormat &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; WebMessageFormat.Json)]
    IDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password);
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Run the code:&lt;/strong&gt;&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dab1d397-0418-4354-b055-8b1518fbc77a" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 239px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; IDictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; DoThis(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; pword)
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; (var cf &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebChannelFactory&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;IAtTaskService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;(
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; Uri(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;https://mycompaniesurl.attask-ondemand.com/attask/&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)))
    {
        var s &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; cf.CreateChannel();
        var token &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; s.Login(username, pword);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; token;
    } 
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;The above seemed really nice to be able to consume services but the result was always empty. I tried using strongly typed objects in the result, made sure it wasn’t an HTTPS issue, used the normal WCF configuration approach, etc… but couldn’t get it to work. &lt;/p&gt;
&lt;h2&gt;The ‘Fix’&lt;/h2&gt;
&lt;p&gt;I quite like the way that WCF has UriTemplates and you can just attribute up an interface and then make strongly typed calls to access your services so i decided I’d just make that work with my own implementation. Again, perhaps I was ‘doing it wrong’ or whatever, but the below implementation is pretty cool and also lets you get JSON results as a dynamic object :)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Define the contract:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is pretty much the same as above, but we have a dynamic response&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7ee1f6fd-2f30-4361-8f67-2226cad8e101" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 220px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ServiceContract]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IAtTaskService
{        
    [OperationContract]
    [WebGet(
        UriTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;/api/login?username={username}&amp;amp;password={password}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, 
        RequestFormat &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; WebMessageFormat.Json)]
    dynamic Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password);
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;h3&gt;The JsonRestService base class&lt;/h3&gt;
&lt;p&gt;I created a class that lets you specify you contract service as a generic argument and then provides you with a method called “Send” which is defined a:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e99f78fe-c8e3-4144-87ef-8acaad733291" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 24px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Send(Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; func)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;This class will manage all of the http requests and use the WCF attributes applied to your contract to generate the correct URLs and parameters (&lt;em&gt;&lt;strong&gt;source code at the bottom of this article&lt;/strong&gt;&lt;/em&gt;)&lt;/p&gt;
&lt;h3&gt;Define a standard service&lt;/h3&gt;
&lt;p&gt;So to consume the REST services, we just create a ‘real’ service class such as:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d69a4872-393c-4903-bdbf-ab4afd336724" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 284px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService : JsonRestService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;IAtTaskService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;, IAtTaskService
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl)
        : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;base&lt;/span&gt;&lt;span style="color: #000000"&gt;(serviceUrl)
    {
    }

    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Login(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; username, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; password)
    {
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; Send(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Login(username, password));
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the “Login” method just calls the underlying “Send” method which uses a strongly typed delegate. The Send method then inspects the delegate and generates the correct URL with parameters based on the WCF attributes.&lt;/p&gt;
&lt;h3&gt;Using the service&lt;/h3&gt;
&lt;p&gt;So now, to use the new service we can do something like:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:83d1461a-30a6-4d1b-86fb-a3a618e18f63" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 343px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[TestMethod]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; AtAtask_Login()
{

    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Arrange&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    var svc &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; AtTaskService(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;https://mycompanyname.attask-ondemand.com/attask/&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            
    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Act&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    var token &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; svc.Login(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;MyUserName&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;mypassword&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            
    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Assert&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    Assert.IsTrue(token &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);
    Assert.IsTrue(token.data &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);
    Assert.IsTrue(token.data.userID &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;);        
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Too easy!&lt;/p&gt;
&lt;p&gt;As I mentioned earlier, I would have assumed that WCF should handle this out of the box, but for the life of my I couldn’t get it to return any results. Whether it was a request issue or a parsing issue, or whatever i have no idea. In the meantime, here’s the source code for the JsonRestService class which will allow you to do the above. The source requires references to &lt;a href="http://json.codeplex.com/" target="_blank"&gt;Json.Net&lt;/a&gt; and &lt;a href="http://www.aaron-powell.com/dynamics-library" target="_blank"&gt;Aaron-powell.dynamics&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;JsonRestService source&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer: I made this today in a couple of hours, I’m sure there’s some code in here that could be refactored to be nicer&lt;/strong&gt;&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:88d9ad21-9a6f-4500-9815-53861fc44fb0" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 789px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonRestService&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; ServiceUrl { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonRestService(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl)
    {
        ServiceUrl &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; serviceUrl;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Queries the WCF attributes of the method being called, builds the REST URL and sends the http request
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; based on the WCF attribute parameters. When the JSON response is returned, it is changed to a dynamic object.
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="func"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; dynamic Send(Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; func)
    {
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;find the method on the main type that is being called... i'm sure there's a better way to do this,
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;but this does work.
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;This will not work if there are methods with overloads.&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var methodNameMatch &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Regex.Match(func.Method.Name, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;&amp;lt;(.*?)&amp;gt;&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #000000"&gt;methodNameMatch.Success &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000"&gt; methodNameMatch.Groups.Count &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;2&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; MissingMethodException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Could not find method &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; func.Method.Name);
        }
        var realMethodName &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; methodNameMatch.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value;
        var m &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(T).GetMethods().Where(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; realMethodName).SingleOrDefault();
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (m &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; MissingMethodException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Could not find method&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; realMethodName &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt; on type &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt;
                                                &lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(T).FullName);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;now that we have the method, find the wcf attributes&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var a &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; m.GetCustomAttributes(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;&lt;span style="color: #000000"&gt;);
        var webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; a.OfType&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;WebGetAttribute&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;().SingleOrDefault();
        var webInvoke &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; a.OfType&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;WebInvokeAttribute&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;().SingleOrDefault();
        var httpMethod &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;GET&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; : webInvoke &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; webInvoke.Method : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.Empty;
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.IsNullOrEmpty(httpMethod))
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;The WebGet or WebInvoke attribute is missing from the method &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt;
                                            realMethodName);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;now that we have the WCF attributes, build the REST url based on the url template and the
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;method being called with it's parameters&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var urlTemplate &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet &lt;/span&gt;&lt;span style="color: #000000"&gt;!=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; webGet.UriTemplate : webInvoke.UriTemplate;
        var urlWithParams &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; GetUrlWithParams(urlTemplate, func);
        var url &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; ServiceUrl &lt;/span&gt;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt; urlWithParams;

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Do the web requests&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; output;
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (httpMethod &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;GET&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            output &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpGet(url);
               
        }
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;span style="color: #000000"&gt;
        {
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;need to move the query string params to the http parameters&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var parts &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; url.Split(&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #800000"&gt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            output &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpInvoke(parts[&lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;], httpMethod, parts.Length &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; parts[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;] : &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.Empty);
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;change the response to json and then dynamic&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        var stringReader &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; StringReader(output);
        var jReader &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonTextReader(stringReader);
        var jsonSerializer &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; JsonSerializer();
        var json &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; jsonSerializer.Deserialize&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;Dictionary&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;(jReader);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; json.AsDynamic();
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Updates the url template with the correct parameters
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="template"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="expression"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; GetUrlWithParams(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; template, Func&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;T, dynamic&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; expression)
    {
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;parse the template, get the matches&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var m &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; Regex.Matches(template, &lt;/span&gt;&lt;span style="color: #800000"&gt;@"&lt;/span&gt;&lt;span style="color: #800000"&gt;\{(.*?)\}&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;).Cast&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;Match&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;())
        {
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (m.Groups.Count &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800080"&gt;2&lt;/span&gt;&lt;span style="color: #000000"&gt;)
            {
                var m1 &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; m;
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;find the fields based on the expression(Func&amp;lt;T&amp;gt;), get their values and replace the tokens in the url template&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                var field &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; expression.Target.GetType().GetFields().Where(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; x.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; m1.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value).Single();
                template &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; template.Replace(m.Groups[&lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;].Value, field.GetValue(expression.Target).ToString());
            }
        }
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; template;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Do an Http GET
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="uri"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpGet(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; uri)
    {
        var webClient &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebClient();
        var data &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webClient.DownloadString(uri);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; data;
    }

    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; Do an Http POST/PUT/etc...
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="uri"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="method"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;param name="parameters"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
    &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; HttpInvoke(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; uri, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; method, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; parameters)
    {
        var webClient &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; WebClient();
        var data &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; webClient.UploadString(uri, method, parameters);
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; data;
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 11 Feb 2011 22:26:00 Z</pubDate>
      <a10:updated>2011-02-11T22:26:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1296</guid>
      <link>https://shazwazza.com/post/vertically-centring-multi-line-text-with-css/</link>
      <category>Web Development</category>
      <title>Vertically centring multi-line text with CSS</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;So you have a fixed height container, and you want your text to be vertically centred, whether it be one line or many lines of text.&amp;nbsp; In every browser.&amp;nbsp;&amp;nbsp; This is possible, as I just learned from &lt;a href="http://www.boutell.com/newfaq/creating/centervertically.html" target="_blank"&gt;this article&lt;/a&gt;.  &lt;p&gt;For all modern browsers, and IE8 and IE9, the method uses &lt;code&gt;display:table-cell&lt;/code&gt;.&amp;nbsp; For IE6 and IE7, the method described in the above article is used, which takes advantage of a &lt;code&gt;height&lt;/code&gt; bug.&lt;/p&gt; &lt;p&gt;You HTML looks like this:&lt;/p&gt;&lt;pre&gt;&amp;lt;div id="container"&amp;gt;
	&amp;lt;div id="wrapper"&amp;gt;
		&amp;lt;div id="content"&amp;gt;
			Stuff and things
		&amp;lt;/div&amp;gt;
	&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
&lt;p&gt;Your CSS looks like this:&lt;/p&gt;&lt;pre&gt;#container
{
	width:200px;
	height:50px;
	background-color:red;
	*position:relative;
}
#wrapper
{
	background-color:green;
	height:inherit;
	display:table-cell;
	vertical-align:middle;
	*height:auto;
	*position:absolute;
	*top:50%;
}
#content
{
	background-color:blue;
	*position:relative;
	*top:-50%;
	color:white;
}&lt;/pre&gt;
&lt;p&gt;The result looks like this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IE6&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/image.axd?picture=ie6-multiline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie6-multiline" border="0" alt="ie6-multiline" src="/image.axd?picture=ie6-multiline_thumb.png" width="219" height="83"&gt;&lt;/a&gt;&lt;a href="/image.axd?picture=ie6-singleline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie6-singleline" border="0" alt="ie6-singleline" src="/image.axd?picture=ie6-singleline_thumb.png" width="216" height="66"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IE7&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/image.axd?picture=ie7-multiline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie7-multiline" border="0" alt="ie7-multiline" src="/image.axd?picture=ie7-multiline_thumb.png" width="220" height="87"&gt;&lt;/a&gt;&lt;a href="/image.axd?picture=ie7-singleline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie7-singleline" border="0" alt="ie7-singleline" src="/image.axd?picture=ie7-singleline_thumb.png" width="217" height="66"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IE8&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/image.axd?picture=ie8-multiline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie8-multiline" border="0" alt="ie8-multiline" src="/image.axd?picture=ie8-multiline_thumb.png" width="222" height="68"&gt;&lt;/a&gt;&lt;a href="/image.axd?picture=ie8-singleline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie8-singleline" border="0" alt="ie8-singleline" src="/image.axd?picture=ie8-singleline_thumb.png" width="216" height="62"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IE9 Beta&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/image.axd?picture=ie9-multiline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie9-multiline" border="0" alt="ie9-multiline" src="/image.axd?picture=ie9-multiline_thumb.png" width="217" height="65"&gt;&lt;/a&gt;&lt;a href="/image.axd?picture=ie9-singleline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ie9-singleline" border="0" alt="ie9-singleline" src="/image.axd?picture=ie9-singleline_thumb.png" width="217" height="62"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Google Chrome&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/image.axd?picture=chrome-multiline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="chrome-multiline" border="0" alt="chrome-multiline" src="/image.axd?picture=chrome-multiline_thumb.png" width="216" height="67"&gt;&lt;/a&gt;&lt;a href="/image.axd?picture=chrome-singleline.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="chrome-singleline" border="0" alt="chrome-singleline" src="/image.axd?picture=chrome-singleline_thumb.png" width="217" height="67"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And of course, this is not just constrained to text.&amp;nbsp; &lt;code&gt;#content&lt;/code&gt; can be any variable-height block...&lt;/p&gt;</description>
      <pubDate>Thu, 18 Nov 2010 22:34:00 Z</pubDate>
      <a10:updated>2010-11-18T22:34:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1200</guid>
      <link>https://shazwazza.com/post/mixing-percentage-and-pixel-dimensions-in-css/</link>
      <category>Web Development</category>
      <title>Mixing percentage and pixel dimensions in CSS</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;Okay, so there is no trick to actually compute these dimensions (&lt;a href="http://msdn.microsoft.com/en-us/library/ms537634(VS.85).aspx"&gt;except in IE&lt;/a&gt;).&amp;nbsp; However, there is a little known technique in CSS which is supported in all browsers and solves most of these problems.  &lt;p&gt;Most usually, the desire to mix the two units comes about when one wants column A to fill the page &lt;em&gt;except&lt;/em&gt; for column B, which has a fixed pixel width.&amp;nbsp; The problem becomes more formidable when doing the same thing with rows, the reason being that while a block element will naturally expand to fit its container’s width, there is no parallel behaviour for height.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The misconception: &lt;/strong&gt;when positioning absolutely, two positions and two dimensions should be set.&amp;nbsp; I.E., &lt;em&gt;top, left, width, height.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The truth:&lt;/strong&gt; many combinations of position and dimension may be used.&amp;nbsp; Want to fill the page except for a 300px bar on the right?&amp;nbsp; Sure:&lt;/p&gt;&lt;pre&gt;html, body {
    height: 100%;
}

#content {
    position:absolute;
    top:0;
    right:300px;
    bottom:0;
    left:0;
}&lt;/pre&gt;
&lt;p&gt;Turns out you don't even need to specify dimensions, they can be implied by positions. This is actually really old-school stuff which is supported by IE of old, back when it couldn't deal with flowing layouts very well. If you want to start combining percentages with pixels, just mess around with this technique and some containers.&lt;/p&gt;</description>
      <pubDate>Tue, 09 Nov 2010 21:32:00 Z</pubDate>
      <a10:updated>2010-11-09T21:32:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1285</guid>
      <link>https://shazwazza.com/post/injecting-javascript-into-other-frames/</link>
      <category>Web Development</category>
      <title>Injecting JavaScript into other frames</title>
      <description>&lt;p&gt;The beautiful part of JavaScript is that it is ridiculously flexible and lets you do things that ‘probably’ shouldn’t be done. Here’s a good example of that. &lt;/p&gt;  &lt;p&gt;During &lt;a title="uComponents" href="http://uComponents.codeplex.com" target="_blank"&gt;uComponents&lt;/a&gt; development I stumbled upon a situation where I needed to attach a JavaScript method to the top-level frame from inside of an iframe. Well in fact it turns out this is quite easy, you can do something like this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b2b7db1f-4968-4424-90e5-74aef73189b9" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 538px; height: 20px;background-color:White;overflow: hidden;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;window.top.doThis &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;function&lt;/span&gt;&lt;span style="color: #000000;"&gt;() { alert(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;woot!&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;); }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;However, since we’re attaching the ‘doThis’ method to the main frame from an inner iframe, when the inner iframenavigates to another page, this function will no longer exist on the main frame… So this clearly isn’t going to work if we want to be able to call the ‘doThis’ method from the inner frame no matter when and where it navigates to… Conundrum! &lt;/p&gt;

&lt;p&gt;So the next possibility is to try to just inject a script block into the main frame from the iframewhich actually does work in Firefox and Chrome but fails in Internet Explorer and Safari. &lt;em&gt;(This snippet of code requires that you have jQuery loaded in the main frame)&lt;/em&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8de3a41f-d280-4f2c-a969-b8f027f818be" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 458px; height: 88px;background-color:White;overflow: hidden;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&lt;span style="color: #000000;"&gt; js &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;function doThis() { alert('woot!'); }&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&lt;span style="color: #000000;"&gt; injectScript &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        .attr(&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;type&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;text/javascript&lt;/span&gt;&lt;span style="color: #000000;"&gt;'&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        .html(js);
window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;head&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;).append(injectScript);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;In the above, we’re creating a string function, creating a &amp;lt;script&amp;gt; block with jQuery, appending the string function to the script block and then appending this script block to the &amp;lt;head&amp;gt; element of the main frame. But as i said before, Firefox and Chrome are ok with this but Internet Explorer and Safari will throw JavaScript exceptions such as: &lt;strong&gt;Unexpected call to method or property access&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ok, so unless you don’t want to be cross browser, this isn’t going to work. It took me a while to figure out that you can do this, but this does work. Yes it looks pretty dodgy, and it probably is. In reality, attempting to do something like this is pretty dodgy to begin with. So here it is &lt;em&gt;(this works in Internet Explorer 8 (probably earlier ones too), Firefox 3.6 (probably earlier ones too) and Chrome 5 (probably earlier ones too) and i didn’t get around to testing Safari but I am assuming it works)&lt;/em&gt;:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ef246e3a-c9e9-4a00-953a-ce41cf099551" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 635px; height: 210px;background-color:White;overflow: hidden;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&lt;span style="color: #000000;"&gt; iframe &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;#dummyIFrame&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (iframe.length &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&lt;span style="color: #000000;"&gt; html &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;script type='text/javascript'&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;this.window.doThis = function() { alert('woot'); };&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
                &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/script&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    iframe &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;iframe id='dummyIFrame'&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
                .append(html)
                .hide()
                .css(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;width&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;0px&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
                .css(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;height&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;0px&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;body&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;).append(iframe);
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;So i guess this requires a bit of explanation. All browsers seem to let you create iframes dynamically which also means that you can put whatever content into the iframes while it’s being created, including script blocks. Here’s what we’re doing:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;checking if our ‘dummy’ iframe already exists (since we don’t want to create multiple dummy iframes since we only need one), if it doesn't: &lt;/li&gt;

  &lt;li&gt;create an html text block including the script that will attach the ‘doThis’ method to the ‘this.window’ object &lt;em&gt;(which for some reason will be referring to the window.top object)&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;next we create an iframe element and append the html text block, and then make sure the iframe is completely hidden &lt;/li&gt;

  &lt;li&gt;finally, we append the iframe to the main window’s body element &lt;/li&gt;
&lt;/ul&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a4477b2f-25ba-41cc-a332-dce82c870058" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 497px; height: 20px;background-color:White;overflow: hidden;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;window.top.doThis();&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Nice! So this pretty much means that you can create code from an inner frame and attach it to a different frame, then have that code run in the context of the main frame with the main frame’s objects and script set.&lt;/p&gt;

&lt;p&gt;One last thing that i found out you can do, though i wouldn’t recommend it because i think it might start filling up your memory. But this is also possible:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:54c85647-7adb-41e2-a3e5-47d4cb1380bb" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 635px; height: 129px;background-color:White;overflow: hidden;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&lt;span style="color: #000000;"&gt; html &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;script type='text/javascript'&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
            &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;this.window.doThis = function() { alert('woot'); };&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
            &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;this.window.doThis();&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
            &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/script&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
iframe &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; window.top.$(&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;iframe id='dummyIFrame'&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            .append(html);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;All that is happening here is that I’m attaching the ‘doThis’ method to the main frame’s window object, calling it directly after and then creating an iframe in memory with this script block. The funny part is that the method executes straight away and I haven’t attached the iframe to the DOM anywhere! &lt;/p&gt;</description>
      <pubDate>Thu, 09 Sep 2010 20:44:00 Z</pubDate>
      <a10:updated>2010-09-09T20:44:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1206</guid>
      <link>https://shazwazza.com/post/virtualpathutility-fixed-in-net-4/</link>
      <category>Web Development</category>
      <title>VirtualPathUtility Fixed in .Net 4</title>
      <description>&lt;p&gt;If you are not already aware there’s an issue with the &lt;a title="VirtualPathUtility" href="http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx" target="_blank"&gt;VirtualPathUtility&lt;/a&gt; object in the .Net framework. If you are trying to use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.isabsolute.aspx" target="_blank"&gt;IsAbsolute&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.toabsolute.aspx" target="_blank"&gt;ToAbsolute&lt;/a&gt; methods with a path that contains a query string, you’ll get an exception thrown. A quick Google search on this issue will show you a ton of posts and ‘work arounds’ for this problem and I’ve implemented a custom work around for this in the &lt;a title="ClientDependency" href="http://clientdependency.codeplex.com" target="_blank"&gt;ClientDependency&lt;/a&gt; framework as well. In fact, I only noticed this problem when testing the ClientDependency framework with .Net 3.5 and trying to register embedded resources through it (as see in this blog post &lt;a title="Registering web resources via ClientDependency" href="http://www.farmcode.org/post/2010/08/03/Adding-embedded-resource-with-ClientDependency.aspx" target="_blank"&gt;here&lt;/a&gt;). Since I’d been developing the latest builds of ClientDependency in .Net 4, i had no problems at all but as soon as I changed the build to .Net 3.5, the exceptions began to show since web resource URLs have query strings. &lt;/p&gt;  &lt;p&gt;So it the good new is that it appears as though Microsoft have finally fixed this bug in .Net 4! The bad news is that you’ll still have to use your ‘work arounds’ if you are targeting framework versions below 4.&lt;/p&gt;</description>
      <pubDate>Mon, 30 Aug 2010 22:13:00 Z</pubDate>
      <a10:updated>2010-08-30T22:13:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1235</guid>
      <link>https://shazwazza.com/post/how-to-embed-asmx-web-services-in-a-dll/</link>
      <category>Umbraco</category>
      <category>Web Development</category>
      <title>How to embed ASMX web services in a DLL</title>
      <description>&lt;p&gt;The development of &lt;a href="http://uComponents.codeplex.com" target="_blank"&gt;uComponents&lt;/a&gt; for &lt;a href="http://umbraco.org" target="_blank"&gt;Umbraco&lt;/a&gt; is still ongoing but is probably close to a v1.0 release. During the development of the Multi-node tree picker data type, I’ve spotted a bug in the Umbraco core in regards to the ASMX web service the existing tree uses to render out it’s initial node. The bug basically won’t allow the tree to render an individual tree, only a full app. Since the Multi-node tree picker requires rendering of an individual tree, i needed to fix this. To do this was a bit of a work around but i had to override the tree’s ASMX web service with my own (I’ll write a post about this one later). It then occurred to me that I’ve never embedded an ASMX web service into a DLL without a physical file and I didn’t want to go updating the web.config with custom handlers, etc… So I came up with a way to embed ASMX web services in your DLL without modifying web.config’s, etc… here’s how:&lt;/p&gt;  &lt;p&gt;First, create your web service as a standard class and add all of the functionality that your require your web service to have:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb.png" width="436" height="155" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In your class library project, create a new text file but give it the extension .asmx:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_1.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_1.png" width="440" height="255" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Set the file to be a non compiled file:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_2.png" width="352" height="82" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Create an resource file:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_3.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_3.png" width="439" height="251" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Double click on your new RESX file, then drag your ASMX file into the editor to add the file as a property of your RESX file:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_4.png" width="391" height="177" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Double click your ASMX file to edit it and set it to inherit from your custom web service class:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_5.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_5.png" width="425" height="48" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, we need to register this ASMX web service into the web application that it’s being deployed to. This is easily done by just checking if the ASMX file exists where it needs to be before it needs to be called and if it doesn’t, we just create the file. In the case of uComponents and the multi-node tree picker requires the ASMX web service for the CustomTreeControl, so we’ll do this check in the CustomTreeControl’s static constructor:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8465a3ee-ce08-442b-998a-9b6f3398db04" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 672px; height: 430px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; CustomTreeControl()
{
    var filePath &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpContext.Current
                    .Server.MapPath(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;~/CustomTreeService.asmx&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
            
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;check if it exists&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;File.Exists(filePath))
    {
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;lock the thread&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;lock&lt;/span&gt;&lt;span style="color: #000000;"&gt; (m_Locker)
        {
            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;double check locking....&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;File.Exists(filePath))
            {
                &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;now create our new local web service&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var sw &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; StreamWriter(File.Create(filePath)))
                {
                    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;write the contents of our embedded resource to the file&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                    sw.Write(CustomTreeServiceResource.CustomTreeService);
                }
            }
        }
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;That’s it! &lt;/p&gt;</description>
      <pubDate>Fri, 13 Aug 2010 00:01:00 Z</pubDate>
      <a10:updated>2010-08-13T00:01:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1260</guid>
      <link>https://shazwazza.com/post/net-open-source-blog-comparison/</link>
      <category>Umbraco</category>
      <category>Web Development</category>
      <title>.Net Open Source Blog Comparison</title>
      <description>&lt;p&gt;When I started creating this site this morning, the first thing i though to myself was that I wasn’t going to use &lt;a title="BlogEngine.Net" href="http://www.dotnetblogengine.net/" target="_blank"&gt;BlogEngine.Net&lt;/a&gt; for yet another blog. So I went on a mission to find some other open source blogging systems written in ASP.Net. One of the things I needed the codebase to support was to work seamlessly with Live Writer, so after some quick research I found that the new &lt;a title="Orchard Project" href="http://www.orchardproject.net/" target="_blank"&gt;Orchard&lt;/a&gt; project support Live Writer and so does &lt;a title="SubText" href="http://subtextproject.com/" target="_blank"&gt;SubText&lt;/a&gt;. So using WebPI, I decided to give them each a quick test run.&lt;/p&gt;  &lt;p&gt;I’m sure the Umbracians out there are asking: Why wouldn’t you just use &lt;a title="Blog 4 Umbraco" href="http://our.umbraco.org/projects/collaboration/blog-4-umbraco" target="_blank"&gt;Blog 4 Umbraco&lt;/a&gt; considering I’m on the core development team. Well, i guess it’s because all I really want is a very simple blogging tool and really just wanted to try out some new tools. &lt;/p&gt;  &lt;p&gt;So I started with Orchard and was kind of excited to see what it offered since it’s newcomer to the CMS scene and has a bit of hype behind it. The admin UI was nice and is was very easy to install. Setting up a new blog was pretty straight forward as well… just click on Create Blog. It' also has a couple nice skins you can test out. So far so good (even though this is still a alpha/beta product). So I tried plugging in Live Writer to it and it turns out that Orchard really hasn’t implemented some of the much needed features of the &lt;a title="MetaWeblog API" href="http://en.wikipedia.org/wiki/MetaWeblog" target="_blank"&gt;MetaWeblog API&lt;/a&gt; such as categories &amp;amp; tags! ouch. On a side note, i tried looking at creating new content types in Orchard, thinking it would be similar to how Umbraco works. Turns out it’s not quite the same and looks as though this functionality is in its infancy in Orchard. There was really only one data type to choose from which was text and it appeared as though i couldn’t delete a content type either. We’ll see how the project looks in a few months.&lt;/p&gt;  &lt;p&gt;Next I went on to SubText. This was also pretty easy to install (not as easy as Orchard though) but the admin UI isn’t so pretty. SubText has pretty much every feature that you could want out of a blogging system and comes with a bunch of skins as well. One thing i noticed which looked pretty cool is that you can add text filters so the SubText will automatically create links for you in your posts. So I plugged in Live Writer and everything is pretty good, except that they haven’t implemented tagging in their MetaWeblog API either.&lt;/p&gt;  &lt;p&gt;As you’ve probably noticed, I’ve settled back on BlogEngine.Net. It has better Live Writer integration than either of these other two blogging systems, the admin UI is nice enough and it’s pretty damn easy to use. I really wish they’d implement non ASPX page extensions sometime soon though!&lt;/p&gt;</description>
      <pubDate>Wed, 11 Aug 2010 11:09:00 Z</pubDate>
      <a10:updated>2010-08-11T11:09:00Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1185</guid>
      <link>https://shazwazza.com/post/and-it-begins/</link>
      <category>Umbraco</category>
      <category>Web Development</category>
      <title>And it begins</title>
      <description>&lt;p&gt;For the last couple of years I've been blogging on &lt;a title="FARMCode" href="http://farmcode.org" target="_blank"&gt;FARMCode.org&lt;/a&gt; and today decided that I was going to start up my own blog as well. This doesn't mean that I won't still be blogging on FARMCode, it just means that I have a new place to post up information that may not directly relate to &lt;a href="http://thefarmdigital.com.au" target="_blank"&gt;TheFARM&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the coming weeks I'll start posting up some new content, code snippets, some info on the up and coming Umbraco project we've been working on called &lt;a title="uComponents" href="http://uComponents.codeplex.com" target="_blank"&gt;uComponents&lt;/a&gt; and maybe some portfolio work as well.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Aug 2010 10:22:00 Z</pubDate>
      <a10:updated>2010-08-11T10:22:00Z</a10:updated>
    </item>
  </channel>
</rss>