环境配置:Silverlight 2 tools for visual studio 2008 sp1
VSTS2008 +sp1
MSSQLSERVER2005
启用 Silverlight 的 WCF 模板
您可以通过在 Visual Studio? 中创新建一个 WCF 项目来构建可与 Silverlight 应用程序进行通信的 WCF 服务。
作为创建 WCF 服务的替代方法,您可以在 Visual Studio 中选择文件项目模板来创建启用 Silverlight 的 WCF 服务。 显示的是 Visual Studio 中的新项目模板。据说此模板会自动将绑定设置为 basicHttpBinding 并添加一些属性,以使服务与 ASP.NET 兼容。尽管此方法可为您设置正确的绑定配置,但不要忘记您仍可使用现有的 WCF 服务,但前提是这些绑定是针对 basicHttpBinding 设置的。
只要 Silverlight 应用程序具有 basicHttpBinding 类型的绑定,它就可以调用标准的 WCF 服务。您必须确保自己可将 WCF 服务的默认绑定从 customBinding 更改为 basicHttpBinding,否则必须新建一个 basicHttpBinding 类型的绑定。WCF 服务宿主应用程序的 web.config 文件包含用来定义服务配置的以下 XML。
在我的VS2008英文版中,web.config 为WCF多配置了如下信息;
<behaviors>
<serviceBehaviors>
<behavior name="wcf.WCFBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="wcf.WCFBehavior" name="wcf.WCF">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
contract="wcf.WCF" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
不知道是不是英文版的缘故,<customBinding>应该默认是<basicHttpBinding>,在一些国外的论坛上发现了同样的问题,
而且,关键问题是在客户端的ServiceReferences.ClientConfig文件中,只有</configuration>.这样容易出现“关键字不在数据字典中的问题”,这个问题很明显
OK,那现在来改动一下,因为Silverlight只支持<basicHttpBinding>绑定,所以修改部分代码。
首先,web.config文件修改成:
<behaviors>
<serviceBehaviors>
<behavior name="wcf.WCFBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="customBinding0">
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="wcf.WCFBehavior" name="wcf.WCF">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="customBinding0"
contract="wcf.WCF" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
其实只是两个地方要改动,绑定形式
<bindings>
<basicHttpBinding>
<binding name="customBinding0">
</binding>
</basicHttpBinding>
</bindings>
,和下面的引用部分。
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="customBinding0">
ServiceReferences.ClientConfig 中添加如下:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WCF" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2254/WCF.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_WCF" contract="MyData.WCF"
name="BasicHttpBinding_WCF" />
</client>
</system.serviceModel>
</configuration>
保存就可以测试运行了 ,我们在服务接口中写一个User类来测试
2using System.Linq;
3using System.Runtime.Serialization;
4using System.ServiceModel;
5using System.ServiceModel.Activation;
6using System.Collections.Generic;
7using System.Text;
8
9namespace wcf
10{
11 [ServiceContract(Namespace = "")]
12 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
13 public class WCF
14 {
15 [OperationContract]
16 public int CountUsers()
17 {
18 return 2;
19 }
20 [OperationContract]
21 public IEnumerable<User> GetUser()
22 {
23 List<User> list = new List<User>();
24
25 list.Add( new User() { IsMember = false, Name = "qewerl", Age = 34});
26 list.Add(new User() { IsMember = true, Name = "asdful", Age = 23 });
27 list.Add(new User() { IsMember = true, Name = "Paul", Age = 254 });
28 list.Add( new User() { IsMember = false, Name = "John", Age = 64 });
29 return list.AsEnumerable<User>();
30
31 }
32 }
33
34 [DataContract]
35 public class User
36 {
37 [DataMember]
38 public bool IsMember { get; set; }
39
40 [DataMember]
41 public string Name { get; set; }
42
43 [DataMember]
44 public int Age { get; set; }
45 }
46
47}
接下来看客户端MainPage.xaml.cs。
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Documents;
8using System.Windows.Input;
9using System.Windows.Media;
10using System.Windows.Media.Animation;
11using System.Windows.Shapes;
12using System.Collections.ObjectModel;
13using SilverlightApplication1.MyData;
14
15namespace SilverlightApplication1
16{
17 public partial class MainPage : UserControl
18 {
19 public MainPage()20 {
21 InitializeComponent();
22 WCFClient c = new WCFClient();
23
24 c.GetUserAsync();
25 c.GetUserCompleted += (s, e) =>
26 {
27 ObservableCollection<User> p= e.Result;
28 p.ToList<User>();
29
30 };
31 }
32 }
33}
34
其中的ObservableCollection<User> 就可以绑定到DependencyObject的DataSource了;
上面有省略WCF服务的引用过程,至于绑定部分,网上有很多例子,就不再赘述了。