本文整理汇总了C#中Result.Throw方法的典型用法代码示例。如果您正苦于以下问题:C# Result.Throw方法的具体用法?C# Result.Throw怎么用?C# Result.Throw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Result
的用法示例。
在下文中一共展示了Result.Throw方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
/// <summary>
/// Invoke the plug with the <see cref="Verb.GET"/> verb and no message body and return as a document result.
/// </summary>
/// <remarks>
/// Since this method goes straight from a <see cref="DreamMessage"/> to a document, this method will set a
/// <see cref="DreamResponseException"/> on the result, if <see cref="DreamMessage.IsSuccessful"/> is <see langword="False"/>.
/// </remarks>
/// <param name="plug">Plug instance to invoke.</param>
/// <param name="result">The <see cref="Result{XDoc}"/>instance to be returned by this method.</param>
/// <returns>Synchronization handle.</returns>
public static Result<XDoc> Get(this Plug plug, Result<XDoc> result)
{
plug.Invoke(Verb.GET, DreamMessage.Ok(), new Result<DreamMessage>()).WhenDone(r => {
if(r.HasException) {
result.Throw(r.Exception);
} else if(!r.Value.IsSuccessful) {
result.Throw(new DreamResponseException(r.Value));
} else {
result.Return(r.Value.ToDocument());
}
});
return result;
}
示例2: WriteData_Helper
protected override Yield WriteData_Helper(ExportItem item, Result result) {
string file = GetFilePath(item);
string filepath = Path.Combine(_packageDirectory, file);
string path = Path.GetDirectoryName(filepath);
if(!Directory.Exists(path)) {
_log.DebugFormat("creating directory: {0}", path);
Directory.CreateDirectory(path);
}
FileStream fileStream = File.Create(filepath);
Result<long> copyResult;
yield return copyResult = item.Data.CopyTo(fileStream, item.DataLength, new Result<long>()).Catch();
item.Data.Close();
fileStream.Close();
if(copyResult.HasException) {
result.Throw(copyResult.Exception);
yield break;
}
if(item.DataLength != copyResult.Value) {
throw new IOException(string.Format("tried to write {0} bytes, but wrote {1} instead for {2}", item.DataLength, copyResult.Value, filepath));
}
_log.DebugFormat("saved: {0}", filepath);
AddFileMap(item.DataId, file);
item.Data.Close();
result.Return();
yield break;
}
示例3: PostImport
public Yield PostImport(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
string uri = context.GetParam("uri", null);
string reltopath = context.GetParam("reltopatch", "/");
DreamMessage packageMessage = request;
if(!string.IsNullOrEmpty(uri)) {
Result<DreamMessage> packageResult;
yield return packageResult = Plug.New(uri).InvokeEx("GET", DreamMessage.Ok(), new Result<DreamMessage>());
packageMessage = packageResult.Value;
if(!packageMessage.IsSuccessful) {
throw new DreamAbortException(DreamMessage.BadRequest(string.Format("Unable to retrieve package from Uri '{0}': {1}", uri, packageMessage.Status)));
}
}
string tempFile = Path.GetTempFileName();
Stream tempStream = File.Create(tempFile);
Result<long> copyResult;
// TODO (steveb): use WithCleanup() to dispose of resources in case of failure
yield return copyResult = packageMessage.ToStream().CopyTo(tempStream, packageMessage.ContentLength, new Result<long>()).Catch();
tempStream.Dispose();
if(copyResult.HasException) {
response.Throw(copyResult.Exception);
yield break;
}
ArchivePackageReader archivePackageReader = new ArchivePackageReader(File.OpenRead(tempFile));
Result<ImportManager> importerResult;
Plug authorizedDekiApi = _dekiApi.WithHeaders(request.Headers);
// TODO (steveb): use WithCleanup() to dispose of resources in case of failure
yield return importerResult = ImportManager.CreateAsync(authorizedDekiApi, reltopath, archivePackageReader, new Result<ImportManager>()).Catch();
if(importerResult.HasException) {
archivePackageReader.Dispose();
File.Delete(tempFile);
response.Throw(importerResult.Exception);
yield break;
}
ImportManager importManager = importerResult.Value;
Result importResult;
yield return importResult = importManager.ImportAsync(new Result()).Catch();
archivePackageReader.Dispose();
File.Delete(tempFile);
if(importResult.HasException) {
response.Throw(importResult.Exception);
yield break;
}
response.Return(DreamMessage.Ok());
yield break;
}
示例4: Logoff
public Result<bool> Logoff(Result<bool> aResult)
{
if (aResult == null)
throw new ArgumentNullException("aResult");
BasePlug.At("_session").Delete(new Result<DreamMessage>()).WhenDone(
a => {
if (a.Status == DreamStatus.Ok)
aResult.Return(true);
else
aResult.Throw(new CouchException(a));
},
aResult.Throw
);
return aResult;
}
示例5: IsLogged
public Result<bool> IsLogged(Result<bool> aResult)
{
if (aResult == null)
throw new ArgumentNullException("aResult");
BasePlug.At("_session").Get(new Result<DreamMessage>()).WhenDone(
a =>
{
if (a.Status == DreamStatus.Ok)
{
JObject user = JObject.Parse(a.ToText());
aResult.Return(user["info"]["authenticated"] != null);
}
else
{
aResult.Throw(new CouchException(a));
}
},
aResult.Throw
);
return aResult;
}
示例6: WriteData_Helper
//--- Methods ---
protected override Yield WriteData_Helper(ExportItem item, Result result) {
var tempfilename = Path.GetTempFileName();
var file = GetFilePath(item);
using(var fileStream = File.Create(tempfilename)) {
Result<long> copyResult;
yield return copyResult = item.Data.CopyTo(fileStream, item.DataLength, new Result<long>()).Catch();
item.Data.Close();
if(copyResult.HasException) {
result.Throw(copyResult.Exception);
yield break;
}
if(item.DataLength != copyResult.Value) {
throw new IOException(string.Format("tried to write {0} bytes, but wrote {1} instead for {2}", item.DataLength, copyResult.Value, tempfilename));
}
fileStream.Seek(0, SeekOrigin.Begin);
yield return WriteZipStream(file, fileStream, item.DataLength, new Result());
}
File.Delete(tempfilename);
_log.DebugFormat("saved: {0}", file);
AddFileMap(item.DataId, file);
item.Data.Close();
result.Return();
yield break;
}
示例7: Logon
/// <summary>
/// Perform Cookie base authentication with given username and password
/// Resulting cookie will be automatically used for all subsequent requests
/// </summary>
/// <param name="username">User Name</param>
/// <param name="password">Password</param>
/// <param name="result"></param>
/// <returns>true if authentication succeed</returns>
public Result<bool> Logon(string username, string password,
Result<bool> result)
{
if (String.IsNullOrEmpty(username))
throw new ArgumentException("username cannot be null nor empty");
if (String.IsNullOrEmpty(password))
throw new ArgumentException("password cannot be null nor empty");
if (result == null)
throw new ArgumentNullException("result");
string content = String.Format("name={0}&password={1}",
username, password);
BasePlug
.At("_session")
.Post(DreamMessage.Ok(MimeType.FORM_URLENCODED, content),
new Result<DreamMessage>())
.WhenDone(
a =>
{
switch (a.Status)
{
case DreamStatus.Ok:
BasePlug.CookieJar.Update(a.Cookies,
new XUri(BasePlug.Uri.SchemeHostPort));
BasePlug = BasePlug.WithHeader("X-CouchDB-WWW-Authenticate",
"Cookie");
result.Return(true);
break;
default:
result.Throw(new CouchException(a));
break;
}
},
result.Throw
);
return result;
}