<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Ting som gjør at du får vondt i hode</title>
        <link>http://geekswithblogs.net/nickholmes/Default.aspx</link>
        <description>by Nick Holmes</description>
        <language>en-GB</language>
        <copyright>Nick Holmes</copyright>
        <managingEditor>nickh@coyote-software.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Ting som gjør at du får vondt i hode</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/nickholmes/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Using LINQ To SQL in the F# WCF Web Service, Part 2</title>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/07/using-linq-to-sql-in-the-f-wcf-web-service-again.aspx</link>
            <description>&lt;p&gt;When we use LINQ To SQL in C#, the compiler doesn't generate IL to implement our query - we want Sql Server to execute the query, and so IL would not be much good. Instead the compiler creates data that describes our query, in the form of a System.Linq.Expressions.Expression. When the query needs to be executed, LINQ To SQL examines the expression and converts it to the equivalent T-SQL, and sends that to the database for execution.&lt;/p&gt;  &lt;p&gt;It follows, then, that if we want to use LINQ To SQL in F#, we'd like to get the F# compiler to convert our code into a System.Linq.Expressions.Expression. The surprising news is, the F# compiler can't do this directly.&lt;/p&gt;  &lt;p&gt;F# does have something equivalent; Quotations. If we place an expression within &amp;lt;@ and @&amp;gt;, we'll get back not an executable expression, but an object of type Microsoft.FSharp.Quotations.Expr&amp;lt;a'&amp;gt;. This needs to be converted to a Linq expression, and the function Microsoft.FSharp.Data.Linq.SQL will do this for us, but we have explicitly apply this function to the quotation.&lt;/p&gt;  &lt;p&gt;Our query is eventually expressed in (at least) these 5 forms, in this order:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;F# Source &lt;/li&gt;    &lt;li&gt;Microsoft.FSharp.Quotatons.Expr &lt;/li&gt;    &lt;li&gt;System.Linq.Expressions.Expression &lt;/li&gt;    &lt;li&gt;T-Sql &lt;/li&gt;    &lt;li&gt;Sql Server Execution Plan &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;I said "at least" because there could will be intermediate forms, like the compiler's AST.&lt;/p&gt;  &lt;p&gt;When you try and use this method, you're in for the next surprise. The supporting code for LINQ seems to exist only in the samples folders. As it's not compiled up by default, searching your entire drive for the "FSharp.Linq.dll" is both futile and frustrating. When I'd finished cursing whoever called this build a "release candidate", I copied the sample project over to the solution, deleted the usage sample files, and referenced this from the web service project.&lt;/p&gt;  &lt;p&gt;OK, all the prep is done, let's implement the methods. Firstly, lets open the necessary namespaces:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; System&lt;span style="color: blue"&gt;.&lt;/span&gt;ServiceModel       &lt;br /&gt;   &lt;br /&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; Microsoft&lt;span style="color: blue"&gt;.&lt;/span&gt;FSharp&lt;span style="color: blue"&gt;.&lt;/span&gt;Quotations&lt;span style="color: blue"&gt;.&lt;/span&gt;Typed       &lt;br /&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; Microsoft&lt;span style="color: blue"&gt;.&lt;/span&gt;FSharp&lt;span style="color: blue"&gt;.&lt;/span&gt;Data&lt;span style="color: blue"&gt;.&lt;/span&gt;Linq       &lt;br /&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; Microsoft&lt;span style="color: blue"&gt;.&lt;/span&gt;FSharp&lt;span style="color: blue"&gt;.&lt;/span&gt;Linq&lt;span style="color: blue"&gt;.&lt;/span&gt;SequenceOps       &lt;br /&gt;&lt;/code&gt;    &lt;/p&gt;&lt;p&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; Coyote&lt;span style="color: blue"&gt;.&lt;/span&gt;FSWCF&lt;span style="color: blue"&gt;.&lt;/span&gt;AWDC       &lt;br /&gt;&lt;span style="color: blue"&gt;open&lt;/span&gt; Coyote&lt;span style="color: blue"&gt;.&lt;/span&gt;FSWCF&lt;span style="color: blue"&gt;.&lt;/span&gt;AWEntity&lt;/p&gt;   &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Those final two Coyote namespaces contain the data context and the data entities.&lt;/p&gt;  &lt;p&gt;Now let's define a useful interface:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;span style="color: blue"&gt;[&amp;lt;&lt;/span&gt;ServiceContract&lt;span style="color: blue"&gt;(&lt;/span&gt;ConfigurationName = &lt;span style="color: maroon"&gt;"ISimpleService"&lt;/span&gt;, Namespace = &lt;span style="color: maroon"&gt;"http://coyote-software.com/FSWCF/SimpleService"&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;]&lt;/span&gt;       &lt;br /&gt;&lt;span style="color: blue"&gt;type&lt;/span&gt; ISimpleService =       &lt;br /&gt;    &lt;span style="color: blue"&gt;[&amp;lt;&lt;/span&gt;OperationContract&lt;span style="color: blue"&gt;&amp;gt;]&lt;/span&gt;       &lt;br /&gt;    &lt;span style="color: blue"&gt;abstract&lt;/span&gt; GetContact&lt;span style="color: blue"&gt;:&lt;/span&gt; contactID&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt; &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;Contact&amp;gt;       &lt;br /&gt;    &lt;span style="color: blue"&gt;[&amp;lt;&lt;/span&gt;OperationContract&lt;span style="color: blue"&gt;&amp;gt;]&lt;/span&gt;       &lt;br /&gt;    &lt;span style="color: blue"&gt;abstract&lt;/span&gt; GetContacts&lt;span style="color: blue"&gt;:&lt;/span&gt; unit &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; seq&amp;lt;Contact&amp;gt;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;This is pretty much as we did before. The return types of seq&amp;lt;Contact&amp;gt; means, in standard .Net terms, IEnumberable&amp;lt;Contact&amp;gt;. WCF doesn't marshal this (by reference) back to the client, instead the serializer iterates though it and places each item into an Xml document that is returned.&lt;/p&gt;  &lt;p&gt;Finally, the web service itself:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;span style="color: blue"&gt;[&amp;lt;&lt;/span&gt;ServiceBehavior&lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;]&lt;/span&gt;       &lt;br /&gt;&lt;span style="color: blue"&gt;type&lt;/span&gt; SimpleService&lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt; =       &lt;br /&gt;    &lt;span style="color: blue"&gt;let&lt;/span&gt; connString = &lt;span style="color: maroon"&gt;"server=VMDBSERVER\\SQLEXPRESS;database=AdventureWorks;trusted_connection=True"&lt;/span&gt;&lt;/code&gt; &lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;interface&lt;/span&gt; ISimpleService &lt;span style="color: blue"&gt;with&lt;/span&gt;     &lt;br /&gt;        &lt;span style="color: blue"&gt;member&lt;/span&gt; x&lt;span style="color: blue"&gt;.&lt;/span&gt;GetContact contactID =  &lt;br /&gt;                &lt;span style="color: blue"&gt;let&lt;/span&gt; awdc = &lt;span style="color: blue"&gt;new&lt;/span&gt; AdventureWorksDataContext&lt;span style="color: blue"&gt;(&lt;/span&gt;connString&lt;span style="color: blue"&gt;)&lt;/span&gt;     &lt;br /&gt;                SQL &amp;lt;@ seq &lt;span style="color: blue"&gt;{&lt;/span&gt; &lt;span style="color: blue"&gt;for&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; %awdc&lt;span style="color: blue"&gt;.&lt;/span&gt;Contacts &lt;span style="color: blue"&gt;when&lt;/span&gt; c&lt;span style="color: blue"&gt;.&lt;/span&gt;ContactID = %contactID &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; c &lt;span style="color: blue"&gt;}&lt;/span&gt; @&amp;gt;     &lt;br /&gt;         &lt;br /&gt;        &lt;span style="color: blue"&gt;member&lt;/span&gt; x&lt;span style="color: blue"&gt;.&lt;/span&gt;GetContacts &lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt; =  &lt;br /&gt;                &lt;span style="color: blue"&gt;let&lt;/span&gt; awdc = &lt;span style="color: blue"&gt;new&lt;/span&gt; AdventureWorksDataContext&lt;span style="color: blue"&gt;(&lt;/span&gt;connString&lt;span style="color: blue"&gt;)&lt;/span&gt;     &lt;br /&gt;                SQL &amp;lt;@ seq &lt;span style="color: blue"&gt;{&lt;/span&gt; &lt;span style="color: blue"&gt;for&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; %awdc&lt;span style="color: blue"&gt;.&lt;/span&gt;Contacts &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; c &lt;span style="color: blue"&gt;}&lt;/span&gt; &lt;span style="color: blue"&gt;|&lt;/span&gt;&amp;gt; take &lt;span style="color: maroon"&gt;50&lt;/span&gt; @&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This code works, but we aren't disposing the data context object. In fact, we can't dispose of it directly, because it will be needed when the serializer iterates the results. Therefore, a better implementation might be:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;span style="color: blue"&gt;[&amp;lt;&lt;/span&gt;ServiceBehavior&lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;]&lt;/span&gt;       &lt;br /&gt;&lt;span style="color: blue"&gt;type&lt;/span&gt; SimpleService&lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt; =       &lt;br /&gt;   &lt;span style="color: blue"&gt;let&lt;/span&gt; connString = &lt;span style="color: maroon"&gt;"server=VMDBSERVER\\SQLEXPRESS;database=AdventureWorks;trusted_connection=True"&lt;/span&gt;&lt;/code&gt; &lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;interface&lt;/span&gt; ISimpleService &lt;span style="color: blue"&gt;with&lt;/span&gt;     &lt;br /&gt;        &lt;span style="color: blue"&gt;member&lt;/span&gt; x&lt;span style="color: blue"&gt;.&lt;/span&gt;GetContact contactID =  &lt;br /&gt;                seq &lt;span style="color: blue"&gt;{&lt;/span&gt; use awdc = &lt;span style="color: blue"&gt;new&lt;/span&gt; AdventureWorksDataContext&lt;span style="color: blue"&gt;(&lt;/span&gt;connString&lt;span style="color: blue"&gt;)&lt;/span&gt;     &lt;br /&gt;                      yield! SQL &amp;lt;@ seq &lt;span style="color: blue"&gt;{&lt;/span&gt; &lt;span style="color: blue"&gt;for&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; %awdc&lt;span style="color: blue"&gt;.&lt;/span&gt;Contacts &lt;span style="color: blue"&gt;when&lt;/span&gt; c&lt;span style="color: blue"&gt;.&lt;/span&gt;ContactID = %contactID &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; c &lt;span style="color: blue"&gt;}&lt;/span&gt; @&amp;gt; &lt;span style="color: blue"&gt;}&lt;/span&gt;     &lt;br /&gt;         &lt;br /&gt;        &lt;span style="color: blue"&gt;member&lt;/span&gt; x&lt;span style="color: blue"&gt;.&lt;/span&gt;GetContacts &lt;span style="color: blue"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;)&lt;/span&gt; =  &lt;br /&gt;                seq &lt;span style="color: blue"&gt;{&lt;/span&gt; use awdc = &lt;span style="color: blue"&gt;new&lt;/span&gt; AdventureWorksDataContext&lt;span style="color: blue"&gt;(&lt;/span&gt;connString&lt;span style="color: blue"&gt;)&lt;/span&gt;     &lt;br /&gt;                      yield! SQL &amp;lt;@ seq &lt;span style="color: blue"&gt;{&lt;/span&gt; &lt;span style="color: blue"&gt;for&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; %awdc&lt;span style="color: blue"&gt;.&lt;/span&gt;Contacts &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; c &lt;span style="color: blue"&gt;}&lt;/span&gt; &lt;span style="color: blue"&gt;|&lt;/span&gt;&amp;gt; take &lt;span style="color: maroon"&gt;50&lt;/span&gt; @&amp;gt; &lt;span style="color: blue"&gt;}&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;This wraps one sequence in another, and will create and dispose of the data context for each iterator that's created.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131874"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131874" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131874.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/07/using-linq-to-sql-in-the-f-wcf-web-service-again.aspx</guid>
            <pubDate>Thu, 07 May 2009 16:24:32 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131874.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/07/using-linq-to-sql-in-the-f-wcf-web-service-again.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131874.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Using LINQ To SQL in the F# WCF Web Service, Part 1</title>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/06/using-linq-to-sql-in-the-f-wcf-web-service.aspx</link>
            <description>&lt;p&gt;To use LINQ To SQL, we need a Data Context object to provide our point of entry. It might be possible to use a System.Data.Linq.DataContext object directly, but its more usual to derive from this class to make a database specific version. Additionally, we need classes to represent our "entities". These define the mapping to the tables (or views) and columns.&lt;/p&gt;  &lt;p&gt;We could code all this up by hand, but Visual Studio 2008 comes with the grandly named &lt;a href="http://msdn2.microsoft.com/en-us/library/bb384429.aspx" target="_blank"&gt;Object Relational Designer&lt;/a&gt; that makes short work of this. This is a graphical tool to define our data context and entities, and a code generator to spit out some C# when we're done. The designer edits a .dbml file that defines the mapping, and the code generation is based on this, so it easy to refine these mappings as we go. There is also a command line tool, &lt;a href="http://msdn2.microsoft.com/en-us/library/bb386987.aspx" target="_blank"&gt;SqlMetal.exe&lt;/a&gt;, for those working without Visual Studio, or wanting to set up more complex build processes.&lt;/p&gt;  &lt;p&gt;I don't want to spend time on this part of the process; it's exactly the same as if it was being done for consumption in C#, and there is plenty of coverage on MSDN. I will be posting the code for all of this soon.&lt;/p&gt;  &lt;p&gt;I'll be using the Microsoft AdventureWorks sample database, which can be downloaded &lt;a href="http://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=4004" target="_blank"&gt;here&lt;/a&gt;. This first test is based on the Contact table. The data entity class will also be called Contact. The web service will then be returning instances of this class - in other words, it will be our WCF Data Contract.&lt;/p&gt;  &lt;p&gt;How then, are we going to get the WCF DataContract attribute onto this generated class? Happily, the LINQ team wondered about that too, and added a "Serialization Mode" flag to the O/R Designer. Its values are "None" and the curiously named "Unidirectional", but the latter is the one we want.&lt;/p&gt;  &lt;p&gt;A quick look at the generated class confirms this. Here's a snippet to give you the idea of what's generated:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;    [Table(Name="Person.Contact")]        &lt;br /&gt;    [DataContract()]         &lt;br /&gt;    public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged         &lt;br /&gt;    {         &lt;br /&gt;        private string _Title;         &lt;br /&gt;&lt;/code&gt;&lt;/p&gt;    &lt;p&gt;&lt;code&gt;        [Column(Storage="_Title", DbType="NVarChar(8)")]        &lt;br /&gt;        [DataMember(Order=3)]         &lt;br /&gt;        public string Title         &lt;br /&gt;        {         &lt;br /&gt;            get         &lt;br /&gt;            {         &lt;br /&gt;                return this._Title;         &lt;br /&gt;            }         &lt;br /&gt;            set         &lt;br /&gt;            {         &lt;br /&gt;                if ((this._Title != value))         &lt;br /&gt;                {         &lt;br /&gt;                    this.OnTitleChanging(value);         &lt;br /&gt;                    this.SendPropertyChanging();         &lt;br /&gt;                    this._Title = value;         &lt;br /&gt;                    this.SendPropertyChanged("Title");         &lt;br /&gt;                    this.OnTitleChanged();         &lt;br /&gt;                }         &lt;br /&gt;            }         &lt;br /&gt;        }&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Just a couple of comments before moving on to using this stuff from F#.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The class is marked as partial. This allows us to create another source file in the same project containing more fields, methods and properties for this class. Its perfect for code generators, because it allows clean separation between generated and hand-written code. F# does not seem to have such a feature, but I think it should. &lt;/li&gt;    &lt;li&gt;The entity is by default a POCO. As with WCF, everything that LINQ to SQL needs is added via attributes. &lt;/li&gt;    &lt;li&gt;The setter shown here looks busy, but the OnXXX methods are partial methods. If these are not implemented in another partial part of this class, the compile drops the calls. Again, a nice mechanism targeted at code generators, but missing from F#. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This code needs to be compiled up into an assembly, and referenced from the F# project.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131845"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131845" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131845.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/06/using-linq-to-sql-in-the-f-wcf-web-service.aspx</guid>
            <pubDate>Wed, 06 May 2009 20:12:26 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131845.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/06/using-linq-to-sql-in-the-f-wcf-web-service.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131845.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Problems with WCF Data Contracts in F#</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/06/problems-with-wcf-data-contracts-in-f.aspx</link>
            <description>&lt;p&gt;[Update: This blog post was orignally posted in early 2008, in another blog. Things have changed a little in F#, and Ted Nedward has provided updated information related to this, showing &lt;a href="http://blogs.tedneward.com/2009/01/24/Building+WCF+Services+With+F+Part+2.aspx"&gt;how to create data contracts in F#&lt;/a&gt;]&lt;/p&gt;
&lt;p&gt;On the road to a really interesting Web Service, I increased the complexity of my "could-not-be-simpler" web service one notch, and added a class-typed argument to the operation. This requires constructing of a Data Contract, so that WCF can build the correct WSDL, and also know how to serialize (and de-serialize) messages. As with the Service Contract, this is simply done with a couple of attributes, like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;&lt;strong&gt;[&amp;lt;DataContract&amp;gt;]&lt;/strong&gt; &lt;br /&gt;
type public SimpleDataContract() = &lt;br /&gt;
        let mutable _propertyOne = System.String.Empty &lt;br /&gt;
        let mutable _propetyTwo = 0 &lt;br /&gt;
        &lt;strong&gt;[&amp;lt;DataMember&amp;gt;]&lt;/strong&gt; &lt;br /&gt;
        member public x.PropertyOne &lt;br /&gt;
            with get() = _propertyOne &lt;br /&gt;
            and set(value) = _propertyOne &amp;lt;- value            &lt;br /&gt;
        &lt;strong&gt;[&amp;lt;DataMember&amp;gt;]&lt;/strong&gt; &lt;br /&gt;
        member public x.PropertyTwo &lt;br /&gt;
            with get() = _propetyTwo &lt;br /&gt;
            and set(value) = _propetyTwo &amp;lt;- value&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[&amp;lt;ServiceContract(ConfigurationName = "ISimpleService", Namespace = "&lt;a href="http://coyote-software.com/FSWCF/SimpleService"&gt;http://coyote-software.com/FSWCF/SimpleService")&lt;/a&gt;&amp;gt;] &lt;br /&gt;
type ISimpleService = &lt;br /&gt;
    [&amp;lt;OperationContract&amp;gt;] &lt;br /&gt;
    abstract TestMethod: Param:SimpleDataContract -&amp;gt; string&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[&amp;lt;ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)&amp;gt;] &lt;br /&gt;
type SimpleService() = &lt;br /&gt;
    interface ISimpleService with &lt;br /&gt;
        member x.TestMethod s = "Hello " + s.PropertyOne;&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Unfortunately, code causes a run-time exception when the service is started (well before any messages arrive). The error is about "method get_PropertyOne" not being a property, and therefore an invalid recipient of the DataMember attribute. I strongly suspect that this is due to a know bug in the current F# compiler emitting IL for properties in a non-standard way.&lt;/p&gt;
&lt;p&gt;(WCF data contract members must be placed on properties, and not fields.)&lt;/p&gt;
&lt;p&gt;The obvious work-around is to create the class in C#. The equivalent C# class is simply:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;    [DataContract] &lt;br /&gt;
    public class SimpleDataContract &lt;br /&gt;
    { &lt;br /&gt;
        public SimpleDataContract() { }&lt;/code&gt;&lt;code&gt;&lt;br /&gt;
        [DataMember] &lt;br /&gt;
        public string PropertyOne { get; set; } &lt;br /&gt;
        [DataMember] &lt;br /&gt;
        public int PropertyTwo { get; set; } &lt;br /&gt;
    }&lt;/code&gt; &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For once, C# trumps F# for brevity! Anyway, using this class from F# is no more complex than referencing the dll, and removing the F# SimpleDataContract class, and the web service now works as expected.&lt;/p&gt;
&lt;p&gt;The next step is to do something a little more interesting in the operation; use LINQ to SQL to query a database, and return some data. As it turns out, our data contracts will be again come via C#, so this issue will be by-passed.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131844"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131844" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131844.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/06/problems-with-wcf-data-contracts-in-f.aspx</guid>
            <pubDate>Wed, 06 May 2009 20:09:32 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131844.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/06/problems-with-wcf-data-contracts-in-f.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131844.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An WCF Service Host in F#</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-wcf-service-host-in-f.aspx</link>
            <description>&lt;p&gt;I had half an hour to fill while I waited for a large download, on my slow connection. I'd thought about re-implementing my C# WCF test host in F#. It didn't look too complex, and indeed it wasn't:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;#light      &lt;br /&gt;#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\"       &lt;br /&gt;#r "System.ServiceModel.dll"       &lt;br /&gt;#r "Server.dll"       &lt;br /&gt;open System       &lt;br /&gt;open System.ServiceModel       &lt;br /&gt;open System.Diagnostics       &lt;br /&gt;open Coyote.FSWCFTest&lt;/code&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;code&gt;let services =&lt;/code&gt;&lt;/p&gt;  &lt;p align="left"&gt;&lt;code&gt;    [ (typeof&amp;lt;TestWebService&amp;gt;, "http://localhost:8080/Test/FSWCFTest.svc") ]&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;let logException (msg, exn:Exception) =      &lt;br /&gt;    Trace.TraceWarning(msg)       &lt;br /&gt;    Trace.WriteLine(exn.Message)       &lt;br /&gt;    Trace.WriteLine(exn.Source)&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;let startHost (serviceType:Type, url) =      &lt;br /&gt;    try       &lt;br /&gt;        let uri = [| new Uri(url) |]       &lt;br /&gt;        let host = new ServiceHost(serviceType, uri)       &lt;br /&gt;        host.Open()       &lt;br /&gt;        Some(host)       &lt;br /&gt;    with       &lt;br /&gt;    | e -&amp;gt; logException ("Exception thrown starting service.", e)       &lt;br /&gt;           None&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;let stop&lt;/code&gt;&lt;code&gt;Host (host: ServiceHost option)  =      &lt;br /&gt;    try       &lt;br /&gt;        match host with       &lt;br /&gt;        | None -&amp;gt; ()       &lt;br /&gt;        | Some(host) -&amp;gt;       &lt;br /&gt;            if host.State &amp;lt;&amp;gt; CommunicationState.Closed then       &lt;br /&gt;                host.Close()       &lt;br /&gt;                Trace.WriteLine("Service Stopped")                &lt;br /&gt;    with       &lt;br /&gt;    | e -&amp;gt; logException ("Exception thrown stopping service.", e)&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;let showHostStatus (host: ServiceHost option) =      &lt;br /&gt;    match host with       &lt;br /&gt;    | Some(hst) -&amp;gt;       &lt;br /&gt;        let strdots = new String ( '.', 40 - hst.Description.Name.Length);       &lt;br /&gt;        Console.ForegroundColor &amp;lt;- match hst.State with       &lt;br /&gt;                                   | CommunicationState.Opened -&amp;gt; ConsoleColor.Green       &lt;br /&gt;                                   | _ -&amp;gt; ConsoleColor.Red         &lt;br /&gt;        Console.WriteLine("{0} service{1}{2}", hst.Description.Name,&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;            strdots, hst.State.ToString())      &lt;br /&gt;    | None -&amp;gt; Console.ForegroundColor &amp;lt;- ConsoleColor.Red       &lt;br /&gt;              Console.WriteLine("Service failed to start.")&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;let main =      &lt;br /&gt;    Console.Title &amp;lt;- "Server Test Console"       &lt;br /&gt;    Console.BackgroundColor &amp;lt;- ConsoleColor.DarkBlue       &lt;br /&gt;    Console.WindowWidth &amp;lt;- 120       &lt;br /&gt;    Console.Clear()       &lt;br /&gt;    Console.WriteLine("Server Test Console")       &lt;br /&gt;    Console.WriteLine("Copyright 2008 Coyote Software, GmbH.")       &lt;br /&gt;    Console.WriteLine()       &lt;br /&gt;    let hosts = services |&amp;gt; List.map startHost       &lt;br /&gt;    let mutable quit = false       &lt;br /&gt;    while not quit do       &lt;br /&gt;        let inp = Console.ReadLine()   &lt;br /&gt;        let inpClean = inp.Trim().ToLower()       &lt;br /&gt;        match inpClean with       &lt;br /&gt;        | "exit"   -&amp;gt; quit &amp;lt;- true       &lt;br /&gt;        | "cls"    -&amp;gt; Console.Clear()       &lt;br /&gt;        | "status" -&amp;gt; let cc = Console.ForegroundColor       &lt;br /&gt;                      Console.WriteLine("Status at {0} is", DateTime.Now)       &lt;br /&gt;                      hosts |&amp;gt; List.iter showHostStatus       &lt;br /&gt;                      Console.ForegroundColor &amp;lt;- cc       &lt;br /&gt;        | "stop"   -&amp;gt; hosts |&amp;gt; List.iter stopHost       &lt;br /&gt;        | _        -&amp;gt; Console.WriteLine("Unknown command {0}", inp)       &lt;br /&gt;    done&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;This implementation is about half the size of the C# implementation, although the original has a couple of extra commands. I preferred Console.WriteLine to printfn, as I have used other Console methods, and I think the code is a little clearer.&lt;/p&gt;  &lt;p&gt;One improvement to this app would be to have is use reflection to automatically find all  the services in the assembly (rather than require the list to be set up), but I've been using the C# version almost daily for over a year, and its good enough like this.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131814"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131814" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131814.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-wcf-service-host-in-f.aspx</guid>
            <pubDate>Tue, 05 May 2009 17:50:39 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131814.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-wcf-service-host-in-f.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131814.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Accessing F# Interface Implementations from C#</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/accessing-f-interface-implementations-from-c.aspx</link>
            <description>&lt;p&gt;I tidied up the F# web service, mainly by factoring out an interface for the web service. Without the attributes it looks like this:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;type ITestWebService =      &lt;br /&gt;    abstract TestMethod: Param:string -&amp;gt; string &lt;/code&gt;&lt;code&gt;type TestWebService() =      &lt;br /&gt;    interface ITestWebService with       &lt;br /&gt;        member x.TestMethod s = "Hello " + s;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;However, I noticed when I was setting up the C# host, I needed to cast to get to access the interface, like this:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;TestWebService tws = new TestWebService();      &lt;br /&gt;ITestWebService itws = tws as ITestWebService;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;Neither of these far more obvious ways to access the member work:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;tws.TestMethod("test");                     // Doesn't compile      &lt;br /&gt;tws.ITestWebService.TestMethod("test");     // Doesn't compile &lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131813"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131813" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131813.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/accessing-f-interface-implementations-from-c.aspx</guid>
            <pubDate>Tue, 05 May 2009 17:44:12 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131813.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/accessing-f-interface-implementations-from-c.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131813.aspx</wfw:commentRss>
        </item>
        <item>
            <title>An F# WCF Web Service</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-f-wcf-web-service.aspx</link>
            <description>&lt;p&gt;For all the power and flexibility that WCF provides, creating a basic web service in C# is easy enough. Essentially, it's just a POCO adorned with some special attributes. Doing the same thing in F#, then, should also be easy enough. I tried this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;#light        &lt;br /&gt;#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\"         &lt;br /&gt;#r "System.ServiceModel.dll"&lt;/code&gt;&lt;/p&gt;    &lt;p&gt;&lt;code&gt;open System.ServiceModel        &lt;br /&gt;&lt;/code&gt;&lt;/p&gt;    &lt;p&gt;&lt;code&gt;[&amp;lt;ServiceContract(ConfigurationName = "TestWebService", Namespace = "&lt;a href="http://coyote-software.com/FSWCFTest"&gt;http://coyote-software.com/FSWCFTest")&lt;/a&gt;&amp;gt;]         &lt;br /&gt;[&amp;lt;ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)&amp;gt;]         &lt;br /&gt;&lt;/code&gt;&lt;code&gt;type public TestWebService =        &lt;br /&gt;    class&lt;/code&gt;&lt;code&gt;        &lt;br /&gt;        [&amp;lt;OperationContract&amp;gt;]         &lt;br /&gt;        member public x.TestMethod(s) = "Hello " + s;         &lt;br /&gt;    end&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Essentially, I've referenced the WCF assembly, and created a class with a single method, and added the needed ServiceContract, ServiceBehavior and OperationContract attributes. I compiled this into to a dll.&lt;/p&gt;  &lt;p&gt;To test this, I needed to host it somewhere. I already have a console host application in C#, and so I used that for testing, and got this exception:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Constructors in F# are a little different to C#, and a bit of hunting through the spec, and I had my solution:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;type public TestWebService&lt;strong&gt;()&lt;/strong&gt; =&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Just a pair of missing parenthesis! This was going extremely well, so the next task is to call the method. For this we need to generate a client proxy with svcutil.exe. This generates C# code, but no reason not to use this, driven from an F# client. I compiled up the generated C# into a dll, and then referenced that from a simple F# console client:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;#light        &lt;br /&gt;#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\"         &lt;br /&gt;#I @"c:\Development Projects\Research\FSWCF\Client\bin\debug\"         &lt;br /&gt;#r "System.ServiceModel.dll"         &lt;br /&gt;#r "client.dll"&lt;/code&gt;&lt;code&gt;open System&lt;/code&gt; &lt;/p&gt;    &lt;p&gt;do      &lt;br /&gt;    let service = new TestWebServiceClient()       &lt;br /&gt;    let retVal = service.TestMethod "Nick"       &lt;br /&gt;    printfn "%s" retVal       &lt;br /&gt;    Console.ReadLine() |&amp;gt; ignore&lt;/p&gt;    &lt;p&gt;&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This resulted in "Hello Nick" on the console. It was extremely simple to get this (albeit trivial) web service running.&lt;/p&gt;  &lt;p&gt;What I've not done yet is pass a more complex data contract, but that will come with in the next step, using LINQ-to-Sql to grab some data, and pass that back to the client.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131812"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131812" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131812.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-f-wcf-web-service.aspx</guid>
            <pubDate>Tue, 05 May 2009 17:41:31 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131812.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/an-f-wcf-web-service.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131812.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Curried Functions In C#</title>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/curried-functions-in-c.aspx</link>
            <description>&lt;p&gt;I previously said that C#’s lambda functions were not curried, and it wasn’t possible to partially apply them. However, it’s usually possible to manually curry functions, if you need to. Here is an example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;static void Main(string[] args)       &lt;br /&gt;{        &lt;br /&gt;    int t = f(1)(3)(5)(7); &lt;/code&gt;      &lt;/p&gt;&lt;p&gt;    Console.WriteLine("{0}", t); &lt;/p&gt;      &lt;p&gt;    var g = f(1);       &lt;br /&gt;    var h = g(3);        &lt;br /&gt;    var i = h(5);        &lt;br /&gt;    int j = i(7); &lt;/p&gt;      &lt;p&gt;    Console.WriteLine("{0}", j);       &lt;br /&gt;} &lt;/p&gt;      &lt;p&gt;static Func&amp;lt;int, Func&amp;lt;int, Func&amp;lt;int, int&amp;gt;&amp;gt;&amp;gt; f(int a)       &lt;br /&gt;{        &lt;br /&gt;    return (b =&amp;gt; c =&amp;gt; d =&amp;gt; (a + b + c + d));        &lt;br /&gt;}&lt;/p&gt;    &lt;/blockquote&gt;  &lt;p&gt;It’s interesting to step through that code in Visual Studio. As the complex lambda expression is repeatedly returned to, the highlighting outlines exactly which sub-function is being evaluated. However, the return type of the f is not at all easy to read (although the new C# 3.0 var keyword means we don’t need to repeat this horror for g, h, &amp;amp; i). The equivalent code in F# is far more svelte:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;code&gt;let f a b c d = a + b + c + d&lt;/code&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Even with my cryptically short function name, its clear what this function does. There’s one more thing on curried functions I want to look at, but before that I’m going to look at implementing Web Services in F# and WCF.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131809"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131809" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131809.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/curried-functions-in-c.aspx</guid>
            <pubDate>Tue, 05 May 2009 16:37:06 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131809.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/curried-functions-in-c.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131809.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Do I have to have a curried function in F#?</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/do-i-have-to-have-a-curried-function-in-f.aspx</link>
            <description>&lt;p&gt;All this currying and partial application stuff is all very interesting, but surely its has to have some kind of run-time performance hit. What if I just want to keep things straightforward?&lt;/p&gt;
&lt;p&gt;Firstly, if we have a function that takes only a single argument, there is nothing to curry. It makes no sense to call a function with no arguments, so your only option is to make the &lt;em&gt;exact&lt;/em&gt; call.&lt;/p&gt;
&lt;p&gt;That in mind, we can do this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let repeat(str, n) = ...&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;That syntax is sure to catch out C# programmers (like me), because it looks a lot like a normal C# function call. You might not even register, at first glance, the extra parenthesis around the arguments. However, these parenthesis are nothing to do with the function syntax, but everything to do with tuples:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let name = ("Fred", "Smith")&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;here, name is equal to the pair of values “Fred” and “Smith”. Tuples can have more than 2 components. The type is reported as:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;val x : string * string &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Personally, I find that * as the separator a bit jarring, but I assume there is a good reason to use it in preference to “,” - maybe it will come to light some day.&lt;/p&gt;
&lt;p&gt;Anyway, a tuple is a simple data structure that allows you to group 2 or more components into a single argument, and thereby create functions that need not be curried.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131808"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131808" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131808.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/do-i-have-to-have-a-curried-function-in-f.aspx</guid>
            <pubDate>Tue, 05 May 2009 16:33:28 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131808.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/do-i-have-to-have-a-curried-function-in-f.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131808.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Understanding F#&amp;rsquo;s Function Types, Part 3</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-3.aspx</link>
            <description>&lt;p&gt;In my last two posts, I described how F# transforms functions with more than one argument into chains of functions with a single argument. Strictly speaking, all functions in F# must have exactly one argument, and return exactly one result.  There is a special type, unit, which can be used as a dummy when no argument or return is required, like void in C#.&lt;/p&gt;  &lt;p&gt;F#’s functions are lambda functions, formally described by lambda calculus which provides the theoretic model for functional languages. Happily, we don’t all need to be fluent in lambda calculus to use F#. The F# compiler automatically converts functions to these chains of lambda functions in a process called &lt;em&gt;currying&lt;/em&gt;. The resulting function is said to be &lt;em&gt;curried -&lt;/em&gt; but remember the resulting function is just the first in the chain&lt;em&gt;.&lt;/em&gt; The term is named for Haskell Curry - for whom the Haskell language is also named (indeed there is another language called Curry too).&lt;/p&gt;  &lt;p&gt;Incidently C#’s lambda expressions are not curried automatically curried, and so its not possible to partially apply them.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131807"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131807" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131807.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-3.aspx</guid>
            <pubDate>Tue, 05 May 2009 16:30:22 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131807.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-3.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131807.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Understanding F#&amp;rsquo;s Function Types, Part 2</title>
            <category>F#</category>
            <category>Functional Programming</category>
            <link>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-2.aspx</link>
            <description>&lt;p&gt;OK, so you define a function that takes 2 parameters, but you actually get a function that takes 1 parameter, and returns a function that will take the second parameter and return the final result. Hmm, this raises lots of questions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What happens if the function has more than two parameters?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;gt;let f a b c = a + b + c&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;is of type&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font face="Courier New"&gt;val f : int -&amp;gt; int -&amp;gt; int -&amp;gt; int&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So we get a chain of functions, each one taking exactly one parameter, and returning a function that takes the next.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;If there are more that 2 parameters, can I partially apply more than one at a time?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Following on from above, we can do:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;gt;let g = f 1 2&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font face="Courier New"&gt;val g : (int -&amp;gt; int)&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;gt;g 3&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font face="Courier New"&gt;val it : int = 6&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This looks like F# functions can just accept a subset of their arguments, but remember that as f returns a function, what is really happening is the equivalent of this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let g = (f 1) 2&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We are making a sequence of function calls. We could make the exact call like this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;((f 1) 2) 3&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The consequence of this is that we can’t partially apply &lt;em&gt;any&lt;/em&gt; sub-set of the arguments. We still have to call the function chain in the correct order, which partially apply the arguments strictly from left to right.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What happens to the partially applied arguments?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;They are captured forever and immutably in the returned function.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let f a b = a + b&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let g = f 1&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;let h = f 10&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;g will always return its argument plus 1, h always plus 10. f did not return the same function when called with different arguments.&lt;/p&gt;
&lt;p&gt;Strange as this last point seems to those of us more used to C#, this notion of capturing values into functions does exist in C#. Anonymous delegates capture the values of variables from their declaring method. In C# 3.0, lambda functions are a simpler syntax for these anonymous delegates. Here is an example.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;static void Main(string[] args) &lt;br /&gt;
{ &lt;br /&gt;
    var g = f(1); &lt;br /&gt;
    var h = f(10); &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    Console.WriteLine(”g(5) = {0}”, g(5)); &lt;br /&gt;
    Console.WriteLine(”h(5) = {0}”, h(5)); &lt;br /&gt;
    Console.WriteLine(”f(3)(5) = {0}”, f(3)(5)); &lt;br /&gt;
} &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;static Func&amp;lt;int, int&amp;gt; f(int a) &lt;br /&gt;
{ &lt;br /&gt;
    return new Func&amp;lt;int, int&amp;gt;(x =&amp;gt; x + a); &lt;br /&gt;
}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Similar result, ugly code!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131806"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=131806" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/nickholmes/aggbug/131806.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Nick Holmes</dc:creator>
            <guid>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-2.aspx</guid>
            <pubDate>Tue, 05 May 2009 16:25:37 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/nickholmes/comments/131806.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/nickholmes/archive/2009/05/05/understanding-frsquos-function-types-part-2.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/nickholmes/comments/commentRss/131806.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>