Fremus.co.za

Demistifying Life and Web Development

WCF – Getting the foundations right

Ok so admittedly I have been using ASMX services for too long now and the time has come to kick it to the curb and adopt WCF. And the issue I have been having of late was that I was skimming through code just to get stuff done, without spending the time understanding some of the details.

Why would I want to adopt WCF? Well there are the list of reasons found in articles on MSDN, one whitepaper can be found here, and of particular interest is the combination of technologies and the general idea that interoperability is the main goal. But these things are just a way of promoting the technology, and its not until you understand what it can do that you realise what it is you are dealing with. And to help you get to that point you need to work through an example, and I found that after I worked through the “Getting Started Tutorial” example, a light went on and I was like, “ok I get it”. Essentially a WCF service is made up of two key elements (there is a third as well) but in terms of C# code there are two key elements:
*An interface marked as a Service Contract using the ServiceContract attribute and with the methods marked as OperationContracts using an attribute with the same name
*A class that implements the methods in the interface

The third part of a WCF service is the configuration settings which can be found in a web.config/app.config’s system.servicemodel tag. Within the servicemodel section you define service behaviours as well as endpoints. One of the keys to understanding WCF is knowing that a service is defined by its endpoint, see it as a consumer. WCF can be consumed by client web apps, Silverlight apps and desktop apps. The endpoints themselves have configuration settings as well specifically relating to message sizes.

From the tutorial I was able to see that you can run a WCF service in a browser, without having IIS running. Thats something I need to think about but it does pose a few interesting questions. After I did the tutorial I wanted to do a simple REST service, and that took a few minutes but eventually got that sorted. StackOverflow was quite helpful and so was several articles on MSDN, with this one being the most helpful.

  • Share/Bookmark
Tags: ,
posted by fr3dr1k in C#,WCF and have No Comments

Using System.Uri’s Segments property and List().ForEach

You know the feeling when you see someone use a property and you go, nice, I never knew that. Well last night that happened to me after reading Scott Hanselman’s article on Windows Powershell. In the article he created a script that automatically downloaded his podcasts. To do this he use the System.Uri class which has a property called Segments. Segments returns a string array which consists of elements in a Uri separated by a forward slash ‘/’. So lets say you have this url:

http://developer.yahoo.com/yap/guide/caja-support.html

You could then use System.Uri to get all the bits in the Uri like this:

            Uri url = new Uri("http://developer.yahoo.com/yap/guide/caja-support.html");

            string[] arrUri = url.Segments;
            var item = from u in arrUri
                       select u;

            foreach (string s in item)
            {
                Console.WriteLine(s);
            }

Something else I started doing or using yesterday is the ToList().ForEach delegate method. I noticed it in the LinqToTwitter api:


            var twitterTrends = from trends in tCtx.Trends
                                select trends;

            twitterTrends.ToList().ForEach(t =>
                Console.WriteLine(t.Query));

The ForEach works on a list as well, so if you take the same code I wrote earlier using a foreach loop for the Uri segments you can rewrite that as:


item.ToList().ForEach(s => Console.WriteLine(s));

The variable s is an anonymous type so it infers from the type what type it is. That is shorter code, not sure if its more efficient, but it sure looks nicer.

  • Share/Bookmark
Tags:
posted by fr3dr1k in C# and have No Comments

Getting a single XAttribute or XElement

I have come across two ways querying XElements or XAttributes:

  • Make an object IEnumerable and loop through the results
  • Use the Single() method to return a single object only

Typically you may have a situation where you write some Linq-to-XML like this:

IEnumerable attrib = from att in elemInner.Attributes()
                                             where (string)att.Name.ToString() == "sectionName"
                                             select att;

To loop through the results you have to use a foreach loop like this:


foreach (XAttribute innerAttrib in attrib)
{
     builder.AppendLine("

" + innerAttrib.Value +"

"); }

If however you just wanted a single attribute result, you could write the same code like this:


 XAttribute attribDisplayStyle = (from att in elemInner.Attributes()
                                            where (string)att.Name.ToString() == "sectionName"
                                            select att).Single();

Then you only have a single XAttribute instance and you dont need a foreach loop:


attribDisplayStyle.Value;

You would apply the same logic to XElement as well.

  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments

Mimicking AJAX behaviour with Generic Handler (.ashx) file uploader

Earlier today I posted a blog about using a generic handler (.ashx) to upload a file to a web server, and in the back of my head I wanted to use it somewhere neat and special, and I also want to find the most reliable and working version. And I also want to learn what the approaches are to doing so, and why not to do it a certain way, etc.

So back to the topic of the post and the first thing that needs to be understood is that you CANNOT upload files with AJAX/JavaScript. This is because of you cannot retrieve the contents of a file off a local system, and if this was so it would cause major security headaches. So what I ended up doing is following the IFRAME approach, by which you make it look as if the upload is happening all AJAX-like when in actual fact its not. So what I did was create an IFRAME:

Notice that I added a div called divTimerValue, which I use to display some progress indicator, in my case it will be busy that will grow and subside with dots. In the source file (where the IFRAME points to) I create a form:

Notice that it has the ReturnValue.ashx action and that I have added an onclick function to the submit button, which looks like this:

        function dotsAnimate() {
            parent.document.getElementById("divFrame").style.display = 'none';
            var dotspan = parent.document.getElementById("divTimerValue");
            dotspan.style.display = '';
            setInterval(function() {
                if (dotspan.innerHTML == 'busy...') {
                    dotspan.innerHTML = 'busy.';
                }
                else {
                    dotspan.innerHTML += '.';
                }
            }, 1000);
        };

The dotsAnimate function uses the setTimeout function to create the animation. The generic handler then returns some HTML that contains a javascript function that clears the timeout and prints a message in the divTimerValue div:

        context.Response.Write(@"");
        context.Response.Write(savedFileName);

And this code produces the desired result.

  • Share/Bookmark
posted by fr3dr1k in ASP.NET,C#,Web Development and have No Comments

Uploading files with a generic handler (.ashx)

In recent times, the last year or so, I have come to move away from web forms and move more towards an architecture that involves plain html combined with web services or in some cases generic handlers. I like this approach because it allows me to perfectly control the quality of the HTML that is rendered, which in turn is good for a few reasons, one being search engines and the other being true to web standards (or at least trying to). Its relatively easy to combine an asynchronous call with an ASHX file. ASHX files are lighter in terms of processing than their ASPX counterparts. With that in mind I decided to write or create a generic handler based on Scott Hanselman’s example here. And it works! Thats the key thing.

First create an HTML file and add code for a form like this:

Then you create the code in the ASHX file like this:

        string savedFileName = "";

        foreach(string file in context.Request.Files)
        {
            HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;
            if (hpf.ContentLength == 0)
                continue;
            savedFileName = context.Server.MapPath(Path.GetFileName(hpf.FileName));
            hpf.SaveAs(savedFileName);
        }
        context.Response.Write(savedFileName);
  • Share/Bookmark
Tags:
posted by fr3dr1k in ASP.NET,C# and have No Comments

Getting search results from Bing REST service and using LINQ to process results

So today I started reading about the Bing API and I got myself an API key and I read through the basic instruction manual, which tells you how to get search results from the web through the Bing REST service. Its pretty straight forward, just get your own key though! But here is some sample code that does the trick and uses Linq to XML to process the results:

            XDocument document = XDocument.Load("http://api.search.live.net/xml.aspx?Appid=&query=sushi&sources=web");
            XElement root = document.Root;
            XNamespace web = "http://schemas.microsoft.com/LiveSearch/2008/04/XML/web";
            var searchItems = document.Descendants(web + "Results").SingleOrDefault();

            IEnumerable testelem = from el in searchItems.Elements()
                                                 select el;
            foreach (XElement myElem in testelem)
            {
                context.Response.Write(myElem.Value + "");
            }

That is very easy!

  • Share/Bookmark
posted by fr3dr1k in Bing,C#,LINQ to XML and have No Comments

What are extension methods?

Extension methods in C# enable you to extend existing types without having to create a new type. This essentially means that you can take the class String and add methods to it that extends it, and when you create a string those methods become available. You just have to make sure you include the namespace of the class that implements the methods. A very basic example would be to create an extension method that returns the length of a string. So you start off first by creating:

namespace MyExtensionMethods
{
    public static class MyExtensionMethods
    {
        public static int ReturnText(this String text)
        {
            return text.Length;
        }
    }
}

Notice the this keyword before String. Also notice that the class is static and that the method is static, which means that the class can only contain static methods. If you create an instance method in a static class you will get a compiler error. To use the extension method you include the using directive:

namespace ExtensionMethods2
{
    using MyExtensionMethods;
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Test";
            Console.WriteLine(s.ReturnText());
        }
    }
}

Notice that the string s now has an extra method associated to it called ReturnText.

  • Share/Bookmark
posted by fr3dr1k in C# and have No Comments

C# and streams

C# uses a lot of streams, or at least it seems so in some of the code examples I have looked at.
Having said that I felt the need to read up on the Stream class. The stream class is an abstract class and an abstract acts as a type of base class from which other classes can inherit. And just to recap, abstract methods do not have a method body and must be implemented by the class inheriting from it. A method marked as virtual can be overridden but it does not have to be implemented. Methods that are marked as abstract can be overriden.

So the stream class is an abstract base class for a set of other classes that do various operations such as reading content from files, reading files in a directory, etc. Each of the classes that inherit from the base stream class can be seen as a type of stream. Streams can be read from, written to and they support seeking.

The classes that inherit from the Stream class can be divided into three broad categories:

  • Classes that are used for File I/O
  • Classes that are used for reading and writing to streams
  • Common IO stream classes

The reason for taking a look at these classes is because I was looking at a PowerPoint presentation recently. More specifically I was looking at PowerPoint 2007 (pptx) and its underlying XML. If you save a Powerpoint 2007 as XML and you look at the source you will note that there are sections called pkg:binaryData. It got me wondering that maybe that same binary data can be processed in C# code. Take a look at the following sample C# code:

            Image img = Image.FromFile(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            byte[] myByte = File.ReadAllBytes(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            Console.WriteLine(myByte.Length);
            MemoryStream stream = new MemoryStream(myByte);
            int lengthOfByte = Convert.ToInt32(stream.Length);
            byte[] mySecondByte = ReadFully(stream, lengthOfByte);
            Console.WriteLine(mySecondByte.Length);
            Image img2 = Image.FromStream(stream);
            img2.Save(@"C:\Users\fredrike\Documents\LEGAL SUITE3.bmp");

Notice a couple of things I have done in the code. I created a Image class instance and read it from an existing bitmap image. I then create a byte array and used the static ReadAllBytes method supplied by the File class. I then just simply wrote the length of the file to the screen because in the next couple of lines I create a MemoryStream instance using the byte array, after which I also get the length property of the MemoryStream instance because I use it in a function called ReadFully:

        public static byte[] ReadFully(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

I got this from Jon Skeet’s website. Be sure to check out his other C# articles. In my code I then created a second byte array which is generated by the ReadFully method. Once the bytes have finished reading I created a second Image instance, img2 and pass it the MemoryStream instance, and I then call img2′s Save method and create a second image based on the first one.

So my question is, can the binary data from Powerpoint presentations be used in the same way?

  • Share/Bookmark
posted by fr3dr1k in Application Development,C# and have No Comments

For and If statements without the braces

I noted a lot recently that C# developers do not use the curly braces for if and for loops, it seems that the indentation is enough. So if you write this:

                if (elemInner.HasElements)
                    foreach (XElement elemInnerInner in elemInner.Elements())
                        context.Response.Write("
" + elemInnerInner.Name + "
");

Apparently you use curly braces if you have multiple lines you should use curly braces. If you don’t use curly braces only the first line in your condition will be execute.

  • Share/Bookmark
Tags:
posted by fr3dr1k in C# and have No Comments

Enumerating through an XML document

I am in the process of developing some code for easily updating my CV, and one of the technologies I am using for this purpose is Linq to XML. As I have said previously on this blog, Linq to XML is an in-memory XML manipulation technology which allows you to manipulate XML as if you would use the XML DOM. So as part of my application I want to use XML because its easy to create or get the structure needed for creating the CV. I have discussed the idea of a CV Builder previously here. So part of this project involves reading XML and reading XML with LINQ to XML seems a rather straight forward process. The first thing you need to know about though is that the IEnumerable interface you need to use exists in the System.Collections.Generic namespace, not System.Collections. If you use only the latter you will get an error message saying:

The non-generic type ‘System.Collections.IEnumerable’ cannot be used with type arguments

Lets show some code that reads XML:

        XDocument document = XDocument.Load(context.Server.MapPath(@"CVTemplate.xml"));
        XElement root = document.Root;
        IEnumerable elements = from el in root.Descendants("parentsection")
                                             select el;
        foreach (XElement elem in elements)
        {
            builder.AppendLine("Element Name:\t" + elem.Name.ToString());
            builder.AppendLine("Attributes:");
            foreach (XAttribute attrib in elem.Attributes())
            {
                builder.AppendLine("\t\t" + attrib.Value);
            }
            foreach (XElement subElem in elem.Descendants())
            {
                builder.AppendLine("\t\t" + subElem.Name.ToString() + "\n\t\t\t" + subElem.Value);
            }
        }
        context.Response.Write(builder.ToString());

In the code above I use the XDocument class to read the XML from an XML file that I specified in a web.config app setting. I then create an XElement instance by assigning it to the document.Root of the XDocument instance, because its return type is XElement. I then create an enumerable variable elements of type XElement and get the enumerable result from a LINQ query that selects the elements from the XML of the nodes parentsection. I then enumerate through the XElements from the elements variable. Inside the foreach loop I enumerate twice again, once for the attributes of the XML node and the second time for the nested XElements. I just created a StringBuilder instance to write the values as the enumeration took place, its just so that I can see the values and also because I implemented this with a generic handler. Ok so what is next? Well next I need to be able to select items and move them around, but thats for another article.

  • Share/Bookmark
posted by fr3dr1k in C#,LINQ to XML and have No Comments
Get Adobe Flash playerPlugin by wpburn.com wordpress themes