Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

WCF 4.0 service consumed in Silverlight 4.0 with cross domain

Sep 16 2010 7:52AM by Dhananjay Kumar   

Objective

This article will explain how to call WCF 4.0 service hosted in IIS 7.5 in a Silverlight application. This article will explain the way to avoid cross domain problem also.

Expected Output

clip_image001

When user will click on + button a WCF service hosted on IIS will get called. As a input parameter to service value from two text box will be passed and result from the service will get display in result text box.

clip_image002

Create WCF Service

Create WCF service. Open visual studio select new project and then from WCF tab select WCF Service application to create a new WCF service.

clip_image004

Delete the default code created by WCF from IService1 and Service1.svc.cs

  1. So delete the data contract
  2. Modify the service contract as below. Just make one operation contract to return a string.

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService5
{
    [ServiceContract]
    publicinterfaceIService1
    {
        [OperationContract]
        string  GetMessage(string  number1 , string number2);

    }   
}

Now implement the service as below,

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService5
{
    publicclassService1 : IService1
    {
        publicstring GetMessage(string number1, string number2)
        {
            return (Convert.ToInt32(number1) + Convert.ToInt32(number2)).ToString(); 
        }
    }
}

Since, we are creating WCF Service for SilverLight client, so we need to modify the endpoint of the service with basicHttpBinding.

As we know SilverLight supports either basicHttpBinding or webHttpBinding. webHttpBinding is essentially for REST based service . We are going to use basicHttpBinding to enable SOAP based WCF service for SilverLight client.

<services>
      <servicename ="WcfService3.Service1"behaviorConfiguration ="svcbh">
        <host>
          <baseAddresses>
            <addbaseAddress = "http//localhost:9000/Service1/" />
          </baseAddresses>
        </host>
        <endpointname ="duplexendpoint"
                  address =""
                  binding ="wsDualHttpBinding"
                  contract ="WcfService3.IService1"/>
        <endpointname ="MetaDataTcpEndpoint"
                  address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>
  1. Declare the end point with basicHttpBinding.
    <endpointname ="duplexendpoint"
              address =""
              binding ="wsDualHttpBinding"
              contract ="WcfService3.IService1"/>
  2. Declare the Meta data exchange end point.
    <endpointname ="MetaDataTcpEndpoint"
              address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange"/>
  3. Define the service behavior
    <serviceBehaviors>
      <behaviorname ="svcbh">
        <serviceMetadatahttpGetEnabled="true"/>
          <serviceDebugincludeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>

So the service will look like

<services>
  <servicename ="WcfService5.Service1"behaviorConfiguration ="svcbh">
    <endpoint name ="SilverLightendpoint"
              address =""
              binding ="basicHttpBinding"
              contract ="WcfService5.IService1"/>
    <endpoint name ="MetaDataSilverlightEndpoint"
              address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange"/>
  </service>
</services>

Web.Config

<?xmlversion="1.0"?>
<configuration>
<system.web>
  <compilationdebug="true"targetFramework="4.0" />
</system.web>
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behaviorname ="svcbh">
        <serviceMetadatahttpGetEnabled="true"/>
          <serviceDebugincludeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <servicename ="WcfService5.Service1"behaviorConfiguration ="svcbh">
      <endpoint name ="SilverLightendpoint"
                address =""
                binding ="basicHttpBinding"
                contract ="WcfService5.IService1"/>
      <endpoint name ="MetaDataSilverlightEndpoint"
                address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange"/>
    </service>
  </services>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
</system.serviceModel>
<system.webServer>
  <modulesrunAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

Hosting in IIS and solving the cross domain problem

Host the service in IIS. Please read here step by step to host WCF 4.0 Service in IIS 7.5

Solving cross domain problem

  1. Right click on WCF Service project and add new item. Select XML file from Data tab.

    clip_image012
  2. Give the name of the XML file clientaccesspolicy.xml

    clip_image014
  3. Add the below markup in ClientAcccessPolicy.xml file

    clip_image016

    So, finally now you will have a ClientAccesspolicy.xml file in WCF project

    ClientAccesspolicy.xml

    <?xmlversion="1.0"encoding="utf-8" ?>
    <access-policy>
      <cross-domain-access>
        <policy>
          <allow-fromhttp-request-headers="SOAPAction,Content-Type">
            <domainuri="*" />
          </allow-from>
          <grant-to>
            <resourcepath="/"include-subpaths="true"/>
          </grant-to>
        </policy>
      </cross-domain-access>
    </access-policy>
  4. Now open WCF Service project in windows explorer. To do this right clicks on the project and select open folder in Window explorer.

    clip_image018

    Copy clientaccesspolicy.xml file and paste it in C:\inetpub\wwwroot. Make sure in wwwroot folder clientaccesspolicy.xmlfile exist.

Consuming in SilverLight 4.0

  1. Create a Silver Light project by Project->new ->SilverLight -> SilverLight Application

    image

    And create a web site to host Silver light application.

    image
  2. Now design the page in XAML.

    clip_image022

    MainPage.xaml

    <UserControl x:Class="SilverlightApplication3.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d">
    <Grid x:Name="LayoutRoot" Background="Gray" Height="200" Width="300">
      <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="2*" />
      </Grid.RowDefinitions>
      <TextBox x:Name="txtResult" Height="40" Text="" Margin="12,14,22,14" />
      <StackPanel Orientation="Horizontal" Grid.Row="1" >
        <Canvas>
          <TextBox x:Name="txtNumber1" Height="40" Width="99" Canvas.Left="20" Canvas.Top="30"/>
          <Button x:Name="btnAdd" Height="40" Width="50" Content="+" Canvas.Left="120" Canvas.Top="30" />
          <TextBox x:Name="txtNumber2" Height="40" Width="99" Canvas.Left="170" Canvas.Top="30"/>
        </Canvas>
      </StackPanel>
     </Grid>
    </UserControl>
    Explanation
    1. Divided the main grid in two rows.
    2. In first row put a textbox to display the result.
    3. In second row put a stack panel with horizontal orientation.
    4. Put a canvas inside stack panel
    5. Put two text box and button in canvas.
  3. Right click on SilverLightproject and add Service Reference.

    clip_image023

    Give the URL of the WCF service hosted in IIS in Address text box. And then click GO.

    clip_image024

  4. Calling the Service

    Add namespace

    using SilverlightApplication3.ServiceReference1;

    On click event of the button

    Service1Client proxy = newService1Client();
    proxy.GetMessageCompleted += delegate(object sender1, GetMessageCompletedEventArgs e1) { txtResult.Text = e1.Result;};
    proxy.GetMessageAsync(txtNumber1.Text ,txtNumber2.Text);           

Explanation

  1. Silverlight calls WCF service asynchronously
  2. So first completed event is getting called. If operation contract name is GetMessage then event name will be GetMessageCompleted.
  3. After event aysnc function is being called.

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication3.ServiceReference1; 

namespace SilverlightApplication3
{
    publicpartialclassMainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            btnAdd.Click += newRoutedEventHandler(btnAdd_Click);
        }
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {

            Service1Client proxy = newService1Client();
            proxy.GetMessageCompleted += delegate(object sender1, GetMessageCompletedEventArgs e1) { txtResult.Text = e1.Result;}; 
            proxy.GetMessageAsync(txtNumber1.Text ,txtNumber2.Text);           
        }
    }
}

Output

clip_image001[1]

Conclusion

In this article we saw how we can consume WCF Service in Silverlight application with cross domain. I hope this article was useful. Thanks for reading. Happy Coding

Tags: #DOTNET, DOTNET, #ASP.NET, BRH, #SILVERLIGHT, SILVERLIGHT,


Dhananjay Kumar
49 · 4% · 1198
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]