在我们的Xamarin项目中使用Simple.OData.Client之前,我在LINQPad的帮助下进行了尝试。它的易用性给我留下了深刻的印象。当我将它构建到我们的Xamarin项目中时,我尝试从SharePoint的 REST Api 获取数据时遇到了异常。
Simple.OData.Client.WebRequestException:遇到意外的 WebException ---> System.Net.WebException:错误:SendFailure(写入标头时出错)---> System.Net.WebException:写入标头时出错 ---> System.IO。 IOException:身份验证或decr ... {Simple.OData.Client.WebRequestException:遇到意外的WebException ---> System.Net.WebException:错误:SendFailure(写入标头错误)---> System.Net.WebException:错误写入标头 ---> System.IO.IOException:身份验证或解密失败。---> Mono.Security.Protocol.Tls.TlsException:从服务器收到的证书无效。错误代码:0xffffffff800b010a
我相信,此异常是由于我们的SharePoint实例使用自签名证书这一事实引起的。我尝试通过将始终返回 true 到ServerCertificateValidationCallback来消除它
System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
现在我总是从 Simple.OData.Client 得到未经授权的异常
Simple.OData.Client.WebRequestException:未经授权
初始调用来自MainViewModel通过业务层
private async void InitializeAsync()
{
// TODO [Anton Kalcik - Dienstag, 05. Mai 2015 17:09:55]: Show loading indicator
TaskEntity getTaskForCurrentMonthAsyncTask = await _taksBusinessLayer.GetTaskForCurrentMonthAsync();
_timeToDisplay = getTaskForCurrentMonthAsyncTask.DueDate - DateTime.Now;
// TODO [Anton Kalcik - Dienstag, 05. Mai 2015 17:10:04]: Hide loading indicator
StartCountdownTimer();
}
执行调用的类是类SharePointTaskRepository
public class SharePointTaskRepository : ITaskRepository
{
private readonly string _collectionName;
private readonly ODataClient _oDataClient;
public SharePointTaskRepository(Uri sharepointUri, string collectionName, ICredentials credentials)
{
if (sharepointUri == null)
{
throw new ArgumentNullException("sharepointUri");
}
if (String.IsNullOrWhiteSpace(collectionName))
{
throw new ArgumentException("Argument can't be null, empty or white space!", "collectionName");
}
if (credentials == null)
{
throw new ArgumentNullException("credentials");
}
_collectionName = collectionName;
var oDataClientSettings = new ODataClientSettings(sharepointUri, credentials);
_oDataClient = new ODataClient(oDataClientSettings);
}
public async Task<IEnumerable<TaskModel>> ReadAsync(Expression<Func<TaskModel, bool>> filter, Expression<Func<TaskModel, object>> orderBy, int numberOfResults)
{
return await _oDataClient
.For<TaskModel>(_collectionName)
.Filter(filter)
.OrderBy(orderBy)
.Top(numberOfResults)
.FindEntriesAsync();
}
}
我仔细检查了凭据,这绝对是正确的。利用ServerCertificateValidationCallback的代码位于ApplicationRuntimeSettings中。此类是特定于平台的、单例的,并通过依赖注入作为所有其他对象提供。
[assembly: Dependency(typeof(ApplicationRuntimeSettings))]
namespace AZeitReminder.Droid.Infrastructure
{
public class ApplicationRuntimeSettings : ApplicationRuntimeSettingsBase
{
public ApplicationRuntimeSettings()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
}
public override SQLiteConnection CreateSqLiteConnection()
{
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, DatabaseFileName);
var currentPlatform = new SQLitePlatformAndroid();
var connection = new SQLiteConnection(currentPlatform, path);
return connection;
}
public override CultureInfo GetCultureInfo()
{
var androidLocale = Java.Util.Locale.Default;
var netLanguage = androidLocale.ToString().Replace("_", "-"); // NOTE [Anton Kalcik - Dienstag, 05. Mai 2015 17:21:10]: turns pt_BR into pt-BR
return new CultureInfo(netLanguage);
}
}
}