Removing element from SortedSet in C# - c#

I'm trying to remove an element from a Redis Sorted list without success
public bool Delete(int id)
{
try
{
var redisManager = new RedisManagerPool(Global.RedisConnector);
using (var redis = redisManager.GetClient())
{
var entities = redis.As<BookingRequestModel>();
var list = entities.SortedSets["BookingRequests"].GetAll();
//count = 320
var model = list.First(z=>z.Id == id);
list.Remove(model);
//count 319 - however when I refresh the page I still see my old data in grid ...
entities.Save();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
also tried like this :
public bool Delete(int id)
{
try
{
var redisManager = new RedisManagerPool(Global.RedisConnector);
using (var redis = redisManager.GetClient())
{
var entities = redis.As<BookingRequestModel>();
var list = entities.SortedSets["BookingRequests"];
var model = list.First(z=>z.Id == id);
var result = entities.RemoveItemFromSortedSet(list, model); // result is false
entities.Save();
return true;
}
}
catch (Exception ex)
{
return false;
}
}
As I commented there I still can see the old data after removing them from grid.
Can you please help me with this ?

Don't understand why , but this is working :
public bool Delete(int id)
{
try
{
var redisManager = new RedisManagerPool(Global.RedisConnector);
using (var redis = redisManager.GetClient())
{
var items = redis.GetAllItemsFromSortedSet("BookingRequests");
foreach (var item in items)
{
var dto = item.FromJson<BookingRequestModel>();
if (dto.Id == id)
{
redis.RemoveItemFromSortedSet("BookingRequests", item);
break;
}
}
return true;
}
}
catch (Exception ex)
{
return false;
}
}

Related

EFCore return value when no data exists in table

I have the following method to return data from database:
public async Task<DentalHistoryModel> GetDentalHistoryAsync(int patientid)
{
DentalHistoryModel dentalHistory = new();
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
try
{
dentalHistory = await context.DentalHistory.Where(p => p.PatientId == patientid).FirstOrDefaultAsync();
}
catch (Exception ex)
{
}
}
return dentalHistory;
}
If there is no data in the DentalHistory table, what should be returned in dentalHistory? I am getting an empty dentalHistory object. Shouldn't I get a null in return?

Is there a way to pass a method into another method in c# and what's the best way to do it?

I'm trying to keep my code dry. I have 5 methods that have the same bulk of code but I don't want the write them all multiple times. Below are 2 sample methods. I want to know how I can consolidate it into 1 generic method. Is that the best way to do it?
public async Task<PlaylistListResponse> GetPlaylistsFromChannel(string channelID, bool forceRefresh = false)
{
string barrelURL = "GetPlaylistsFromChannel" + channelID.Trim();
PlaylistListResponse results = default;
if(forceRefresh != true)
{
results = GetCached<PlaylistListResponse>(barrelURL);
}
else
{
try
{
if (object.Equals(results, default(PlaylistListResponse)))
{
var playlistFromChannelRequest = youtubeService.Playlists.List("id");
playlistFromChannelRequest.ChannelId = channelID;
playlistFromChannelRequest.MaxResults = 50;
results = await playlistFromChannelRequest.ExecuteAsync();
Barrel.Current.Add(barrelURL, results, TimeSpan.FromDays(30));
}
return results;
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
}
return results;
}
public async Task<PlaylistListResponse> GetPlaylistsFromChannel(string channelID, bool forceRefresh = false)
{
string barrelURL = "GetPlaylistsFromChannel" + channelID.Trim();
PlaylistListResponse results = default;
if (forceRefresh != true)
{
results = GetCached<PlaylistListResponse>(barrelURL);
}
else
{
try
{
if (object.Equals(results, default(PlaylistListResponse)))
{
var videosFromPlaylistRequest = youtubeService.PlaylistItems.List("snippet");
videosFromPlaylistRequest.PlaylistId = playlistID;
videosFromPlaylistRequest.MaxResults = 50;
results = await videosFromPlaylistRequest.ExecuteAsync();
Barrel.Current.Add(barrelURL, results, TimeSpan.FromDays(30));
}
return results;
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
}
return results;
}
I've tried creating a generic method but can't find a way to make changes to it as I don't want a multitude of if statements.
public async Task<T> GetAsync<T>(string barrelURL, int days = 7, bool forceRefresh = false)
{
results = default(T);
if (!CrossConnectivity.Current.IsConnected)
results = Barrel.Current.Get<T>(barrelURL);
if (!forceRefresh && !Barrel.Current.IsExpired(barrelURL))
results = Barrel.Current.Get<T>(barrelURL);
try
{
if (object.Equals(results, default(T)))
{
//myMethodsCode??
Barrel.Current.Add(barrelURL, results, TimeSpan.FromDays(days));
}
return results;
}
catch (Exception ex)
{
Console.WriteLine($"Unable to get information from server {ex}");
}
}
A possible solution to this problem would be to create a generic method and pass delegates that represent differing parts into it.
public async Task<T> GetFromChannel<T>(
string channelID,
Func<Task<T>> resultGetter,
bool forceRefresh = false)
{
string barrelURL = "GetPlaylistsFromChannel" + channelID.Trim();
T results = default;
if (forceRefresh != true)
{
results = GetCached<T>(barrelURL);
}
else
{
try
{
if (object.Equals(results, default(T)))
{
results = await resultGetter();
Barrel.Current.Add(barrelURL, results, TimeSpan.FromDays(30));
}
return results;
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
}
return results;
}
And then you can insert the differing parts using other methods or lambda exressions, for example:
public Task<PlaylistListResponse> GetPlaylistsFromChannel(
string channelID,
bool forceRefresh = false)
{
return GetFromChannel<PlaylistListResponse>(
channelID,
async () =>
{
var videosFromPlaylistRequest = youtubeService.PlaylistItems.List("snippet");
videosFromPlaylistRequest.PlaylistId = playlistID;
videosFromPlaylistRequest.MaxResults = 50;
return await videosFromPlaylistRequest.ExecuteAsync();
},
forceRefresh);
}

FireClient does not work and return to class with any error in Xamarin?

this is login function but when compiler compile the line where "value" mean its return to Xamarin form without proceeding further
Thank You
async public Task<User> GetLogin(User info)
{
User login = new User();
var firebase = new FirebaseClient("https://xyz.firebaseio.com/");
try
{
var value = await firebase.Child("User").OrderByValue().OnceAsync<User>();
foreach (var item in value)
{
if (info.Username == item.Object.Username && info.Password == item.Object.Password )
{
login.Id = item.Object.Id;
login.Fullname = item.Object.Fullname;
login.Username = item.Object.Username;
login.Password = item.Object.Password;
login.Role = item.Object.Role;
MasterPage session = new MasterPage(login);
return login;
}
else { }
}
}
catch (Exception ex) { }
return null;
}

Edit field in all documents in Azure DocumentDb

I have my data in DocumentDb.
I want to edit one field in all documents with Type == 8.
How I can do it?
Create console app for it
Use Azure Functions
I tried the second option and it not working.
public static void Run(string myQueueItem, dynamic inputDocument)
{
if(inputDocument.Type == 8) {
inputDocument.Entity.Discount.Name = inputDocument.Entity.Discount.Name + "smth";
}
}
Do you have any idea?
Below code seems naïve and RU-wasteful but will work.
DocumentClient _documentDbclient;
// Database configuration
static readonly string DocumentDbKeyConfig = "3dzS7T3a8lgEblkEnzSsdfasdfasf2omI1cp7==";
static readonly string DocumentDbEndPointConfig = "https://your-docdb.documents.azure.com:443/";
static readonly string DocumentDbDatabaseNameConfig = "your-database-name";
void Main()
{
string collectionName = "TestCollection";
Uri uri = UriFactory.CreateDocumentCollectionUri(DocumentDbDatabaseNameConfig, collectionName);
InitializeDocumentDbAsync(DocumentDbDatabaseNameConfig).ConfigureAwait(false);
createDocumentCollectionIfNotExistAsync(DocumentDbDatabaseNameConfig, collectionName).ConfigureAwait(false);
DocumentCollection collection = _documentDbclient.ReadDocumentCollectionAsync(uri).Result;
var query = _documentDbclient.CreateDocumentQuery(uri);
var queryList = query.ToList();
int index = 0;
int count = 50; // number of item to fetch at a time
while (true)
{
var result = queryList.Skip(index * count).Take(count);
if (!result.Any())
{
// end iterating.
return;
}
foreach (Document element in result)
{
JObject j = (dynamic)element;
if(j["Type"] != 8) continue;
j["YourProperty"] = "Some Value"; // edit value.
var upsertedResult = _documentDbclient.UpsertDocumentAsync(uri, j, null, true).Result;
}
++index;
}
}
public async Task InitializeDocumentDbAsync(string databaseName)
{
// Create a new instance of the DocumentClient
_documentDbclient = new DocumentClient(
new Uri(DocumentDbEndPointConfig), DocumentDbKeyConfig);
// Check if the database FamilyDB does not exist
try
{
await _documentDbclient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName));
}
catch (DocumentClientException de)
{
// If the database does not exist, create a new database
if (de.StatusCode == HttpStatusCode.NotFound)
{
await _documentDbclient.CreateDatabaseAsync(new Database { Id = databaseName });
}
else
{
throw;
}
}
catch (Exception e)
{
throw;
}
}
private async Task createDocumentCollectionIfNotExistAsync(string databaseName, string collectionName)
{
try
{
await
_documentDbclient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName));
}
catch (DocumentClientException de)
{
// not exist
throw;
}
catch (Exception e)
{
// do nothing.
}
}

Duplex WCF net.tcp service disposed object exception for my client

I implemented a client-server application for chatting with WCF. And i have a callback for broadcasting. It was working properly until i changed some functions. I was using my dataset fix data. It was initialized on my server and was used. I implemented an XML parser mechanism and then i'm getting an error like that:
Cannot access a disposed object. Object name:
'System.ServiceModel.InstanceContext'.
I'm using my service like that:
System.ServiceModel.InstanceContext context = new System.ServiceModel.InstanceContext(this.cb);
this._client = new WCFDevComServiceClient(context);
and then i'm openning this._client just one time and i'm using this object for my each call. (I don't close this object anytime.)
If i open before each call and then close it, then it's working. But in this case my callback function doesn't work. And i can't broadcast. So I can't close my client object.
Do you have any further information about this error? Or any fix suggestion?
My Service Codes:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFDevComService : IWCFDevComService
{
private const string deviceXmlPath = #"D:\MyDocuments\Visual Studio 2015\Projects\WCFBroadcastorService\WCFDeviceCom\WCFDeviceCom_Service\bin\Devices.xml";
private const string userXmlPath = #"D:\MyDocuments\Visual Studio 2015\Projects\WCFBroadcastorService\WCFDeviceCom\WCFDeviceCom_Service\bin\Users.xml";
private DbOperations.DbOperations dbOperator = new DbOperations.DbOperations();
private static List<User> UserCollection = new List<User>();
private static Dictionary<string, IWCFDevComServiceBroadcastorCallback> clients = new Dictionary<string, IWCFDevComServiceBroadcastorCallback>();
private static object locker = new object();
private List<Device> deviceCollection = new List<Device>();
public bool LoginToServer(string clientName)
{
bool retVal = false;
if (clientName != "")
{
retVal = CheckUserName(clientName);
if (true == retVal)
{
try
{
IWCFDevComServiceBroadcastorCallback callback =
OperationContext.Current.GetCallbackChannel<IWCFDevComServiceBroadcastorCallback>();
if (clients.Keys.Contains(clientName))
clients.Remove(clientName);
clients.Add(clientName, callback);
}
catch (Exception ex)
{
throw;
}
}
}
return retVal;
}
private bool CheckUserName(string clientName)
{
lock (locker)
{
UserCollection = dbOperator.QueryForGetAllUsers(userXmlPath);
foreach (var user in UserCollection)
{
if (user.m_userName.Contains(clientName))
{
return true;
}
}
return false;
}
}
public List<Device> GetAllDevicesInformation()
{
try
{
return GetInformationFromDeviceXML();
}
catch (Exception ex)
{
throw;
return null;
}
}
private List<Device> GetInformationFromDeviceXML()
{
try
{
lock (locker)
{
deviceCollection = dbOperator.QueryForGetAllDevices(deviceXmlPath);
}
return deviceCollection;
}
catch (Exception ex)
{
throw;
return null;
}
}
public int GetNumberOfDevices()
{
try
{
lock (locker)
{
//todo: GetNumberOfDevicesFromXML
return dbOperator.QueryForGetDeviceCount(deviceXmlPath);
}
}
catch (Exception ex)
{
throw;
return 0;
}
}
public Device GetDeviceInformation(int index)
{
List<Device> deviceCollection = GetInformationFromDeviceXML();
return deviceCollection[index];
}
public int BlockedDevice(int index, User user)
{
try
{
if (index < 0 && user == null)
return -1; //fail
lock (locker)
{
if (deviceCollection[index].m_state.m_state != States.Free)
return 1; // isn't free for blocking
DeviceState tmpState = new DeviceState();
tmpState.m_state = States.Locked;
tmpState.m_lockedUser = user;
deviceCollection[index].m_state = tmpState;
//todo: UpdateXML
dbOperator.QueryForUpdateDeviceStateById(deviceXmlPath, index + 1, tmpState);
//deviceCollection = GetInformationFromDeviceXML();
NotifyOtherClients(index, user);
return 0; //success
}
}
catch (Exception ex)
{
throw;
return -1;
}
}
public int FreeDevice(int index, User user)
{
try
{
if (index < 0 && user == null)
return -1; //fail
lock (locker)
{
if (deviceCollection[index].m_state.m_state != States.Locked)
return 1; // isn't locked to free
if (deviceCollection[index].m_state.m_lockedUser.m_userName != user.m_userName && user.m_userPermission != Permission.ADMIN)
return 2; // isn't right person
DeviceState tmpState = new DeviceState();
tmpState.m_state = States.Free;
tmpState.m_lockedUser = null;
deviceCollection[index].m_state = tmpState;
//todo: UpdateXML
dbOperator.QueryForUpdateDeviceStateById(deviceXmlPath, index + 1, tmpState);
//deviceCollection = GetInformationFromDeviceXML();
NotifyOtherClients(index, user);
return 0; //success
}
}
catch (Exception ex)
{
throw;
return -1;
}
}
public Permission GetPermission(string userName)
{
lock (locker)
{
foreach (var user in UserCollection)
{
if (user.m_userName == userName)
return user.m_userPermission;
}
return Permission.USER;
}
}
private void NotifyOtherClients(int index, User user)
{
try
{
var inactiveClients = new List<string>();
EventDataType eventData = new EventDataType();
eventData.ClientName = user.m_userName;
eventData.Index = index;
foreach (var client in clients)
{
if (client.Key != eventData.ClientName)
{
try
{
client.Value.BroadcastToClient(eventData);
}
catch (Exception ex)
{
inactiveClients.Add(client.Key);
}
}
}
if (inactiveClients.Count > 0)
{
foreach (var client in inactiveClients)
{
clients.Remove(client);
}
}
}
catch (Exception ex)
{
throw;
}
}
}

Categories