vs2010 学习Silverlight学习笔记(13):数据与通信之WCF

概要:

  初次接触WCF,结果惨不忍睹。高手飘过,新手的去看TerryLee原版吧一步一步学Silverlight 2系列(14):数据与通信之WCF。
  这个就留给我当做以后复习的笔记吧。

WCF:

  既然是用WCF通信,那么首先在Web文件里建立WCF-Service,命名为Blog.svc。然后web文件夹就会多出Blog.svc和IBlog.cs。
开头有I的,一般都是接口。IBlog.cs里有个默认的void DoWork();同样在Blog.svc就有个DoWork的实现方法。
  例子中就是在接口中声明一个类,返回Post[]类型。在Blog.svc有这个例子的具体方法。
  这个还好理解。
  然后在web.config中修改:
代码
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightAppDemo1.Web.BlogBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="SilverlightAppDemo1.Web.BlogBehavior"
name
="SilverlightAppDemo1.Web.Blog">
<endpoint address="" binding="basicHttpBinding" contract="SilverlightAppDemo1.Web.IBlog">
</endpoint>
</service>
</services>

</system.serviceModel>
这里使用basicHttpBinding绑定,并且开启httpGetEnabled,以便后面我们可以在浏览器中查看服务。
Post类:
代码
[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; }
}
[DataContract]是WCF中定义数据契约。其实就是将字段序列化。
那么IBlog.cs就叫做服务契约。——契约这个东西还是第一次在编程中听到。
 

Silverlight对WCF的引用:

  MainPage.xaml:

 

代码
<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>

 

这个倒没什么,关键是MainPage.xaml.cs文件:
代码
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Binding binding
= new BasicHttpBinding();
EndpointAddress endPoint
= new EndpointAddress(
"http://localhost:52424/Blog.svc");

BlogClient client
= new BlogClient(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;
}
}
还有命名空间:
using System.ServiceModel.Channels;
using System.ServiceModel;
using SilverlightAppDemo14.BlogService;
前两个有时报错,要手引入:

第三个要引入Service Reference。引入前,你需要设定web的端口为固定。而且不能重复引入。
引入的时候一直报错,后来将+点开才引入成功。后来实验几次,完全靠运气才成功。最后也没搞懂
为什么。
最后总算成功了,不过页面打开时很慢。而且数据过一会才能显示。一个可能是虚拟机性能问题,还有一个可能是因为加载时才
开始异步读取数据的原因。

总结:

  一段很简单的代码,也不复杂的逻辑,弄了3个多小时才运行出来,悲剧了。
不过总算见识到了什么是WCF了,第一次哦。
总目录
上一篇:vs2010 学习Silverlight学习笔记(12):数据与通信之WebRequest
下一篇:vs2010 学习Silverlight学习笔记(14):数据与通信之ASMX

 


posted @ 2010-05-18 22:42  耀哥  阅读(1935)  评论(2编辑  收藏  举报