当我尝试使用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的dataservice。因此,我检查了服务器端,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>
我关闭了防火墙,但客户端仍然无法成功加载数据。我的服务器在Windows Server 2008上,带有SQL Server 2012。 在Windows 7上运行的服务器上部署了相同的WCF Dataservices,客户端可以从中成功加载数据。
我希望有人能帮助我找出原因。 Thx提前。
BTW,相同的代码在控制台项目中顺利运行。
答案 0 :(得分:1)
根据您的描述,我认为没有IP矛盾或端口占用,因为相同的代码在您的控制台项目中顺利运行。您应该检查Windows应用商店应用的.appxmanifest文件,并确保已选中 capibilities 标签中的专用网络(Clien&amp; Server)。
专用网络(客户端和服务器)功能通过防火墙提供对家庭和工作网络的入站和出站访问。此功能通常用于通过局域网(LAN)进行通信的游戏,以及用于跨各种本地设备共享数据的应用程序。