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

<channel>
	<title>House of Metnetsky &#187; mono</title>
	<atom:link href="http://cowarthill.com/blog/index.php/tag/mono/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowarthill.com/blog</link>
	<description>Run Tortoise Run</description>
	<lastBuildDate>Fri, 26 Feb 2010 17:15:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>log4net PatternString</title>
		<link>http://cowarthill.com/blog/index.php/2009/12/23/log4net-patternstring/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/12/23/log4net-patternstring/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:30:10 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=316</guid>
		<description><![CDATA[I&#8217;ve been using log4net since it was first available for Mono and barely know anything about it. To a certain extent you&#8217;ve got to love it because of that &#8211; it just works! The project is great, but development appears somewhat stagnant. Either way, I&#8217;ve never needed to know much more than basic configuration and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a href="http://logging.apache.org/log4net/" target="_blank">log4net</a> since it was first available for <a href="http://mono-project.com/" target="_blank">Mono</a> and barely know anything about it. To a certain extent you&#8217;ve got to love it because of that &#8211; it just works! The project is great, but development appears somewhat stagnant. Either way, I&#8217;ve never needed to know much more than basic configuration and how to get it monitored even though I&#8217;ve used it for daemons, client applications, Add-ins, etc.</p>
<p>Today however, I learned something! A while ago I needed to have the <em><a href="http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html" target="_blank">RollingFileAppender</a></em> log to a varying location depending on Windows version (XP vs Vista/7). At the time I found posts showing me how to use variables within the &#8220;file&#8221; option like so:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;file value=&quot;${APPDATA}\Company\Product\logs\data.log&quot; /&gt;
</pre>
<p>Not having looked at the log4net code this is either explicitly converted to a user&#8217;s roaming folder, or it is replaced as an environment variable. This works great for applications that should log per user, but what if I needed the <a href="http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx" target="_blank">&#8220;All Users&#8221; roaming folder on Windows</a>?  Neither <em>${COMMONAPPDATA}</em> or <em>${ALLUSERAPPDATA}</em> worked for me so I needed another option that would cut it.</p>
<p><strong>Enter <a href="http://logging.apache.org/log4net/release/sdk/log4net.Util.PatternConverter.html" target="_blank">log4net.Util.PatternConverter</a></strong></p>
<p>By extending this very simple class and implementing one method we can now do variable substitution. The syntax leaves a lot to be desired, but hey &#8211; it works!</p>
<pre class="brush: xml; title: ; notranslate">
&lt;file type=&quot;log4net.Util.PatternString&quot;&gt;
  &lt;converter&gt;
    &lt;name value=&quot;sub&quot; /&gt;
    &lt;type value=&quot;SpecialFolderPathConverter, Assembly&quot; /&gt;
  &lt;/converter&gt;
  &lt;conversionPattern value=&quot;%sub{ApplicationData}\Company\Product\logs\addin.log&quot; /&gt;
&lt;/file&gt;
</pre>
<p>With this setup now all I have to do is create the <em><strong>Assembly.SpecialFolderPathConverter</strong></em> class:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.IO;

namespace Assembly
{
    public class SpecialFolderPathConverter : log4net.Util.PatternConverter
    {
        protected override void Convert(TextWriter writer, object state)
        {
            // Option gets set to contents of {}
            if (String.IsNullOrEmpty(this.Option))
                return;

            try {
                Environment.SpecialFolder sf = (Environment.SpecialFolder)
                    Enum.Parse(typeof(Environment.SpecialFolder), this.Option, true);

                writer.Write(Environment.GetFolderPath(sf));
            } catch (Exception) {
            }
        }
    }
}
</pre>
<p>As you can probably see this class takes any string within the <strong>{}</strong> brackets following <strong>%sub</strong> and replaces it using <strong><a href="http://msdn.microsoft.com/en-us/library/14tx8hby.aspx" target="_blank">Environment.GetFolderPath</a></strong> which is quite simple, and helpful.</p>
<p>Thank you <a href="http://logging.apache.org/log4net/" target="_blank">log4net</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/12/23/log4net-patternstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mono Embedded &#8211; Your Own .NET Plugin</title>
		<link>http://cowarthill.com/blog/index.php/2009/08/27/mono-embedded-one/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/08/27/mono-embedded-one/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 19:25:38 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=284</guid>
		<description><![CDATA[I&#8217;ve embedded either for fun or work a number of languages to see what hosting them in C is like &#8211; both for performance and ease of marshalling. So far I&#8217;m impressed with the internals of Mono, but feel like there&#8217;s a lack of proper GOTCHA&#8217;s documented anywhere. Some are spread out around the web, [...]]]></description>
			<content:encoded><![CDATA[<div>I&#8217;ve embedded either for fun or work a number of languages to see what hosting them in C is like &#8211; both for performance and ease of marshalling. So far I&#8217;m impressed with the internals of Mono, but feel like there&#8217;s a lack of proper GOTCHA&#8217;s documented anywhere. Some are spread out around the web, but most don&#8217;t directly state key points. As such, I&#8217;m going to be posted a few as I come across them.</p>
<p>WARNING: Some of this may not make complete sense without reading <a href="http://www.mono-project.com/Embedding_Mono" target="_blank">http://www.mono-project.com/Embedding_Mono</a> first.</p>
<p><strong>Creating Objects</strong><br />
Like the above docs day, all you really need to do is:</p>
<pre class="brush: cpp; title: ; notranslate">
/* get handle for class from assembly */
MonoImage *image = mono_assembly_get_image(MonoAssembly*);
MonoClass *klass = mono_class_from_name(image, &quot;Namespace&quot;, &quot;Class&quot;);

/* instantiate class and call .ctor (as they're nothing more than methods) */
MonoObject *obj = mono_object_new(MonoDomain*, klass);
mono_runtime_init(obj); /* call .ctor w/out args */
</pre>
<p><strong>Reusing Objects</strong><br />
If you need to reuse this new MonoObject instance EVER I would highly suggest calling <strong>mono_gchandle_new(MonoObject*, pinned)</strong> otherwise the GC will get to it before you want it again &#8211; I GUARANTEE THIS. The reason this happens is because objects created from your C code have no real &#8220;SCOPE&#8221;, so they&#8217;re collected quickly just like they were local variables in a method. Read the following link, it&#8217;s helpful:</p>
<p><a href="http://www.go-mono.org/docs/index.aspx?link=xhtml%3Adeploy%2Fmono-api-gchandle.html" target="_blank">http://www.go-mono.org/docs/index.aspx?link=xhtml%3Adeploy%2Fmono-api-gchandle.html</a></p>
<p>As a side note, you can actually call methods on objects that have been collected as long as the methods usage of `this` is only limited to `this.GetType()` (or nearly static). Try it, you&#8217;ll get some really funny results!</p>
<p><strong>Finding Methods</strong><br />
The MonoClass* struct contains (or points to) lots of lists of data. The methods of a class are essentially one big long list and each method (every single variation) is a different instance of MonoMethod*. Take a look at the following C# class:</p>
<pre class="brush: csharp; title: ; notranslate">
namespace Test
{
	public class Foo
	{
		public Foo()
		{
		}

		public void Log(int num)
		{
		}

		public void Log(string msg)
		{
		}
	}
}
</pre>
<p>NOTICE: The Log method is overloaded!</p>
<p>One way to call Foo.Log(?) is like so:</p>
<pre class="brush: cpp; title: ; notranslate">
/* get handle for class from assembly */
MonoImage *image = mono_assembly_get_image(MonoAssembly*);
MonoClass *klass = mono_class_from_name(image, &quot;Namespace&quot;, &quot;Class&quot;);

/* instantiate class and call .ctor (as they're nothing more than methods) */
MonoObject *obj = mono_object_new(MonoDomain*, klass);
mono_runtime_init(obj); /* call .ctor w/out args */

/* Get Log method from MonoClass that takes 1 argument
MonoMethod *m = mono_class_get_method_from_name(klass, &quot;Log&quot;, 1);

/* create single argument */
void *args[1] = { mono_string_new(MonoDomain*, &quot;Hello World&quot;); };

/* last argument is to capture exceptions */
mono_runtime_invoke(m, obj, args, NULL);
</pre>
<p>Any guess what might have just happened? Well, we just passed a string to the Log(int) method because <strong>mono_class_get_method_from_name</strong> returns the first one it finds, and not the one based on the type. To fix this you could write your own method which loops through MonoMethod*&#8217;s and find the correct signature or use the helper methods:</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;mono/metadata/debug-helpers.h&gt;

MonoMethodDesc *d = mono_method_desc_new(&quot;:Log(int)&quot;, FALSE);
MonoMethod *m = mono_method_desc_search_in_class(d, klass);
mono_method_desc_free(d);
</pre>
<p>The <strong>mono_method_desc_*</strong> functions are extremely helpful and remove some of the crazy and changing internals &#8211; so I&#8217;d suggest using them for much of your calls.</p>
<p>Here are some further examples of the string descriptor for mono_method_desc_new:</p>
<ul>
<li>Namespace.Test:Log(string) -&gt; find Log method in Test class (requires second argument to be TRUE)</li>
<li>:Log(string) -&gt; find Log method which takes a string</li>
<li>:Log(XmlReader) -&gt; find Log method which takes an XmlReader</li>
<li>:Log(My.Namespace.XmlReader) -&gt; find the method which takes YOUR funky XmlReader</li>
</ul>
<p>As you can see it covers the gamut &#8211; so thank you to whomever created that extremely useful class/struct/function-set.</p>
</div>
<div>~ Till next time!</div>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/08/27/mono-embedded-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mono + SCGI</title>
		<link>http://cowarthill.com/blog/index.php/2009/06/10/mono-scgi/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/06/10/mono-scgi/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 21:46:13 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[scgi]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=267</guid>
		<description><![CDATA[Over a year ago I started a SCGI daemon for Mono (scgi-mono-server). For those who don&#8217;t know, the &#8220;SCGI protocol is a replacement for the Common Gateway Interface (CGI) protocol. It is a standard for applications to interface with HTTP servers. It is similar to FastCGI but is designed to be easier to implement&#8221;.I stopped [...]]]></description>
			<content:encoded><![CDATA[<div>Over a year ago I started a <a title="http://python.ca/scgi/protocol.txt" href="http://" target="_blank">SCGI</a> daemon for <a title="http://www.mono-project.com/" href="http://" target="_blank">Mono</a> (scgi-mono-server). For those who don&#8217;t know, the <a href="http://www.mems-exchange.org/software/scgi" target="_blank">&#8220;SCGI protocol is a replacement for the Common Gateway Interface (CGI) protocol. It is a standard for applications to interface with HTTP servers. It is similar to FastCGI but is designed to be easier to implement&#8221;</a>.I stopped all development shortly after I started because <a href="http://www.lighttpd.net/" target="_blank">lighttpd</a> (the web server I cared about) required a very small patch to be useful. Having supplied the <a href="http://redmine.lighttpd.net/projects/lighttpd/repository/revisions/2223/diff/branches/lighttpd-1.4.x/src/mod_scgi.c" target="_blank">patch</a> I waited around for it to be rolled into a release (<a href="http://www.lighttpd.net/2008/9/30/1-4-20-Otherwise-the-terrorists-win" target="_blank">1.4.20</a>) and then waited for it to be picked up by <a href="http://packages.ubuntu.com/search?keywords=lighttpd&amp;searchon=names&amp;suite=jaunty&amp;section=all" target="_blank">Ubuntu</a>, but it still hasn&#8217;t. So what brought it back to life? A silly bug: <a href="https://bugs.launchpad.net/ubuntu/+source/mod-mono/+bug/227781" target="_blank">https://bugs.launchpad.net/ubuntu/+source/mod-mono/+bug/227781</a>.</div>
<p>Some of our servers need both PHP5 &amp; Mono to run side-by-side so one of our guys listed the options:</p>
<div>
<ul>
<li dir="ltr">Pull mono &amp; <a href="http://www.mono-project.com/Mod_mono" target="_blank">mod-mono</a> 1.9.1 back-ports from a <a href="https://launchpad.net/ubuntu/+ppas" target="_blank">PPA</a> (<a href="http://ppa.launchpad.net/directhex/ppa/ubuntu">http://ppa.launchpad.net/directhex/ppa/ubuntu</a>)</li>
<li dir="ltr">Patch and rebuild <a href="http://www.mono-project.com/Mod_mono" target="_blank">mod-mono</a> 1.2.5 ourselves</li>
<li dir="ltr">Switch to <a href="http://en.wikipedia.org/wiki/FastCGI" target="_blank">FastCGI</a> for PHP5, so we can use apache2-mpm-worker for mod-mono-server</li>
<li dir="ltr">Switch from Apache to lighttpd (and rebuild with our patch)</li>
<li dir="ltr">Switch from Apache + mod-mono to Apache + FastCGI or SCGI (requires patching lighttpd)</li>
<li dir="ltr">Switch from Apache + mod-mono to Apache + mod_proxy for <a href="http://www.mono-project.com/ASP.NET#ASP.NET_hosting_with_XSP" target="_blank">XSP</a></li>
</ul>
<p>Given our growing dislike for Apache &amp; appreciation for lighttpd we decided on a phased approach:</p>
<ul>
<li dir="ltr">Switch PHP5 to Apache + <a href="http://en.wikipedia.org/wiki/FastCGI" target="_blank">FastCGI</a></li>
<li dir="ltr">Switch Mono to Apache + SCGI</li>
<li dir="ltr">Run for a while (make sure everything works well)</li>
<li dir="ltr">Switch from Apache to lighttpd</li>
</ul>
<p>We choose SCGI for Mono over <a href="http://en.wikipedia.org/wiki/FastCGI" target="_blank">FastCGI</a> because:</p>
<ul>
<li dir="ltr">FastCGI requires a lot of chatter between the client &amp; server</li>
<li dir="ltr">Great incentive to finally give the server to Mono</li>
</ul>
<p>With our approach in mind I started working on scgi-mono-server again, but with a new target &#8211; Apache. This shouldn&#8217;t have been very hard given that mod_scgi is provided by <strong>Quixote</strong>, but there were a few stumbling blocks. Apache, for whatever reason, has decided to break the SCGI specification (in my eyes).  As stated in the <strong>Protocol</strong>: &#8220;[t]he format of the response is not specified&#8221; &#8211; meaning whatever the SCGI server sends back to the client (Apache, lighttpd, etc) should go back to the originator (web browser). Apache however, does not honor this. </p>
<p>The first line of any HTTP response (AFAIK) should be it&#8217;s <em>Status Line</em>:</p>
<p><strong>HTTP/1.1 200 OK</strong>.</p>
<p>Apache&#8217;s mod_scgi requires that all lines until the body have a colon so that is can parse the header and update it&#8217;s internal data model. Therefore, our first line now has to be replaced by:</p>
<p><strong>Status: 200 OK</strong></p>
<p>Thankfully the <strong>Example</strong> section of the protocol showed me how to <strong><em>fix the issue</em></strong>. I&#8217;m tempted to patch Apache/mod_scgi but I&#8217;m a bit worried about how many SCGI servers require this broken code. So for now I&#8217;ve added a configuration option that can be put inside ASP.NET&#8217;s web.config to control the <strong><em>hack</em></strong>:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;appSettings&gt;
	&lt;add key=&quot;MonoServerApacheStatus&quot; value=&quot;true&quot; /&gt;
&lt;/appSettings&gt;
</pre>
<p>I&#8217;ll be submitting the server back to Mono with some documentation after it goes through some solid developer testing on our side &#8211; so I hope someone enjoys!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/06/10/mono-scgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Tutorial: Comments</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/27/programming-tutorial-comments/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/27/programming-tutorial-comments/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 23:38:30 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[humor]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=260</guid>
		<description><![CDATA[Comments are only effective if they threaten abuse. Discovered by Noah Massey.]]></description>
			<content:encoded><![CDATA[<p>Comments are only effective if they threaten abuse.</p>
<pre class="brush: csharp; title: ; notranslate">
/**
 * Note: The following array MUST be sorted in order for BinarySearch to work. I have taken the liberty of
 * creating it sorted to avoid this operation. If you really feel the need to break the manual sorting,
 * uncomment the array.sort below this, and then go out and stand in front of traffic. thank you.
 */

string[] allowed_items  = {&quot;activate&quot;, &quot;restore&quot;, &quot;save&quot;, &quot;update-relay&quot;, &quot;verify-mount&quot;, &quot;verify-restore&quot;}; 

//Array.Sort(allowed_items);

if (Array.BinarySearch(allowed_items, list) &gt;= 0) {
	/* -.v.- */
}
</pre>
<p>Discovered by Noah Massey.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/27/programming-tutorial-comments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful Web Services in .NET (Part 2)</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 01:05:10 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=193</guid>
		<description><![CDATA[Now that I&#8217;ve finally got a bit of time I figured I should explain how we updated our old RESTful framework to be nice and slick. Instead of one method per class which handles all sub method variations we now have something much prettier and easier to maintain. The UserService found in the previous post [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve finally got a bit of time I figured I should explain how we updated our old RESTful framework to be nice and slick. Instead of one method per class which handles all sub method variations we now have something much prettier and easier to maintain. The UserService found in the previous post can now look like:</p>
<pre class="brush: csharp; title: ; notranslate">
public class UserService : RestService
{
	[RestMethod(&quot;GET&quot;, &quot;^$&quot;)]
	public IList RetrieveAll()
	{
	}

	[RestMethod(&quot;GET&quot;, @&quot;^(?&lt;id&gt;[0-9]+)$&quot;)]
	public User Retrieve(int id)
	{
	}

	[RestMethod(&quot;HEAD&quot;, @&quot;^(?&lt;name&gt;\w+)$&quot;)]
	public User Exists(string name)
	{
	}

	[RestMethod(&quot;PUT&quot;, &quot;^$&quot;, typeof(User))]
	public User Create(User user)
	{
	}

	[RestMethod(&quot;POST&quot;, &quot;^$&quot;, typeof(User))]
	public bool Update(User user)
	{
	}

	[RestMethod(&quot;DELETE&quot;, @&quot;^(?&lt;id&gt;[0-9]*)$&quot;)]
	public bool Delete(int id)
	{
	}
}
</pre>
<p>As you can see there are a lot of differences:</p>
<ul>
<li>Heavy influence from newer APIs</li>
<li>Methods can return any Type</li>
<li>Methods can take 0 or more arguments of any Type</li>
<li>RestMethodAttribute has been added to match URLs against methods</li>
</ul>
<p>Most of the code above should be self-explanatory, but in case it&#8217;s not here&#8217;s some help.</p>
<p>The method decorator <b>[RestMethod("GET", "^$")]</b> tells the framework that any HTTP GET requests against the <b>UserService</b> class without any trailing arguments (<b>http://localhost/user.r</b>) should be handled by <b>RetrieveAll()</b>.</p>
<p>The method decorator <b>[RestMethod("GET", @"^(?&lt;id&gt;[0-9]+)$&#8221;)]</b> tells the framework that any HTTP GET request against the <b>UserService</b> class which ends with a number only (<b>http://localhost/user.r/1</b>, <b>http://localhost/user.r/5000</b>, etc) should be handled by <b>Retrieve(int id)</b>. The cool thing here is that the regex group <id> will provide a match to the argument name so our function will be called with whatever number the URL ends with. So calling <b>http://localhost/user.r/25</b> where the following is the executed method will result in?</p>
<pre class="brush: csharp; title: ; notranslate">
[RestMethod(&quot;GET&quot;, @&quot;^(?&lt;id&gt;[0-9]+)$&quot;)]
public bool Retrieve(int id)
{
	return id == 25;
}
</pre>
<p><b>TRUE</b>!! Pretty simple, and very cool. Another nice feature is when the HTTP request contains a body like a POST or PUT would. So POSTing to <b>http://localhost/user.r</b> with a body of:</p>
<pre class="brush: jscript; title: ; notranslate">
{'user': {
	'first_name': 'Matthew',
	'last_name': 'Metnetsky'
}}
</pre>
<p>would call the method decorated with <b>[RestMethod("POST", "^$", typeof(User))]</b> and it&#8217;s <b>user</b> argument would actually be set assuming the class implemented a JSON serializer. All of the URL/body mappers are definable by implementing <a href="http://msdn.microsoft.com/en-us/library/system.reflection.binder.aspx" target="_blank">Binder</a> and listing them in the web.config against a Content-Type. By default there are Binders for form variables, JSON, XML, raw streams, and a few more.</p>
<p>So why might I be discussing the external API of a closed library? To PUSH innovation in the current public ones. By taking these ideas and creating an &#8220;open&#8221; version I&#8217;m sure I&#8217;d be breaking my contract so I&#8217;ve got two options:</p>
<ol>
<li>Expose our public API to entice and PUSH others to innovate</li>
<li>Get a lot of feedback which might enable me to get our library opened</li>
</ol>
<p>So&#8230;. let me know&#8230;. <img src='http://cowarthill.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful Web Services in .NET (Part 1)</title>
		<link>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:51:22 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=178</guid>
		<description><![CDATA[A couple of years ago I tossed together a RESTful IHttpHandlerFactory for .NET. It wasn&#8217;t amazing, but it was being used long before Microsoft et all decided to create there own. Instead of extending Page you extended RestService and implemented whatever methods you wanted. As you can probably guess the request&#8217;s HTTP Method was transformed [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago I tossed together a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> <a href="http://msdn.microsoft.com/en-us/library/system.web.ihttphandlerfactory.aspx" target="_blank">IHttpHandlerFactory</a> for <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET</a>. It wasn&#8217;t amazing, but it was being used long before Microsoft et all decided to create there own. Instead of extending <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx" target="_blank">Page</a> you extended RestService and implemented whatever methods you wanted.</p>
<pre class="brush: csharp; title: ; notranslate">
public interface IRestService
{
	void Head(RestEventArgs args);
	object Get(RestEventArgs args);
	object Put(RestEventArgs args);
	object Post(RestEventArgs args);
}
</pre>
<p>As you can probably guess the request&#8217;s <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" target="_blank">HTTP Method</a> was transformed into the function name and executed accordingly. If the method wasn&#8217;t &#8220;known&#8221; we would then use reflection to find the method for execution. Not to fansy as you can see, but it worked.</p>
<p>If a request was made for <b>http://localhost/app/users.r</b> the <a href="http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx" target="_blank">IHttpHandler</a> <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" target="_blank">factory</a> would load users.r and it would tell us what <a href="http://msdn.microsoft.com/en-us/library/0b0thckt.aspx" target="_blank">class</a> you assigned to it (much like <a href="http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx" target="_blank">@Page</a> CodeBehind/Inherits in .aspx). From there we would instantiate the <a href="http://msdn.microsoft.com/en-us/library/0b0thckt.aspx" target="_blank">class</a> and call the appropriate <a href="http://msdn.microsoft.com/en-us/library/ms173114.aspx" target="_blank">method</a>. Whatever you returned would be marshalled for wire transfer based on it&#8217;s type.</p>
<pre class="brush: csharp; title: ; notranslate">
	class User : IXmlSerializable
	{
		/* ... */
	}

	public class UserService : RestService
	{
		public object Get(RestEventArgs args);
		{
			return new User(5, &quot;Matthew&quot;, &quot;Metnetsky&quot;);
		}
	}
}
</pre>
<p>Calling <b>http://localhost/app/users.r</b> would execute UserService::Get(..) which would return a User instance. The results are then inspected for a few different interfaces like <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx" target="_blank">IXmlSerializable</a>, IJsonSerializable (one of ours), etc. Based on what is supported by the <a href="http://msdn.microsoft.com/en-us/library/ms173104.aspx" target="_blank">Type</a>, we then check if the requester supports it by interogating their <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html" target="_blank">&#8220;Accepts&#8221; header</a>. This way each requester can receive what they understand how to handle (for instance &#8211; don&#8217;t send <a href="http://www.json.org/" target="_blank">JSON</a> to <a href="http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html" target="_blank">AS3</a>). The system really worked well, and it&#8217;s simplicity made it damn fast.</p>
<p>Like all frameworks however, once it was actually in use we were able to see the pitfalls and issues. Take a look at the following URLs:</p>
<ul>
<li><b>http://localhost/app/users.r</b></li>
<li><b>http://localhost/app/users.r/1</b></li>
<li><b>http://localhost/app/users.r/matthew</b></li>
</ul>
<p>In order to handle all of the urls above the Get(RestEventArgs) method turned into one big switch/if/else conditional. Some people made it prettier than others, but the result was the same. So about 14 months ago we upgraded our framework &#8211; check back soon to see how we did it, and why I&#8217;m bringing it up now.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PMS2 &#8211; Coming Soon to an Interweb Near You</title>
		<link>http://cowarthill.com/blog/index.php/2009/02/19/pms2-coming-soon/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/02/19/pms2-coming-soon/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 23:39:28 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[pms]]></category>
		<category><![CDATA[printgroove]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=158</guid>
		<description><![CDATA[A long, long time myself and two friends were working on a college project where the entire goal as I saw it was to &#8220;create&#8221; stuff. Mono was a wee lad back in 2003, but I still preferred it to Java any day of the week. So myself, Phil Tricca, and Joseph Scaduto created a [...]]]></description>
			<content:encoded><![CDATA[<p>A long, long time myself and two friends were working on a college project  where the entire goal as I saw it was to &#8220;create&#8221; stuff. <a href="http://www.mono-project.com/Main_Page" target="_blank">Mono</a> was a wee lad back in 2003, but I still preferred it to Java any day of the week. So myself, <a href="http://media.www.dailyorange.com/media/storage/paper522/news/2003/11/19/Feature/Traditional.Remedies.Come.Up.Short.In.The.Quest.For.The.Perfect.Hangover.Cure-561669.shtml" target="_blank">Phil Tricca</a>, and Joseph Scaduto created a whole framework for creating what are now called &#8220;learning communities&#8221; in <a href="http://www.mono-project.com/Main_Page" target="_blank">Mono</a>/<a href="http://www.mono-project.com/GtkSharp" target="_blank">Gtk#</a>/<a href="http://www.mono-project.com/Mod_mono" target="_blank">mod_mono</a>.</p>
<p>One of the pieces we needed was a databases abstraction layer (we had grand ideas) so we first sought to flush out the <a href="http://msdn.microsoft.com/en-us/library/ms971512.aspx" target="_blank">ObjectSpaces</a> API for Mono. Sadly, Microsoft couldn&#8217;t get their act together so we built our own. So in honor of Microsoft we called it Phil-And-Matt Spaces, shortened to PMS. After it&#8217;s original debut it sat on the selves for quite some time until I started working for <a href="http://kmbs.konicaminolta.us/" target="_blank">Konica Minolta Business Solutions</a> and was tasked with creating <a href="http://kmbs.konicaminolta.us/content/products/models/printgroove.html" target="_blank">Printgroove</a>. PMS hit the spot (man that sounds funny) &#8211; it was far from bad, but it certainly wasn&#8217;t awesome. At the point of its resurrection it needed a face lift, but never got one.</p>
<p>It&#8217;s now been years since <a href="http://kmbs.konicaminolta.us/content/products/models/printgroove.html" target="_blank">Printgroove</a> got started and PMS has continued to work well for it and numerous other projects &#8211; but I&#8217;ve got an itch that must be scratched. So while working on a small web service only project I&#8217;ve been adding in a couple dozen extra hours to mature PMS &#8211; or basically throw away it&#8217;s entire user facing API.</p>
<p>Here&#8217;s a sneak peak of whats to come:</p>
<pre class="brush: csharp; title: ; notranslate">
// - Pull IDbConnection from pool based on unique name (from config file)
// - This is useful if you have multiple databases to chat with
// - Not specifying a name will load the one marked default or the first one found
using (DbBroker cxt = new DbBroker(&quot;name-mapping-to-config-connection-string&quot;)) {

    // SELECT member.* FROM member WHERE username LIKE '%a%' ORDER BY id
	// Enumerate through results without storing Member objects in container
	foreach (Member m in cxt.Query&lt;Member&gt;().Like(&quot;username&quot;, &quot;%a%&quot;).OrderBy(&quot;id&quot;).Exec())
		Console.WriteLine(&quot; : &quot; + m);

	// Use raw SQL and ignore chainable methods for generation
	// Store results in List&lt;Member&gt; and then iterate through it
	string sql = &quot;SELECT member.* FROM member where username LIKE '%a%' ORDER BY id&quot;;
	foreach (Member m in cxt.Exec&lt;Member&gt;().Objects&lt;List&lt;Member&gt;&gt;(sql))
		Console.WriteLine(&quot; : &quot; + m);

	// get first member
	Console.WriteLine(&quot;first: &quot; + cxt.Query&lt;Member&gt;().Exec().First());

	// a short-cut to get the first member since we aren't filtering etc
	Console.WriteLine(&quot;first: &quot; + cxt.Exec&lt;Member&gt;().First());

	// SELECT COUNT(member.*) FROM member WHERE id &gt; 50000
	// Use generics to cast the output of ExecuteScalar to an Int32
	Console.WriteLine(&quot;count: &quot; + cxt.Query&lt;Member&gt;().Filter(&quot;id &gt; 50000&quot;).Exec().Count&lt;int&gt;());

	// Directly call ExecuteScalar but use generics to cast the output
    sql = &quot;SELECT id FROM member WHERE username='mimetnet'&quot;;
	Console.WriteLine(&quot;scalar: &quot; + cxt.Exec&lt;Member&gt;().Scalar&lt;int&gt;(sql));

	// Directly call ExecuteReader() with our generated SQL
	// SELECT id,username FROM member WHERE id &gt; 68665
	using (IDataReader reader = cxt.Query&lt;Member&gt;()
									.GreaterThan(&quot;id&quot;, 68665)
									.SetColumns(&quot;id,username&quot;)
									.Exec()
									.Reader()) {
		while (reader.Read()) {
			Console.WriteLine(&quot;Member(id={0}, name='{1}')&quot;, reader[0], reader[1]);
		}
	}
}
</pre>
<p>Please send me your thoughts/concerns/nightmares.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/02/19/pms2-coming-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

