【问题标题】:Using WCF Data Services (ODATA) operations in Windows Phone development在 Windows Phone 开发中使用 WCF 数据服务 (ODATA) 操作
【发布时间】:2012-09-11 12:22:08
【问题描述】:

我正在开发一个 Windows Phone 应用程序,该应用程序需要使用来自 OData 源的数据(使用 WCF 数据服务)。具体来说,我需要调用如下操作

http://localhost/ODATA/PIDataService.svc/RecordedValues?path='\\OPENXC\OpenXC\EventFrames[testFrame]|Sinusoid'&startTime='*-1d'&endTime='*'

当我在浏览器 (Chrome) 中执行此操作时,它会给我正确的结果

<feed xml:base="http://[redacted]/OData/PIDataService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">RecordedValues</title>
<id>http://[redacted]/ODATA/PIDataService.svc/RecordedValues</id>
<updated>2012-09-11T12:09:12Z</updated>
<link rel="self" title="RecordedValues" href="RecordedValues"/>
<entry>
<id>http://[redacted]/OData/PIDataService.svc/TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T13%3A31%3A09Z')</id>
<title type="text"/>
<updated>2012-09-11T12:09:12Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="TimeSeriesValue" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T13%3A31%3A09Z')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UOM" type="application/atom+xml;type=entry" title="UOM" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T13%3A31%3A09Z')/UOM"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SourceAttribute" type="application/atom+xml;type=entry" title="SourceAttribute" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T13%3A31%3A09Z')/SourceAttribute"/>
<category term="vCampus.OData.Web.ObjectModel.TimeSeriesValue" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:Timestamp m:type="Edm.DateTime">2012-09-10T13:31:09Z</d:Timestamp>
<d:SourceAttributePath>\\OPENXC\OpenXC\EventFrames[testFrame]|Sinusoid</d:SourceAttributePath>
<d:Value>85.7081527709961</d:Value>
<d:UOMName m:null="true"/>
<d:AdditionalInfo/>
<d:Annotated m:type="Edm.Boolean">false</d:Annotated>
<d:IsGood m:type="Edm.Boolean">true</d:IsGood>
<d:Questionable m:type="Edm.Boolean">false</d:Questionable>
<d:Status>Good</d:Status>
<d:Substituted m:type="Edm.Boolean">false</d:Substituted>
</m:properties>
</content>
</entry>
<entry>
<id>http://[redacted]/OData/PIDataService.svc/TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T14%3A40%3A09Z')</id>
<title type="text"/>
<updated>2012-09-11T12:09:12Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="TimeSeriesValue" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T14%3A40%3A09Z')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UOM" type="application/atom+xml;type=entry" title="UOM" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T14%3A40%3A09Z')/UOM"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SourceAttribute" type="application/atom+xml;type=entry" title="SourceAttribute" href="TimeseriesValues(SourceAttributePath='//OPENXC/OpenXC/EventFrames%5BtestFrame%5D%7CSinusoid',Timestamp=datetime'2012-09-10T14%3A40%3A09Z')/SourceAttribute"/>
<category term="vCampus.OData.Web.ObjectModel.TimeSeriesValue" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:Timestamp m:type="Edm.DateTime">2012-09-10T14:40:09Z</d:Timestamp>
<d:SourceAttributePath>\\OPENXC\OpenXC\EventFrames[testFrame]|Sinusoid</d:SourceAttributePath>
<d:Value>99.2516555786133</d:Value>
<d:UOMName m:null="true"/>
<d:AdditionalInfo/>
<d:Annotated m:type="Edm.Boolean">false</d:Annotated>
<d:IsGood m:type="Edm.Boolean">true</d:IsGood>
<d:Questionable m:type="Edm.Boolean">false</d:Questionable>
<d:Status>Good</d:Status>
<d:Substituted m:type="Edm.Boolean">false</d:Substituted>
</m:properties>
</content>
</entry>
...

(Windows Phone)客户端代码:

  var path = attr.Path;
                   var context = new DatabaseData(new Uri(MainPage.serviceUrl));

                   var query = context.CreateQuery<TimeSeriesValue>("RecordedValues")
                       .AddQueryOption("path", string.Format("'{0}'", path))
                       .AddQueryOption("startTime", "'*-1d'")
                       .AddQueryOption("endTime", "'*'");
                   query.BeginExecute(TimeSeriesReady, query);


   private void TimeSeriesReady(IAsyncResult result)
    {
        var context = result.AsyncState as DataServiceQuery<TimeSeriesValue>;
        var values = context.EndExecute(result).ToList();
        values.ForEach(v => Debug.WriteLine(v.Value));
    }

这向我展示了一个带有

的“InvalidOperationException”

处理此请求时出错。

内部异常消息是“0x80000012”,堆栈跟踪是

 at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.WebRequest_Send(InternalWebRequest request)
       at MS.Internal.InternalWebRequest.Send()
       at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
       at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
       at System.Data.Services.Http.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
       at System.Data.Services.Client.BaseAsyncResult.InvokeAsync(Func`3 asyncAction, AsyncCallback callback, Object state)
       at System.Data.Services.Client.QueryResult.BeginExecute(DataServiceContext context)
       at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state)
       at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)
       at vCampus.OData.Phone.DetailsPage.eventFrames_LoadCompleted(Object sender, LoadCompletedEventArgs e)
       at System.Data.Services.Client.DataServiceCollection`1.<>c__DisplayClass11.<>c__DisplayClass13.<BeginLoadAsyncOperation>b__f()
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

我已搜索此错误代码,但找不到任何答案。在线提供的代码示例建议了这种方法,但没有解释这个错误。

【问题讨论】:

  • 我不知道失败的原因,只是猜测 - 路径参数中的反斜杠感觉不适合 URL。在设置查询选项之前,您可以尝试在其上使用 Uri.EscapeDataString。
  • 我已经检查过了,并且在服务的 [WebGet] 方法中,参数“路径”正确到达。另外:相同的代码可以在普通的 .NET 控制台应用程序中使用。这似乎是 Windows Phone 代码中的一个问题。

标签: windows-phone-7 wcf-data-services odata webget


【解决方案1】:

正如 Vitek 建议的那样,Uri.EscapeUriString 需要用于转义 path 查询选项:

var path = attr.Path;
var context = new DatabaseData(new Uri(MainPage.serviceUrl));
var query = context.CreateQuery<TimeSeriesValue>("RecordedValues")
                .AddQueryOption("path", string.Format("'{0}'", Uri.EscapeUriString(path)))
                .AddQueryOption("startTime", "'*-1d'")
                .AddQueryOption("endTime", "'*'");
query.BeginExecute(TimeSeriesReady, query);

我们的一位开发人员对我们现有的 Windows Phone 客户端以及即将推出的 Windows Phone 客户端进行了测试和验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    相关资源
    最近更新 更多