【发布时间】:2010-10-09 07:21:32
【问题描述】:
我正在构建 Silverlight / Windows Azure 应用程序。
我正在尝试从 Web 角色公开的 REST API 中读取一些简单数据。
private void MainPage_Loaded(object sender, RoutedEventArgs args)
{
// ...
WebClient data = new WebClient();
data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(data_DownloadStringCompleted);
data.DownloadStringAsync(new Uri("localhost:88/expenseservice.svc/expenses"));
}
void data_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
XElement xmlStrs = XElement.Parse(e.Result);
strBox.ItemsSource = xmlStrs.Descendants("ArrayOfstring");
}
不幸的是,它总是失败并显示以下消息:
NotSupportedException:“URI 前缀 无法识别。”
我在这里做错了什么?是在抱怨“本地主机”吗?有没有更简单的方法来访问我自己的解决方案中公开的服务? (这可能是“添加服务引用”的用途吗?)
更新 将http:// 添加到 URI 使其正常工作。 (但显然,一旦它部署到 Azure,这将不起作用?)
仅使用相对路径,如 /expenseservice.svc/expenses 会失败,并出现以下异常:
System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at ExpenseCalc_SilverLight.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs args)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
【问题讨论】:
标签: c# visual-studio silverlight rest