WCF 与 WCF RIA Services

WCF具体使用方法:

1)WCF只需要在Web项目里添加WCF Service,然后再IWCFServices里声明是什么契约的方法,例如[ServiceContract]声明接口里的[OpeerationContract]声明方法。

    [ServiceContract]
    public interface IWCFService
    {
        [OperationContract]
        void DoWork();
        [OperationContract]
        Log[] GetLog();
    }


 

2) 然后再WCFService.svc.cs里对上面的接口进行实现,coding相应的方法的具体实现。

    public class WCFService : IWCFService
    {
        public void DoWork()
        {
        }


        public Log[] GetLog()
        {
            //实现接口方法GetLog
            Log[] log = 
            {
                new Log("今天天气不错"),
                new Log("Silverlight不好学"),
                new Log("WCF返回值")
            };
            return log;
        }
    }


 

3) 在Client端例如Silverlight程序端,“Add Service Reference ”把WCFServiceReference添加到该Client端项目中。之后在CS文件里这样调用

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //创建WCF服务调用的代理对象
            WCFServiceReference.WCFServiceClient proxy = new WCFServiceReference.WCFServiceClient();
            //声明调用完成的事件处理
            proxy.GetLogCompleted += new EventHandler<WCFServiceReference.GetLogCompletedEventArgs>(proxy_GetLogCompleted);
            //开始异步调用
            proxy.GetLogAsync();
        }

        void proxy_GetLogCompleted(object sender, WCFServiceReference.GetLogCompletedEventArgs e)
        {
            lstWCF.ItemsSource = e.Result;
        }


WCF RIA Services的具体使用方法:

1)  在web端项目中添加“ADO.NET Entity Data Model”,然后创建所要用到的数据库中的表的实体数据模型Model1.edmx需要点击F6编译运行一下该实体数据模型,以便下面使用。

2)添加DomainService,所谓的DomainService域服务是Silverlight与ADO.NET实体数据模型打交道的中间层或者中介物质。在该Web项目中添加一个“Domain Service Class" ->DomainService1.cs类。

3) 添加完域服务后,必须编译一下该项目已备其他项目使用。

4)具体到使用WCF RiaService -> SIlverlight XAML代码为MainPage.xaml:

<UserControl x:Class="RiaSample.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"
             xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <Style x:Key="txt" TargetType="TextBlock">
                <Setter Property="FontSize" Value="15"/>
                <Setter Property="Foreground" Value="Black"/>
                <Setter Property="Margin" Value="5"/>
            </Style>
        </Grid.Resources>
        <data:DataGrid x:Name="mydataGrid" IsReadOnly="True"
                       AlternatingRowBackground="Gray"
                       Margin="15" Background="White"
                       Foreground="Black" BorderBrush="Black">
            <!--DataGrid控件的RowDetailTemplate属性允许用户自定义
            每行的明细模板,当点击某一行时就可以显示详细的产品明细-->
            <data:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <StackPanel Background="White"
                                Orientation="Horizontal">
                        <TextBlock Text="品牌:"
                                   Style="{StaticResource txt}"/>
                        <!--下面所Binding的是在创建edmx实体对象的时候自动生成的属性(也就是列的列名)-->
                        <TextBlock Style="{StaticResource txt}"
                                   Text="{Binding ProductName}"/>
                        <TextBlock Text="供应商ID:"
                                   Style="{StaticResource txt}"/>
                        <TextBlock Text="{Binding SupplierID}"
                                   Style="{StaticResource txt}"/>
                    </StackPanel>
                </DataTemplate>
            </data:DataGrid.RowDetailsTemplate>
        </data:DataGrid>
    </Grid>
</UserControl>


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 RiaSample.Web;

namespace RiaSample
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindData();
        }

        private void BindData()
        {
            DomainService1 ds = new DomainService1(); //实例化DomainService域服务
            //把域服务指向的edmx实体数据模型Products类赋值给DataGrid的ItemsSrouce,作为DataGrid的数据源
            mydataGrid.ItemsSource = ds.Products;  
            //DomainService域服务中已经自动生成了Product的查询方法GetProductsQuery
            //这里秩序直接使用该方法就行,该方法是在创建DomainService时候自动生成 
            ds.Load(ds.GetProductsQuery());

            //还可以进行条件查询例如值要CatelogID为1的产品列表,这时在
            //把DomainService1.cs里的public IQueryable<Products> GetProducts()方法体内修改为
            //public IQueryable<Products> GetProducts()
            //{
            //    return this.ObjectContext.Products.Where(p => p.CategoryID == 1);
            //}
        }
    }
}


 个人理解他们之间的区别是:

WCF RIA Service是WCF大类中的一种,只不过WCF RIA Service会自动生成很多辅助的代码,例如对标的增删改查代码,WCF RIA Service更有利于与Silverlight结合使用。

 

WCF 和 Domain Services

domain service是建立在WCF概念上的。domain service保留了下面的特点:

  • WCF Service的标准使用方式。
  • 以有的WCF编程模型结构,例如操作合约,操作行为,以及服务行为。
  • 标准的WCF定制功能,例如绑定配置、行为配置等

domain context和domain service的交流是通过使用WCF ChannelFactory创建一个通道,并向它传递一个从domain service生成的服务合约。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值