Friday, March 7, 2008

Getting ready for Silverlight 2.0 - Part 1

The Silverlight 2 Beta 1 is here. It now includes support for mobile devices and is another step towards helping developers create Rich Internet Applications (RIA). Silverlight 2 includes a cross-platform, cross-browser version of the .NET Framework, and enables a rich .NET development platform that runs in the browser. Developers can write Silverlight applications using any .NET language now (including VB, C#, JavaScript, IronPython and IronRuby) as opposed to having to learn JavaScript as with previous versions. WHat's more it does not require the .NET Framework to be installed on a computer in order to run. The Silverlight setup download includes everything necessary to enable all Silverlight features on a Mac OSX or Windows machine.

So let's get things started:
  1. Install the .NET Framework 3.5 (if you have VS2008 this is not necessary, you have it already) first before you install Expression Blend 2.5.
  2. Install Silverlight 2 Beta 1
  3. Install Expression Blend 2.5 March 2008 Preview (25 megabytes)

My next post will take you through creating you first project.

Customising the WebService Help Page

I remember someone asking this question when I was in a course a long while back and the trainer had said, yeah it can be done,but he didn't tell us how :)

So some years later I'm gonna show you how.

The trick is in the Web.Config. Inside System.web you can set up a WebServices Tag and then include a wsdlHelpGenerator Tag to place your settings. Some options are now available:

Turn of ALL WSDL and Help!!
<System.Web>
<webServices>
<protocols>
<remove name="Documentation"/>
</protocols>
</webServices>

Or you can point it to your own file:
<webServices>
<wsdlHelpGenerator href="Default.aspx"></wsdlHelpGenerator>
</webServices>


But like this you have to do the whole page again yourself, there must be an easier way to just add some custom bits like a compnay logo, contact details etd, well there is. If you dig around, %Windows%\Microsoft.NET\Framework\v2.0.50727\CONFIG you'll find a file called DefaultWsdlHelpGenerator.aspx. Now you know what you have to do: Copy it and customise it :) if you want just your webservice changed, or alter it to alter all WebService DOcumentation across the whole machine!!

Adding a WebReference to a Windows Forms Application in VS2008

I'm on a blog posting frenzy this week, since I'm not training :) What with country elections rapidly approaching this weekend!!

So adding a webreference was very straight forward in VS2005, just right click the project and select Add WebReference.

In VS2008 it is still easy if you are making the reference from a Website ASP.NET app, but try and do it from Windows!!! This is usually one of my favourite demos to show how VS makes it so easy for a windows forms to consume data server across the web. Well we all know that SOA is upon us and so the asnwer is in just that. Ideally we would have a WCF app to consume, not the Webservice directly.

We could go down this route but I will save that for another post ;) To do it the 'old way' we start with a right click on the project and select Add Service Reference. You should now see a new window like this.


Two options exist: The WS way or the WCF way.
  • Clicking Discover will find the Webservice in the solution and create a Service reference that must be consumed via the WCF route.
  • The other way is to click Advanced at the bottom and then select WebService. The rest should be very familiar!!


    What's really interesting is that fact that once you have one WebService Reference you can now do it the old way!!! Right click and you get the image below !!


Wednesday, March 5, 2008

Thank you ProcessMeNtOR for the Bottle Warmer!!


I've been meaning to post this since I got back from Barcelona! These cool thermos beer bottle coolers are great at keeping our baby's bottles warm for atleast 4 hours!! My wife loves them and wants more!! :)
Thank you ...

Creating a One-Way Webservice to improve responsiveness

By default a call to a webmethod will wait for the webservice method to complete the whole webmethod, even if it does not return a value. Now you could invoke the method Asynchronously, but considering you don't care what comes back, because nothing its gonna come back why bother? Well let's see how you can make the method one way, so processing resumes on the client straight after the call to the method. Consider the following:

<SoapDocumentMethod(Oneway:=True)> _
<WebMethod()> _
Public Sub Oneway()
Threading.Thread.Sleep(10000)
End Sub

<WebMethod()> _
Public Sub Twoway()
Threading.Thread.Sleep(10000)
End Sub

And then we would call theses like this:

Dim proxy As New localhost.Service()
Dim dtStart As New Date
dtStart = Now
proxy.Oneway()
MessageBox.Show(DateDiff(DateInterval.Second, dtStart, Now))
dtStart = Now
proxy.Twoway()
MessageBox.Show(DateDiff(DateInterval.Second, dtStart, Now))

When you run the code the one way method returns instantly, no delay, the two way will wait 10 seconds and stall the client app!!

Using Session State in a Webservice AND calling from a Windows Form

So setting up a WebMethod to use Session is not that difficult:

<WebMethod(EnableSession:=True)> _
Public Function HelloWorld() As String

If Session("FirstCall") Is Nothing Then
Session("FirstCall") = Now
End If


Return Session("FirstCall")
End Function

And calling this from a webform is not a problem since the browser can handle session information in a cookie etc. However it gets just a little tricky when you call it from a windows form because it has no idea about cookies. So you have to take care of it in the call like this:

Private Cookies As System.Net.CookieContainer

Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

Dim proxy As New localhost.Service()
' Set the Cookie Container on the proxy
If Cookies Is Nothing Then
Cookies = New System.Net.CookieContainer()
End If
proxy.CookieContainer = Cookies
MessageBox.Show(proxy.HelloWorld)

End Sub


A great explanation can be found here: http://msdn2.microsoft.com/en-us/library/aa480509.aspx

Caching a WebService Call

<WebMethod(CacheDuration:=10)> _
Public Function HelloWorld() As String
Return "Hello World" & Now
End Function


The duration is defined in seconds.