接上面的文章,我们已经发布了Silverlight程序,我在这个Silverlight 程序里面使用了WCF RIA Service,现在我又开发了另外一个程序,需要用到这个服务,该如何去调用这个服务呢?这里我们不直接引用项目原程序,而只是引用WCF RIA Service Class Library的程序集dll。
其中在解决方案文件夹里面的是WCF RIA SERVICE Class Library。EnterpriseTest和EnterpriseTest.web是Silverlight项目。在前面的文章里面,我们已经把这个程序发布到IIS。
我们先看看这个项目WCF RIA SERVICE生成的代码。在Generated_Code文件夹下面。打开**.**.g.cs类。你可以看到生成的一系列代码,下面是其中的一部分:
/// <summary>
/// The domain context corresponding to the 'CustomerService' domain service.
/// </summary>
public sealed partial class CustomerContext : DomainContext
{
#region Extensibility Method Definitions
/// <summary>
/// This method is invoked from the constructor once initialization is complete and
/// can be used for further object setup.
/// </summary>
partial void OnCreated();
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="CustomerContext"/> class.
/// </summary>
public CustomerContext() :
this(new WebDomainClient<ICustomerServiceContract>(new Uri("EnterpriseTest-AppServices-Web-CustomerService.svc", UriKind.Relative)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomerContext"/> class with the specified service URI.
/// </summary>
/// <param name="serviceUri">The CustomerService service URI.</param>
public CustomerContext(Uri serviceUri) :
this(new WebDomainClient<ICustomerServiceContract>(serviceUri))
{
}
}
EnterpriseTest-AppServices-Web-CustomerService.svc,这个地址是由DomainService类的完整地址,.都用-代替。这个是WCF SERVICE的地址。在IE地址栏输入地址http://localhost/Test/EnterpriseTest-AppServices-Web-CustomerService.svc。
我们的SILVERLIGHT程序调用了这个服务,我们使用HTTP WATCH工具可以看到SILVERLIGHT详细请求服务的过程。
看看这个地址http://localhost/Test/ClientBin/EnterpriseTest-AppServices-Web-CustomerService.svc/binary/GetCustomers。这个是调用GETCUSTOERS方法,返回一些客户信息。在地址栏输入会提示下载数据。如上面的描述,我们可以正常的访问服务。那我们现在新建一个Silverlight 程序,名字叫ERP。
在ERP项目引用EnterpriseTest.AppServices.dll和System.ServiceModel.DomainServices.Client.dll
在MainPage.xaml添加DataGrid控件。MainPage.xaml.cs代码如下:
using System;
using System.Windows.Controls;
using EnterpriseTest.AppServices.Web;
using System.ServiceModel.DomainServices.Client;
namespace ERP
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
EnterpriseTest.AppServices.Web.CustomerContext cc = new EnterpriseTest.AppServices.Web.CustomerContext(new Uri("http://localhost/Test/EnterpriseTest-AppServices-Web-CustomerService.svc"));
this.dataGrid1.ItemsSource = cc.Customers;
LoadOperation<Customer> loadOp = cc.Load(cc.GetCustomersQuery());
}
}
}
这段代码很简单,就是调用WCF服务绑定数据。那我们运行程序。先按F5.
报出跨域的异常。于是我GOOGLE下,网上的解决方案是在WCF程序下面加入下面的XML:
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomainpolicy.xml
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="* " headers="*" />
</cross-domain-policy>
我于是将上面的XML文件添加到EnterpriseTest目录下面。结果运行项目还是出现跨域的问题。纳闷!因为最近用上了HTTPWATCH,还有Fiddler。所以习惯去看网页的请求过程。查看Fiddler,如下图:
请求XML的地址是http://localhost/clientaccesspolicy.xml,难怪还跨域。也不知道为什么会请求这个地址去了,是不是我没有把ERP的程序发布到IIS,而是直接使用VS自带的IIS呢。于是将这两个XML放到C:\inetpub\wwwroot下面。再次运行程序:
数据出来了。
参考资料:http://blogs.msdn.com/b/silverlightws/archive/2008/03/30/some-tips-on-cross-domain-calls.aspx