2012-04-29

Web services(soap). This takes me back to 2005, in a project where we thought it was the solution for all the problems.
How wrong were we?

But to be fair, it is not all that bad. I remember back in 2004 when I did a college coursework consuming a .net webservice with a java application. The idea was awesome, but it was so painful I thought it would never be any good.
Now I had to do another coursework. Not for college degree, but anyway I had to build a website, and then convert it to a web service.
But this time I thought: I don't want to build another project to consume it, it will be just a waste of time.

Solution: JQuery. Yes, the solution for so many web questions is the answer once again...

And it was quite easy.
You just have to send it as xml, and build a xml envelope to send as data. Sounds hard?
Well, you will see how easy it is.
To build the envelope all you have to do is to access the webservice you generated, in my case Java.
As you can see, the SOAP request is already done for you, it's only a matter of copy and replace the values.
var soapMessage = '<?xml version="1.0" encoding="UTF-8"?>'
                    + '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body>'
                    + '    <ns2:listCustomers xmlns:ns2="http://webservice/">'
                    + '    </ns2:listCustomers>'
                    + '</S:Body></S:Envelope>';
                $.ajax({
                    type: "POST",
                    url: "./WSManager",
                    data: soapMessage,
                    contentType: "text/xml; charset=\"utf-8\"",
                    success: function(msg) { $("#customerList").html(msg.childNodes[0].textContent); }
                });


But the thing is, why to use soap?
Seriously, I can't see any reason in that. REST is so much better, is clean, nice, you can have it with json instead of the verbose xml, you don't have to send a whole xml as request.

I'll write some post about rest later, because now it is late and I've got to sleep.

see ya

2012-04-10

To log or not to log?

To log or not to log?

You see, to log is mandatory if your software does more than a hello world. But the thing is, I've always thought it wasn't cool to stuff chunks of code all over the code, it makes the code ugly, and less readable.
I was using Log4Net, and it is not all bad, but as I said before, I don't like my code "dirty".

So today a friend of mine told me about the ELMAH, and I thought it is amazing. You might say it isn't that clean, if you want to log something you have to write code to log.
Of course you have, how else it is supposed to guess what you want to log???
But that's not even the most important part, the log caught errors and log them in a very nice way.

As I was trying to set aside a few hours to install and configure it, I've remembered one old Scott Hanselman's video about the .net gems nugets, and how easy it was to set up the addons using nugets.


This works like a charm, you can have it all in 3 simple steps:
  1. Install the ELMAH nuget.
  2. Create a database for it and run the sql file App_Readme/Elmah.SqlServer.sql
  3. Update the ELMAH connection string  in your web.config, just look for the already created key: <add name="elmah-sqlserver" connectionString="Data Source=****;User ID=****;Password=****;Initial Catalog=****;" providerName="System.Data.SqlClient" /> and then insert values about the database you just created.
And that's it, done, Easy Peasy...

Ah, and No, you don't have to look database records, or a shitty unformated text file...
Just access yourapp/elmah.axd and you will see something like this:


And clicking details, will be something like this:


Go ahead and start feeling guilty for logging all your mistakes. Forever. Because it doesn't forgive, and it never forgets...