silverlight 4数据与通信之WCF详解

在本示例中,我们将通过 WCF 来获取一个最新随笔的列表,在 Silverlight 中显示出来,最终完后效果如下所示。 

一:新建Post类,添加引用

using System.Runtime.Serialization;

System.Runtime.Serialization 命名空间包含可用于将对象序列化和反序列化的类。

定义一个数据契约:

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

namespace sample.Web
{
[DataContract]
public class Post
{
public Post(int id, string title, string author)
{
this.Id = id;
this.Title = title;
this.Author = author;
}
[DataMember]
public int Id { get; set; }

[DataMember]
public string Title { get; set; }

[DataMember]
public string Author { get; set; }
}
}

二:在 Web 项目中添加一个 WCF Service 文件,命名为 Blog.svc 

定义服务契约(在Iblog.cs中):

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

namespace sample.Web
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“Iblog”。
[ServiceContract]
public interface Iblog
{
[OperationContract]
Post[] GetPosts();
}
}

三:实现服务,这里可以是从数据库或者其他数据源读取,为了演示方便,我们直接初始化一个集合(在blog.svc.cs中)

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

namespace sample.Web
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“blog”。
public class blog : Iblog
{
public Post[] GetPosts()
{
List
<Post> posts = new List<Post>()
{
new Post(1,"silverlight 4数据与通信之WCF详解","junyuz"),
new Post(2,"silverlight 4数据与通信之WebRequest详解","junyuz"),
new Post(3,"silverlight 4数据与通信之WebClient详解","junyuz")
};

return posts.ToArray();
}
}
}

四:修改 Web.config 中的服务配置,这里使用 basicHttpBinding 绑定,并且开启 httpGetEnabled,以便后面我们可以在浏览器中查看服务

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>

<system.serviceModel>

<services>
<!-- 注意: 服务名称必须与服务实现的配置名称相匹配。 -->
<service name="sample.blog" behaviorConfiguration="MyServiceTypeBehaviors" >
<!-- 添加下列终结点。 -->
<!-- 注意: 服务必须有一个 http 基址以便添加此终结点。 -->
<endpoint contract="sample.Iblog" binding="basicHttpBinding" address="" />
</service>
</services>


<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- 将下列元素添加到服务行为配置中。 -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>
</configuration>

设置本地IIS服务器,端口为80,在浏览器中输入http://localhost/blog.svc,看看服务是否正常

却出现此错误,当前已禁用此服务的元数据发布,后来通过在网络上搜集资料,却发现都和现有的写法没有本质区别,后来在无意中发现了有人提出一个问题,http://www.chengxy.com/article/47001.html,后来按照提示,为name赋空值,修改后代码如下

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>

<system.serviceModel>

<services>
<!-- 注意: 服务名称必须与服务实现的配置名称相匹配。 -->
<service name="sample.blog" >
<!-- 添加下列终结点。 -->
<!-- 注意: 服务必须有一个 http 基址以便添加此终结点。 -->
<endpoint contract="sample.Iblog" binding="basicHttpBinding" address="" >
</endpoint>
</service>
</services>

<behaviors>

<serviceBehaviors>
<behavior name="" >
<!-- 将下列元素添加到服务行为配置中。 -->
<serviceMetadata httpGetEnabled="true" />

</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>
</configuration>

在浏览器中输入http://localhost/blog.svc,看看服务是否正常

好了,现在服务端我们就实现完成了。

五:现在编写界面展示部分,XAML 如下

<UserControl x:Class="sample.s14"
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"
d:DesignHeight
="300" d:DesignWidth="400" Loaded="UserControl_Loaded">

<Grid Background="#46461F">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Row="0" Grid.Column="0" CornerRadius="15"
Width
="240" Height="36" Background="Orange"
Margin
="20 0 0 0" HorizontalAlignment="Left">
<TextBlock Text="最新随笔" Foreground="White"
HorizontalAlignment
="Left" VerticalAlignment="Center"
Margin
="20 0 0 0"></TextBlock>
</Border>
<ListBox x:Name="Posts" Grid.Row="1" Margin="40 10 10 10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Id}" Height="40" Foreground="Red"></TextBlock>
<TextBlock Text="{Binding Title}" Height="40"></TextBlock>
<TextBlock Text="{Binding Author}" Height="40" Foreground="Orange"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

六:在 Silverlight 项目中添加服务引用,输入地址 http://localhost/Blog.svc,输入命名空间 BlogService。 

七:下面编写调用服务并获取数据,这里仍然是采用异步模式,由于在 WCF 服务的配置中我们采取了 BasicHttpBinding,客户端也要采用 BasicHttpBinding。我们需要注册 GetPostsCompleted 事件处理方法,以便完成后回调,同时调用 GetPostsAsync()方法获取数据。完整的代码如下所示

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 sample.BlogService;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace sample
{
public partial class s14 : UserControl
{
public s14()
{
InitializeComponent();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Binding binding
= new BasicHttpBinding();
EndpointAddress endPoint
= new EndpointAddress( "http://localhost/Blog.svc");
IblogClient client
= new IblogClient(binding, endPoint);
client.GetPostsCompleted
+= new EventHandler<GetPostsCompletedEventArgs>(client_GetPostsCompleted);
client.GetPostsAsync();
}
void client_GetPostsCompleted(object sender, GetPostsCompletedEventArgs e)
{
if (e.Error == null)
{
Posts.ItemsSource
= e.Result;
}
}
}
}

至此,一个完整的在 Silverlight 2 中调用 WCF 的示例就完成了,运行后效果如下:

如果想要源代码,请点击这里事例下载
原文地址:https://www.cnblogs.com/junyuz/p/1959595.html