当我尝试使用 DataServiceCollection.loadAsync() 从远程 WCF 数据服务加载数据时,我总是得到一个空集合,比如
CloudEDUEntities ctx = new CloudEDUEntities(uri);
DataServiceCollection<COURSE> dsc = new DataServiceCollection<COURSE>();
var query = from p in ctx.COURSEs select p;
dsc.LoadAsync(query);//The count of dsc will always be 0
dsc.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(complete);
然后我用 loadCompleted 方法检查了状态,发现有错误,比如:
System.InvalidOperationException: An error occurred while processing this request. ---> System.Data.Services.Http.WebException: Unable to connect to the remote server ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions 10.0.1.39:8080
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Client.HttpWebRequestMessage.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Client.ODataRequestMessageWrapper.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Client.WebUtil.GetResponseHelper(ODataRequestMessageWrapper request, DataServiceContext context, IAsyncResult asyncResult, Boolean handleWebException)
at System.Data.Services.Client.WebUtil.EndGetResponse(ODataRequestMessageWrapper request, IAsyncResult asyncResult, DataServiceContext context)
at System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Data.Services.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)
at System.Data.Services.Client.QueryResult.EndExecuteQuery[TElement](Object source, String method, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, String method, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceCollection`1.<>c__DisplayClass2.<LoadAsync>b__1(IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceCollection`1.EndLoadAsyncOperation(Func`2 endCall, IAsyncResult asyncResult)
我高度怀疑这是由服务器引起的问题。但是我仍然可以使用浏览器查看带有 URL 的数据服务。因此,我检查了服务器端,8080端口是开放的,我什至在服务器项目的根目录下添加了一个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>
<socket-resource port="8080" protocol="tcp" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
我关闭了防火墙,但客户端仍然无法成功加载数据。我的服务器在带有 SQL Server 2012 的 Windows Server 2008 上。相同的 WCF 数据服务部署在运行 Windows 7 的服务器上,客户端可以成功地从中加载数据。
我希望有人能帮我找出原因。提前谢谢。
顺便说一句,相同的代码在控制台项目中运行顺利。