I am using google.cloud.firestore package in .net core and I want to query the documents that have a specific field value existing in a provided list of values (like the IN command in sql). I came across the WhereIn method but I keep getting an exception. I have been trying for the last few hours but no success!
Here is the code:
public async Task<List<Document>> GetListByAccountRef(List<string> accountRefs)
{
var docRef = _Firestore.Collection("documents").WhereIn("AccountRef", accountRefs);
var docsQuery = await docRef.GetSnapshotAsync();
var docs = docsQuery.Select(d => d.ConvertTo<Document>()).ToList();
return docs;
}
I get an exception on the second line:
var docsQuery = await docRef.GetSnapshotAsync();
This is the error message:
Status(StatusCode=InvalidArgument, Detail="Unknown FieldFilter operator.")
And here is the stacktrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Grpc.Core.Internal.ClientResponseStream`2.<MoveNext>d__5.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Linq.AsyncEnumerable.<ForEachAsync_>d__174`1.MoveNext() in
D:\a\1\s\Ix.NET\Source\System.Interactive.Async\ForEach.cs:line 141
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Cloud.Firestore.Query.<GetSnapshotAsync>d__54.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Docer.DAL.Repositories.DocumentRepository.<GetListByAccountRef>d__7.MoveNext() in E:\Docer\webapp-
FireStore-Branch\Docer.DAL\Repositories\Document\DocumentRepository.cs:line 84
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Docer.BLL.Doc.DocumentBusiness.<GetUserDocuments>d__10.MoveNext() in E:\Docer\webapp-FireStore-
Branch\Docer.BLL.Doc\Document\DocumentBusiness.cs:line 84
Any help is appreciated, thanks.
This worked for me. You can follow the tutorial (https://pieterdlinde.medium.com/netcore-and-cloud-firestore-94628943eb3c)
List<Cities> cities = new List<Cities>()
{
new Cities()
{
CityName="tests"
}
};
Query employeeQuery = fireStoreDb.Collection(nameof(Employee)).WhereIn("City", cities);
QuerySnapshot employeeQuerySnapshot = await employeeQuery.GetSnapshotAsync();
Thanks to Jon, the problem appeared to be from the old version of the firestore emulator that I was using. After updating the emulator, it's working fine.
Related
I am new to azure. I am trying to create ResourceGroup then trying to deploy a VM. But I am getting exception during accessing resourcegroups.
I am following MSDN example given here: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/csharp-template
I tried different subscription but no luck. Couldn't find any pointers online for this issue or I might have missed. If you could help me please let me know.
Thanks.
//AppId,Key,TenetId are correct
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(ApplicationID, Key, TenetID,
new AzureEnvironment()
{
AuthenticationEndpoint = #"https://login.windows.net/",
ManagementEndpoint = #"https://management.core.windows.net/",
ResourceManagerEndpoint = #"https://management.azure.com/",
GraphEndpoint = #"https://graph.windows.net/",
});
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(AzureProperties.SubscriptionId);
var groupName = "MyresourceGroup1";
var location = Region.USWest;
//Exception comes at here.
var resourceGroup = azure.ResourceGroups.Define(groupName)
.WithRegion(location)
.Create();
//Exception details.
System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: value
Source=mscorlib
StackTrace:
at System.String.EndsWith(String value, StringComparison comparisonType)
at Microsoft.Azure.Management.ResourceManager.Fluent.Authentication.AzureCredentials.<ProcessHttpRequestAsync>d__24.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.ResourceGroupsOperations.<CreateOrUpdateWithHttpMessagesAsync>d__6.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.ResourceGroupImpl.<CreateResourceAsync>d__26.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions.Creatable`4.<Microsoft-Azure-Management-ResourceManager-Fluent-Core-ResourceActions-IResourceCreator<IResourceT>-CreateResourceAsync>d__15.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.CreatorTaskItem`1.<ExecuteAsync>d__6.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.TaskGroupBase`1.<ExecuteNodeTaskAsync>d__14.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Management.ResourceManager.Fluent.Core.Extensions.Synchronize[TResult](Func`1 function)
at Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions.Creatable`4.Create()
at pvt_CreateVM.AzureVMManager.CreateVM()
at pvt_CreateVM.Program.Main(String[] args)
I think code is currently failing because you haven't set any value for KeyVaultSuffix property for AzureEnvrionment that you have initialized.
It's a guess looking at the error info you have shared but I say this after looking at the relevant source code for Azure Management Libraries for .NET
(on a side note it's awesome that anyone can do that.. here is a link)
public async override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var adSettings = new ActiveDirectoryServiceSettings
{
AuthenticationEndpoint = new Uri(Environment.AuthenticationEndpoint),
TokenAudience = new Uri(Environment.ManagementEndpoint),
ValidateAuthority = true
};
string url = request.RequestUri.ToString();
if (url.StartsWith(Environment.GraphEndpoint, StringComparison.OrdinalIgnoreCase))
{
adSettings.TokenAudience = new Uri(Environment.GraphEndpoint);
}
string host = request.RequestUri.Host;
// I guess this is where your code is failing currently.
if (host.EndsWith(Environment.KeyVaultSuffix, StringComparison.OrdinalIgnoreCase))
{
How to fix
Unless there is a good reason, I would not initialize AzureEnvironment object and instead use the values already available, as that would make sure all required properties have correct values.
Example: AzureEnvironment.AzureGlobalCloud. Look here for all possible values
Change this code where you initialize AzureEnvironment
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(ApplicationID, Key, TenetID,
new AzureEnvironment()
{
AuthenticationEndpoint = #"https://login.windows.net/",
ManagementEndpoint = #"https://management.core.windows.net/",
ResourceManagerEndpoint = #"https://management.azure.com/",
GraphEndpoint = #"https://graph.windows.net/",
});
To this code
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId,
clientSecret,
tenantId,
AzureEnvironment.AzureGlobalCloud);
In case you have some reason that none of the available environment values are good enough, then make sure you initialize all required properties for AzureEnvironment
I am trying to get the links from VSO for workitems, and the code seems to work fine when called from a single thread, but throws exceptions when called from a parallel for each loop.
I initalize my vso client object in my class's constructor:
vso = new WorkItemReporting(Config.VSTSAccessToken);
Then later in a method:
Parallel.ForEach(msrcAbBugsToProcess, new ParallelOptions { MaxDegreeOfParallelism = 10 }, bugId =>
{
var workItemLinks = vso.GetWorkItemSourceCodeLinks(bugId);
});
witclient below is a WorkItemTrackingHttpClient which is in a different class (WorkItemReporting) and calls the API. It is this call that fails.
public List<string> GetWorkItemSourceCodeLinks(int bugId)
{
var workItemSourceCodeLinks = new List<string>();
var workItem = _witClient.GetWorkItemAsync(bugId, null, null, WorkItemExpand.Relations).Result;
if (workItem?.Relations != null)
{
var validSourceCodeLinkTypes = new List<string> { "ArtifactLink", "Hyperlink" };
foreach (var relation in workItem.Relations)
{
if (validSourceCodeLinkTypes.Contains(relation.Rel))
{
workItemSourceCodeLinks.Add(relation.Url);
}
}
}
}
This works fine if I don't use the Parallel.ForEach and I get the needed data from the API. When I do, I get this exception 50% of the time:
Object reference not set to an instance of an object.
at System.Security.Cryptography.X509Certificates.X509CertificateCollection.GetHashCode()
at System.Net.HttpWebRequest.GetConnectionGroupLine()
at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.StartGettingResponse(RequestState state)
at System.Net.Http.HttpClientHandler.StartRequest(Object obj)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.VisualStudio.Services.Common.VssHttpMessageHandler.<SendAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.Common.VssHttpRetryMessageHandler.<SendAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__45`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__27`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__26`1.MoveNext()
Is there something I'm doing wrong?
A solution is to create WorkItemReporting inside Parallel.ForEach:
Parallel.ForEach(msrcAbBugsToProcess, new ParallelOptions { MaxDegreeOfParallelism = 10 }, bugId =>
{
var vso = new WorkItemReporting(Config.VSTSAccessToken);
var workItemLinks = vso.GetWorkItemSourceCodeLinks(bugId);
});
You need to lock access to the external resource by doing something like the following.
As per comment #AlexSikilinda _witClient must not be thread safe.
For what your doing though, just because you can create multiple threads doesn't means you should, i would get timings, on what your doing with and without the parallel processing in your code,
Keep in mind if you use non thread safe external resources, and locks then it maybe just as slow as running in a normal for loop. You need to test both scenario's.
Add an object outside your methods to allow access to the lock object from every thread, this will help manage the access to the non thread safe class.
vso = new WorkItemReporting(Config.VSTSAccessToken);
private readonly object _witLock = new object();
In the following you DON'T want to put the lock around the entire methods contents as i have, otherwise you are definitely wasting your time using the Parallel for this piece of code.
Based on what your doing thought this should stop the exceptions.
You need refactor the code so that only the call to _witClient is within the lock. So just declare the workItem variable outside the lock with it proper type, and then only wrap the call to workItem = _witClient.GetWorkItemAsync(bugId, null, null, WorkItemExpand.Relations).Result; in your lock code.
public List<string> GetWorkItemSourceCodeLinks(int bugId)
{
var workItemSourceCodeLinks = new List<string>();
lock (_witLock)
{
var workItem = _witClient.GetWorkItemAsync(bugId, null, null, WorkItemExpand.Relations).Result;
if (workItem?.Relations != null)
{
var validSourceCodeLinkTypes = new List<string> { "ArtifactLink", "Hyperlink" };
foreach (var relation in workItem.Relations)
{
if (validSourceCodeLinkTypes.Contains(relation.Rel))
{
workItemSourceCodeLinks.Add(relation.Url);
}
}
}
}
}
Good Luck
I'm trying to write a program to communicate with a telegram bot, and I'm using C# api:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Run().Wait();
}
static async Task Run()
{
var bot = new Telegram.Bot.Api("bot<token>");
var offset = 0;
while (true)
{
var updates = await bot.GetUpdates(offset);
foreach (var update in updates)
{
await bot.SendTextMessage(update.Message.Chat.Id, update.Message.Text);
offset = update.Id + 1;
}
}
}
}
the problem is that I get aggregate exception when I try to use the default GetUpdates() method defined in the api. and this is the error stack:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Telegram.Bot.Api.<SendWebRequest>d__78`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ConsoleApplication2.Program.<Run>d__1.MoveNext() in C:\Users\Sasan\documents\visual studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 24
I understand that when I try to request for Updates using Long Polling:
var updates = await bot.GetUpdates(offset,100,500);
the problem seems to go away, but that doesn't seem right.
if anyone has any information that could help me, I'd be really thankful.
I am not able to add a custom type to he ContinuationData in the FileOpenPicker. I can add string/int/datetime, but when I try to add my custom CustomFolder object it fails with the following exception.
CustomFolder TargetFolde = new CustomFolder()
FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");
KeyValuePair<string, object> fold = new KeyValuePair<string, object>("Folder", TargetFolder);
open.ContinuationData.Add(fold);
open.PickMultipleFilesAndContinue();
}
{System.Exception: Data of this type is not supported. (Exception from
HRESULT: 0x8007065E) at
System.Runtime.InteropServices.WindowsRuntime.IMap2.Insert(K key, V
value) at
System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Insert[K,V](IMap2
_this, K key, V value) at System.Runtime.InteropServices.WindowsRuntime.MapToDictionaryAdapter.Add[K,V](K
key, V value) at
System.Runtime.InteropServices.WindowsRuntime.MapToCollectionAdapter.Add[K,V](KeyValuePair`2
item) at Lockie.Shared.Util.d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Lockie.Folder.d__5.MoveNext()}
Only basic types are allowed. WinRT does not offer a serialization mechanism like .NET
You can serialize the data yourself using XML or JSON and store that as a string.
I have a C# project with a service reference against a web service.
I have the following code, first to trigger a method at the service request:
private void Button_Click(object sender, RoutedEventArgs e)
{
App.service.getFileCompleted += service_getFileCompleted;
App.service.getFileAsync(App.user, App.document);
}
And the service_getFileCompleted is supposed to retrieve a byte[], put it into a file and then open that file with the default application:
async void service_getFileCompleted(object sender, BackendReference.getFileCompletedEventArgs e)
{
string tmp = "temp." + e.Result.type;
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile localFile = await local.CreateFileAsync(tmp, CreationCollisionOption.ReplaceExisting);
using (Stream writeStream = await localFile.OpenStreamForWriteAsync())
{
writeStream.Seek(0, SeekOrigin.End);
await writeStream.WriteAsync(e.Result.fileData, 0, e.Result.fileData.Length);
}
await Windows.System.Launcher.LaunchFileAsync(localFile);
}
}
First time the apps starts, and the button is triggered it works like a charm. However, when pressing the button the second time the row
Stream writeStream = await localFile.OpenStreamForWriteAsync()
casts an System.UnauthorizedAccessException.
More exactly:
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.WindowsRuntimeStorageExtensions.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.IO.WindowsRuntimeStorageExtensions.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at archive.DocumentDetail.d__0.MoveNext()} System.Exception {System.UnauthorizedAccessException}
Why is this so?
Thanks