Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Friday, September 16, 2011

Add Weather Option in your Silverlight App

Preparing weather report in silverlight 4

1) Get the weather update from google.
2) Prepare WebClient For Fetch the XML (using WCF or Silverlight) here i am doing using WCF Data contract Class:

<DataContract()>
Public Class WeatherReport
    <DataMember()>
    Public ForecastInformation As New ForecastInformation
    <DataMember()>
    Public CurrentWeather As New CurrentWeather
    <DataMember()>
    Public WeatherFullReport As New List(Of WeatherFullReport)
    <DataMember()>
    Public Status As String
End Class


<DataContract()>
Public Class ForecastInformation
    <DataMember()>
    Public City As String
    <DataMember()>
    Public PostalCode As String
    <DataMember()>
    Public ForecastDate As String
    <DataMember()>
    Public CurrentDateTime As String
End Class

<DataContract()>
Public Class CurrentWeather
    <DataMember()>
    Public Condition As String
    <DataMember()>
    Public Temp_Fahrenheit As String
    <DataMember()>
    Public Temp_Celsius As String
    <DataMember()>
    Public Humidity As String
    <DataMember()>
    Public icon As String
    <DataMember()>
    Public WindCondition As String
End Class

<DataContract()>
Public Class WeatherFullReport
    <DataMember()>
    Public DayOfWeek As String
    <DataMember()>
    Public Low As String
    <DataMember()>
    Public High As String
    <DataMember()>
    Public icon As String
    <DataMember()>
    Public WindCondition As String
End Class


WCf Function Class:

Public Function GET_WEATHERREPORT(ByVal Location As String) As WeatherReport Implements IService.GET_WEATHERREPORT
        Dim ReturnObj As New WeatherReport
        Dim webClient As New WebClient()
        webClient.Headers.Add("Content-Type", "application/xml")
        webClient.Encoding = Encoding.UTF8
        webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)")
        Try
            Dim Site As String = "http://www.google.com"
            Dim Query As String = Site & "/ig/api?weather=" & Location
            'Dim result As String = webClient.DownloadString(Query)
            Dim reader As New XmlTextReader(Query)
            ' While reader.Read()
            reader.Read()
            reader.Read()
            reader.Read()
            reader.Read()
            If reader.Name = "forecast_information" Then
                reader.Read()
                If reader.Name = "city" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.City = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "postal_code" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.PostalCode = attribute
                    End If
                End If
                reader.Read()
                reader.Read()
                reader.Read()
                reader.Read()
                If reader.Name = "current_date_time" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.ForecastInformation.CurrentDateTime = attribute
                    End If
                End If
                reader.Read()
            End If
            reader.Read()
            reader.Read()
            If reader.Name = "current_conditions" Then
                reader.Read()
                If reader.Name = "condition" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Condition = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "temp_f" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Temp_Fahrenheit = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "temp_c" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Temp_Celsius = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "humidity" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.Humidity = attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "icon" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.icon = Site & attribute
                    End If
                End If
                reader.Read()
                If reader.Name = "wind_condition" Then
                    Dim attribute As String = reader("data")
                    If attribute IsNot Nothing Then
                        ReturnObj.CurrentWeather.WindCondition = attribute
                    End If
                End If
            End If
            reader.Read()
            While reader.Read()
                If reader.Name = "forecast_conditions" Then
                    Dim forecast_conditions As New WeatherFullReport
                    reader.Read()
                    If reader.Name = "day_of_week" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.DayOfWeek = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "low" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.Low = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "high" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.High = attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "icon" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.icon = Site & attribute
                        End If
                    End If
                    reader.Read()
                    If reader.Name = "condition" Then
                        Dim attribute As String = reader("data")
                        If attribute IsNot Nothing Then
                            forecast_conditions.WindCondition = attribute
                        End If
                    End If
                    ReturnObj.WeatherFullReport.Add(forecast_conditions)
                End If
                reader.Read()
            End While
            reader.Read()
            If reader.Name = "problem_cause" Then
                ReturnObj.Status = "No Wearther Report Found For Location: " + Location
            End If
            'End While
        Catch ex As Exception
            ReturnObj.Status = "Weather Report is currently Unavilable"
        End Try
        Return ReturnObj
    End Function


3) Call WCF In silverlight
mobjproxy.GET_WEATHERREPORTAsync("Chennai")
4) Prepare static design and fill the content in WCF Completed

XAML
 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas>

 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas>

 <Canvas Height="322" Width="860">
                    <Canvas Height="33" Name="Canvas2" Width="458" Canvas.Left="251" Canvas.Top="20">
                        <sdk:Label Canvas.Left="0" Canvas.Top="0" Content="Location" Foreground="White" Height="28" Name="Label4" Width="49" />
                        <sdk:AutoCompleteBox Canvas.Left="58" Canvas.Top="2" IsTextCompletionEnabled="True" Name="SearchArea" Width="320" Text="Chennai, Tamil Nadu" />
                        <Button Canvas.Left="380" Canvas.Top="1" Content="Search" Height="27" Name="Find" TabIndex="3" Template="{StaticResource SearchButton}" Width="74" />
                    </Canvas>
                    <sdk:Label Content="Weather" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" Name="Label3" Width="63" Canvas.Left="61" Canvas.Top="20">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                    <Image Name="Image3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" Canvas.Left="8" Canvas.Top="10" Height="59" Width="50" />
                    <Grid Height="254" Name="WeatherGrid" Width="848" Canvas.Left="11" Canvas.Top="59" Visibility="Visible">
                        <sdk:Label Content="Weather for Chennai, Tamil Nadu" FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="33,0,579,204" Name="lblocation" Width="236" VerticalAlignment="Bottom">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <Image Margin="33,69,681,30" Name="CurrentWeatherIcon" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <Rectangle Margin="357,16,488,18" Style="{StaticResource DividerStyle}" />
                        <Image Margin="393,69,378,98" Name="WeatherDayIcon1" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="393,40,378,191" Name="WeatherDay1" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" Foreground="#FFFFCC01" Height="28" Margin="404,162,421,64" Name="WeatherDayHigh1" FontWeight="Bold" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="438,162,389,64" Name="WeatherDayLow1" />
                        <Image Margin="510,69,261,98" Name="WeatherDayIcon2" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="510,40,261,191" Name="WeatherDay2" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="521,162,304,64" Name="WeatherDayHigh2" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="555,162,272,64" Name="WeatherDayLow2" />
                        <Image Margin="621,69,150,98" Name="WeatherDayIcon3" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="621,40,150,191" Name="WeatherDay3" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="632,162,193,64" Name="WeatherDayHigh3" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="666,162,161,64" Name="WeatherDayLow3" />
                        <Image Margin="738,69,33,98" Name="WeatherDayIcon4" Source="/app-name;component/Images/mostly_cloudy_1281.png" Stretch="Fill" />
                        <sdk:Label Content="Thu" FontFamily="Arial" FontSize="12" FontWeight="Bold" Foreground="White" Height="23" HorizontalAlignment="Center" Margin="738,40,33,191" Name="WeatherDay4" Width="77">
                            <sdk:Label.Effect>
                                <DropShadowEffect Opacity="1" />
                            </sdk:Label.Effect>
                        </sdk:Label>
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFFFCC01" Height="28" Margin="749,162,76,64" Name="WeatherDayHigh4" />
                        <sdk:Label Content="36" FontWeight="Bold" Foreground="#FFC7C0C0" Height="28" Margin="783,162,44,64" Name="WeatherDayLow4" />
                        <sdk:Label Content="36 C" FontWeight="Bold" Foreground="#FFFFCC01" Margin="190,69,0,134" Name="CurrentWeatherC" FontSize="16" HorizontalAlignment="Left" Width="40" />
                        <sdk:Label Content="36 F" FontSize="16" FontWeight="Bold" Foreground="White" Margin="267,69,541,134" Name="CurrentWeatherF" />
                        <Rectangle Margin="248,82,599,142" Style="{StaticResource DividerStyle}" Height="30" />
                        <TextBlock Height="98" HorizontalAlignment="Left" Margin="190,126,0,0" Name="CurrentWeather" Text="TextBlock" VerticalAlignment="Top" Width="154" Foreground="White" FontWeight="Bold" FontSize="14" FontFamily="Arial">
                                Scattered Clouds<LineBreak /><LineBreak />
                                Wind: SW at 13 km/h<LineBreak /><LineBreak />
                                Humidity: 62%<LineBreak /><LineBreak /></TextBlock>
                    </Grid>
                    <sdk:Label Canvas.Left="324" Canvas.Top="173" Content="Loading ..." FontFamily="Arial" FontSize="14" FontWeight="Bold" Foreground="#FFFFB100" Height="23" Name="lberror" Width="236">
                        <sdk:Label.Effect>
                            <DropShadowEffect Opacity="1" />
                        </sdk:Label.Effect>
                    </sdk:Label>
                </Canvas> </canvas>

Fill Content in WCF Completed
1) Image is provided by google also but its GIF
2) Get Images from http://www.gettyicons.com/free-icon/126/weather-icon-set/free-sun-little-cloud-flash-rain-icon-png/

 Private Sub GET_WEATHERREPORTCompleted(sender As Object, e As ClientProxy.GET_WEATHERREPORTCompletedEventArgs)
        Dim WeatherReport = e.Result
        Dim Dictionary As New Dictionary(Of String, String)
        Dictionary.Add("http://www.google.com/ig/images/weather/sunny.gif", "sunny.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mostly_sunny.gif", "mostly_sunny.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/partly_cloudy.gif", "partly_cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mostly_cloudy.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_storm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/rain.gif", "rain.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_rain.gif", "chance_of_rain.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_snow.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/cloudy.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/mist.gif", "mist.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/storm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/thunderstorm.gif", "mist.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/chance_of_tstorm.gif", "chance_of_storm.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/sleet.gif", "snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/snow.gif", "snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/flurries.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/icy.gif", "chance_of_snow.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/dust.gif", "haze.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/fog.gif", "cloudy.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/smoke.gif", "haze.png")
        Dictionary.Add("http://www.google.com/ig/images/weather/haze.gif", "haze.png")


        If Not e.Result.ForecastInformation.City Is Nothing Then
            WeatherGrid.Visibility = Windows.Visibility.Visible
            lblocation.Content = "Weather for " & e.Result.ForecastInformation.City
            CurrentWeatherIcon.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.CurrentWeather.icon), UriKind.RelativeOrAbsolute))
            CurrentWeatherC.Content = e.Result.CurrentWeather.Temp_Celsius & ChrW(176) & "C"
            CurrentWeatherF.Content = e.Result.CurrentWeather.Temp_Fahrenheit & ChrW(176) & "F"

            CurrentWeather.Text = e.Result.CurrentWeather.Condition & ChrW(13) & ChrW(13) & _
                                  e.Result.CurrentWeather.WindCondition & ChrW(13) & ChrW(13) & _
                                  e.Result.CurrentWeather.Humidity


            WeatherDay1.Content = e.Result.WeatherFullReport(0).DayOfWeek
            WeatherDayHigh1.Content = e.Result.WeatherFullReport(0).High & ChrW(176) & "F"
            WeatherDayLow1.Content = e.Result.WeatherFullReport(0).Low & ChrW(176) & "F"
            WeatherDayIcon1.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(0).icon), UriKind.RelativeOrAbsolute))

            WeatherDay2.Content = e.Result.WeatherFullReport(1).DayOfWeek
            WeatherDayHigh2.Content = e.Result.WeatherFullReport(1).High & ChrW(176) & "F"
            WeatherDayLow2.Content = e.Result.WeatherFullReport(1).Low & ChrW(176) & "F"
            WeatherDayIcon2.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(1).icon), UriKind.RelativeOrAbsolute))

            WeatherDay3.Content = e.Result.WeatherFullReport(2).DayOfWeek
            WeatherDayHigh3.Content = e.Result.WeatherFullReport(2).High & ChrW(176) & "F"
            WeatherDayLow3.Content = e.Result.WeatherFullReport(2).Low & ChrW(176) & "F"
            WeatherDayIcon3.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(2).icon), UriKind.RelativeOrAbsolute))

            WeatherDay4.Content = e.Result.WeatherFullReport(3).DayOfWeek
            WeatherDayHigh4.Content = e.Result.WeatherFullReport(3).High & ChrW(176) & "F"
            WeatherDayLow4.Content = e.Result.WeatherFullReport(3).Low & ChrW(176) & "F"
            WeatherDayIcon4.Source = New BitmapImage(New Uri("/applicationname;component/Images/Weather/" & Dictionary(e.Result.WeatherFullReport(3).icon), UriKind.RelativeOrAbsolute))
            lberror.Content = e.Result.Status
        Else
            WeatherGrid.Visibility = Windows.Visibility.Collapsed
            lberror.Content = "Invalid location!"
        End If
    End Sub

Thursday, June 16, 2011

Windows Communication Foundation (WCF)

INTRODUCTION:
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. A few sample scenarios include:

* A secure service to process business transactions.
* A service that supplies current data to others, such as a traffic report or other monitoring service.
* A chat service that allows two people to communicate or exchange data in real time.
* A dashboard application that polls one or more services for data and presents it in a logical presentation.
* Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.
* A Silverlight application to poll a service for the latest data feeds.

While creating such applications was possible prior to the existence of WCF, WCF makes the development of endpoints easier than ever. In summary, WCF is designed to offer a manageable approach to creating Web services and Web service clients.

WHY WCF?

In this WCF tutorial we introduce the primary reasons for moving from other technologies to WCF as well as how to get started using WCF. Prior to .Net 3.0 it was not an easy matter to select a particular technology for communicating between systems due to the number of technologies available from Microsoft. For example, users could have used Web Service to communicate between a Java based application and a .Net application; WSE users could have take advantage of some of the WS-* message protocols; MSMQ has the ability to queue messages which helps to communicate between intermittently connected solutions; Enterprise services (the successor of COM+) helps to build distributed application easily; .Net Remoting is a fast way to move messages between two .NET applications. All the above mentioned technologies have their pros and cons. Using WCF now we can take the advantage of all the above distributed technologies in a unified manner and WCF is the successor to all these message distribution technologies.

WCF Model
A WCF service is made up of three parts: the service, one or more endpoints and a hosting environment.
A service is basically a class written in a .Net compliant language which contains some methods that are exposed through the WCF service. A service may have one or more endpoints – an endpoint is responsible for communication from the service to the client.
Endpoints also have three parts which are known as ‘ABC’: ‘A’ for Address, ‘B’ for Binding and ‘C’ for Contracts.
Address: Address specifies the info about where to find the service.
Binding: Binding specifies the info for how to interact with the service.
Contracts: Contracts specifies the info for how the service is implemented and what it offers.
Finally a hosting environment where the service is contained.
WCF bindings: System-provided WCF bindings are used to specify the transport protocols, encoding, and security details required for clients and services to communicate with each other. As per MSDN followings are the system-provided WCF
bindings:
BasicHttpBinding: A binding that is suitable for communication with WS-Basic Profile conformant Web Services like ASMX-based services. This binding uses HTTP as the transport and Text/XML as the message encoding.
WSHttpBinding:
A secure and interoperable binding that is suitable for non-duplex service contracts.
WSDualHttpBinding: A secure and interoperable binding that is suitable for duplex service contracts or communication through SOAP intermediaries.
WSFederationHttpBinding: A secure and interoperable binding that supports the WS-Federation protocol, enabling organizations that are in a federation to efficiently authenticate and authorize users.
NetTcpBinding:
A secure and optimized binding suitable for cross-machine communication between WCF applications
NetNamedPipeBinding:
A secure, reliable, optimized binding that is suitable for on-machine communication between WCF applications.
NetMsmqBinding:
A queued binding that is suitable for cross-machine communication between WCF applications.
NetPeerTcpBinding :
A binding that enables secure, multi-machine communication.
MsmqIntegrationBinding :
MsmqIntegrationBinding: A binding that is suitable for cross-machine communication between a WCF application and existing MSMQ applications.

Distributed:
With the advent of WCF, communication between applications and systems has become much easier. WCF is a means to build distributed applications in a Microsoft environment, clients however may or may not use any Microsoft technology in order to consume WCF services.

Hosting of WCF Service
WCF service cannot exist in Void.
Every WCF service must be hosted in a Windows process called the Host process.
IIS Hosting
Main advantage of hosting a service in IIS is Host process is launched automatically upon the first client request.
Disadvantage is that only HTTP protocol could be used.
Another disadvantage is all the service will run on the same port.
Here Host Process life cycle is managed by the IIS.
Self Hosting
This requires adding a managed code to host the process.
The code could be either a Windows form application or Console application.
Here host process must be running before client makes a call to the service.
Mainly Console applications are used for the purpose of self hosting.
The Console/Windows application must specifically create and open an instance of ServiceHost object.
The ServiceHost then remains open and available until it is no longer needed.
Configuration about the Service is being kept in the App.Config file.