Edit field in all documents in Azure DocumentDb - c#

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.
}
}

Related

RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method

I am tring to Scan document from my Application but excutution time my code stuck somewhere and Code did not worked. some time throw same error which I mention in Header. I given here my all code and also which I tried.
This is part of my code
private async void HomePageScanner()
{
try
{
string pdfFileName = string.Empty;
pdfFileName = Guid.NewGuid().ToString() + ".pdf";
//scanPdfFile = pdfFileName;
StorageFolder scanTempFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync(Constants.PATH_TEMP_SCAN, CreationCollisionOption.OpenIfExists);
DeviceInformationDisplay selectedScanner = CmbScannerList.SelectedItem as DeviceInformationDisplay;
string ScannerID = selectedScanner.Id;
StorageFolder pdfFolder_DataFTP = await KnownFolders.PicturesLibrary.CreateFolderAsync(Constants.PATH_SCAN, CreationCollisionOption.OpenIfExists);
//var task = Task.Run(async () => { await ScanToFolder(ScannerID, scanTempFolder, pdfFileName, pdfFolder_DataFTP, null); });
//Task.Run(task);
ScanToFolder(ScannerID, scanTempFolder, pdfFileName, pdfFolder_DataFTP, null).RunSynchronously();
// await ScanToFolder(ScannerID, scanTempFolder, pdfFileName, pdfFolder_DataFTP, null);
}
catch (Exception ex) { new ExceptionHelper().AddException(ex.Message, this.ToString(), "HomePageScanner"); }
}
public async Task ScanToFolder(string deviceId, StorageFolder scanTempFolder, string pdfFileName, StorageFolder pdfFolder_dataFTP, StorageFolder pdfFolder_DataServer)
{
try
{
scanningDialog.ShowAsync();
_scanerTimer.Tick += _scanerTimer_Tick;
_scanerTimer.Interval = new TimeSpan(0, 0, 1);
_scanerTimer.Start();
// ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);
ImageScanner myScanner = null;//= ImageScanner.FromIdAsync(deviceId);
await Task.Run(async () =>
{
myScanner = await ImageScanner.FromIdAsync(deviceId);
// var task = Task.Run(async () => { myScanner = await ImageScanner.FromIdAsync(deviceId);
_isScanContinue = true;
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
if (myScanner.IsScanSourceSupported(ImageScannerScanSource.AutoConfigured))
{
myScanner.FlatbedConfiguration.Format = ImageScannerFormat.Jpeg; //Code Stuck here some time
for (int i = 0; i < _totalPageToScan; i++)
{
_pageCounter++;
if (i > 0)
{
scanningDialog.IsEnabled = false;
//scanningDialog.Hide();
ContentDialog scanConfirmationDialog = new ContentDialog()
{
Title = "Click OK button to scan the next page of your document",
CloseButtonText = "OK"
};
scanConfirmationDialog.Closed += ScanConfirmationDialog_Closed;
await scanConfirmationDialog.ShowAsync();
}
}
}
});
}
}
private void _scanerTimer_Tick(object sender, object e)
{
_scanCounter++;
if (_scanCounter == 30)
{
if (_isScanContinue == false)
{
CommonCls.ShowToastMessage("Something went wrong. please check your scanner");
this.Hide();
scanningDialog.DataContext = "Something went wrong!!";
scanningDialog.Hide();
}
}
}
I updated Complete Please View and Help me to solve this.
I hope a better solution which I needed most.
Thank You
You should think object oriented: one object is the scanner, one object is the dialog. They have to be completely independent each other and communicate via a clear interface. You can start from the code below where I skipped all the real scanning logic.
class ScannerInfoEventArgs : EventArgs
{
public string Message { get; set; }
public string ButtonText { get; set; }
public bool DialogEanbled { get; set; }
public bool Close { get; set; }
}
class Scanner
{
private int _totalPageToScan;
private int _pageCounter;
public event EventHandler<ScannerInfoEventArgs>? OnScannerInfo;
public delegate void ScannerInfoEventHandler(object sender, ScannerInfoEventArgs e);
public async Task HomePageScanner()
{
try
{
string pdfFileName = string.Empty;
pdfFileName = Guid.NewGuid().ToString() + ".pdf";
_totalPageToScan = 10; // or your calculation here
_pageCounter = 0;
await ScanToFolder("c:\\tempfolder", pdfFileName, "c:\\pdf_data", "c:\\data_server");
}
catch (Exception ex)
{
}
}
public bool IsScanSourceSupported(string source)
{
// yuor logic here
return true;
}
public enum ImageType
{
png,
jpeg
}
public ImageType imageType
{
get; set;
}
public async Task ScanToFolder(string scanTempFolder, string pdfFileName, string pdfFolder_dataFTP, string pdfFolder_DataServer)
{
try
{
await Task.Run(() =>
{
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
if (this.IsScanSourceSupported("your parameter here"))
{
this.imageType = ImageType.jpeg; //Code Stuck here some time
for (int i = 0; i < _totalPageToScan; i++)
{
_pageCounter++;
if (i > 0)
{
ScannerInfoEventArgs ev = new ScannerInfoEventArgs() { Message = "Click OK button to scan the next page of your document", ButtonText = "OK", DialogEanbled = false, Close = false };
OnScannerInfo?.Invoke(this, ev);
if (ev.Close)
{
break;
}
}
if (i == 30) // or something more meaningful condition
{
ScannerInfoEventArgs ev = new ScannerInfoEventArgs() { Message = "Something went wrong.please check your scanner", ButtonText = "OK", DialogEanbled = true, Close = true };
OnScannerInfo?.Invoke(this, ev);
break;
}
}
}
});
}
catch (Exception)
{ }
}
}
class FakeDialog
{
public void ShowDialog(object sender, ScannerInfoEventArgs e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.ButtonText);
string asw = Console.ReadLine();
if (asw == "stop")
{
e.Close = true;
}
}
}
static class Program
{
static async Task Main()
{
var scanner = new Scanner();
var dialog = new FakeDialog();
scanner.OnScannerInfo += dialog.ShowDialog;
await scanner.HomePageScanner();
}
}
You should keep just the idea of object separation and asyn/await patter. Hope this can be a starting point.

How to wait for methods to finish then do new action?

I'm setting up my architechture to use Cef.Offscreen. In order to make it easy to work with I have divided some parts. But I run into a problem that controller loading finshes and serves a view before everything has been able to load.
Here's my structure --> Controller
public ActionResult InitBrowser()
{
ICefSharpRenderer renderer = RendererSingelton.GetInstance();
//Try to render something in default appdomain
renderer.LoginToTradingView(null, null);
ViewBag.SiteTitle = BrowserActions.RunScriptInNamedBrowser("loginbrowser", #"(function() {return document.title;} )();");
ViewBag.ImagesixtyfourUrl = BrowserActions.TakeScreenshot("loginbrowser");
//this is returned to fast, we have to wait for all
return View();
}
I have this class to get do some basic actions and initialize if needed.
public class CefSharpRenderer : MarshalByRefObject, ICefSharpRenderer
{
private ChromiumWebBrowser _browser;
private TaskCompletionSource<JavascriptResponse> _taskCompletionSource;
private string _name;
public void LoginToTradingView(string url, string browserName)
{
CheckIfCefIsInitialized();
BrowserFactory.GetBrowserInstance(#"https://se.tradingview.com/", "loginbrowser");
}
public void CreateBrowserAndGoToUrl(string url, string browserName)
{
CheckIfCefIsInitialized();
BrowserFactory.GetBrowserInstance(url, "browserName");
}
public void CheckIfCefIsInitialized()
{
if (!Cef.IsInitialized)
{
var settings = new CefSettings();
var assemblyPath = Path.GetDirectoryName(new Uri(GetType().Assembly.CodeBase).LocalPath);
settings.BrowserSubprocessPath = Path.Combine(assemblyPath, "CefSharp.BrowserSubprocess.exe");
settings.ResourcesDirPath = assemblyPath;
settings.LocalesDirPath = Path.Combine(assemblyPath, "locales");
var osVersion = Environment.OSVersion;
//Disable GPU for Windows 7
if (osVersion.Version.Major == 6 && osVersion.Version.Minor == 1)
{
// Disable GPU in WPF and Offscreen examples until #1634 has been resolved
settings.CefCommandLineArgs.Add("disable-gpu", "1");
}
//Perform dependency check to make sure all relevant resources are in our output directory.
Cef.Initialize(settings, performDependencyCheck: false, cefApp: null);
}
}
}
I get my browserinstance here and connected the events to be fired.
public static class BrowserFactory
{
public static ChromiumWebBrowser GetBrowserInstance(string _url, string browsername)
{
if (!BrowserContainer.CheckIfBrowserExists(browsername))
{
ChromiumWebBrowser _browser = new ChromiumWebBrowser(_url);
_browser.LoadingStateChanged += BrowserEvents.OnLoadingStateChanged;
BrowserContainer.AddDataHolder(browsername, new DataBrowserHolder { BrowserName = browsername, ChromiumWebBrow = _browser });
return _browser;
}
return null;
}
}
Browserevent loads correct page.
public static class BrowserEvents
{
public static void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
if (args.IsLoading == false)
{
ChromiumWebBrowser cwb = (ChromiumWebBrowser)sender;
if (cwb.Address == "https://se.tradingview.com/")
{
BrowserActions.LogInToTradingView("xxxxx", "yyyyyyy", "loginbrowser");
}
}
}
}
Last my browseractions, spare med for the thread sleeps it's just under construction and it works atm.
public static class BrowserActions
{
public static void LogInToTradingView(string twusername, string twpassword, string browserName)
{
ChromiumWebBrowser _dataholder = BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow;
IFrame ifww = _dataholder.GetMainFrame();
// var lull = #"(function() { var serielength = TradingView.bottomWidgetBar._widgets.backtesting._reportWidgetsSet.reportWidget._data.filledOrders.length; return serielength; })();";
// JavascriptResponse _js = Task.Run(async () => { return await _browser.GetMainFrame().EvaluateScriptAsync(lull); }).Result;
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-header__link tv-header__link--signin js-header__signin')[0].click();})();");
// var loginusernamescript =
var loginpasswordscript = #"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].value= " + twpassword + "; })();";
var clkloginbtn = #"(function() { document.getElementsByClassName('tv-button tv-button--no-border-radius tv-button--size_large tv-button--primary_ghost tv-button--loader')[0].click();})();";
Thread.Sleep(300);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[0].click();})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[0].value = '" + twusername + "';})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].click();})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { window.document.getElementsByClassName('tv-control-material-input tv-signin-dialog__input tv-control-material-input__control')[1].value = '" + twpassword + "';})();");
Thread.Sleep(50);
ifww.ExecuteJavaScriptAsync(#"(function() { document.getElementsByClassName('tv-button tv-button--no-border-radius tv-button--size_large tv-button--primary_ghost tv-button--loader')[0].click();})();");
}
public static string TakeScreenshot(string browserName)
{
try
{
Bitmap img = Task.Run(async () => { return await BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow.ScreenshotAsync(); }).Result;
// object mgss = img.Clone();
string baseen = ExtraFunctions.ToBase64String(img, ImageFormat.Png);
return baseen;
}
catch (Exception e)
{
var x = e.InnerException;
return null;
}
}
public static string RunScriptInNamedBrowser(string browserName, string script)
{
try
{
string str = Task.Run(async () => { return await BrowserContainer.GetDataHolderByName(browserName).ChromiumWebBrow.GetMainFrame().EvaluateScriptAsync(script); }).Result.ToString();
// object mgss = img.Clone();
return str;
}
catch (Exception e)
{
var x = e.InnerException;
return null;
}
}
}
How can I get my browser actions to report back to my controller so that I can wait for them to finish?
For a Task asynchronous operation to report back, it's possible to use Progress<T>. How that's done is detailed in Enabling Progress and Cancellation in Async APIs. The key is:
var progressIndicator = new Progress<int>(ReportProgress);
This creates a Progress<T> object that can indicate how far a task is complete, and also call a custom method (ReportProgress) at set intervals. You can create a custom class if necessary instead of using int.
So your browser actions can report back to the controller with the progress reporting method until everything is complete.

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);
}

Calling an async method inside Background worker dosen't report progress paralelly

I am calling an async method that returns a Task < CurrentCartridgeStatus> from Background worker. Thing is until the CurrentCartridgeStatus changes its state I want to keep calling it. But I am not awaiting on this method call because making the DoWork method async is giving me an exception.
But in my case with my current call(Please see the StartCurrentRun method) the background worker initially reports 1% then it seems to be staying idle, and after the status changes then it reports the rest 99%. Please help how can I make the Background worker report progress in parallel with StartCurrentRun method.
private void StartCurrentRun(bool obj)
{
this.worker = new BackgroundWorker();
this.worker.WorkerReportsProgress = true;
this.worker.WorkerSupportsCancellation = true;
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
this.worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
IsLiveProgress = true;
StartTimer();
this.worker.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
CreateEventLogs.WriteToEventLog(string.Format("Run with Assay:{0} Volume{1} has been started", SelectedAssay, SelectedVolume));
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
int current = 1;
CurrentCartridgeStatus status = CurrentCartridgeStatus.NotProcessed;
var instance = ConnectToInstrument.InstrumentConnectionInstance;
do
{
var result = instance.StartCurrentRun(SelectedAssay.display_name, int.Parse(SelectedVolume.Remove(SelectedVolume.Length - 2, 2)));
//var result = Test(current);
status = result.Result;
CurrentStatus = status.ToString();
CreateEventLogs.WriteToEventLog("Run status - " + CurrentStatus);
//Thread.Sleep(2000);
worker.ReportProgress(Math.Min(current, 100));
current++;
}
while (status != CurrentCartridgeStatus.Processed);
ConnectToInstrument.IsAlreadyExecuted = false;
if (current < 100)
{
worker.ReportProgress(current + (100 - current));
}
}
Here is the StartCurrentRun method. It internally calls some firmware logic. Just to make this more clear here is the entire ConnectToInstrument Class.
public class ConnectToInstrument
{
private EventProxy _updater;
private IInstrument _instrument;
private QpCallBack _cb;
//private IQEvent #event;
private QpWrapper _qp;
private InstrumentTypes _type;
private CurrentInstrumentStatus InstrumentCurrentStatus = CurrentInstrumentStatus.NotReady;
private static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
public static bool IsAlreadyExecuted = false;
public bool IsConnected { get; set; }
private static ConnectToInstrument _instrumentConnectionInstance;
public static ConnectToInstrument InstrumentConnectionInstance
{
get
{
if (_instrumentConnectionInstance == null)
{
_instrumentConnectionInstance = new ConnectToInstrument();
}
return _instrumentConnectionInstance;
}
}
private ConnectToInstrument()
{
try
{
_cb = new QpCallBack();
_qp = new QpWrapper();
//var config = new ConfigManager();
//var client = new Dna2Client();
_type = InstrumentTypes.Simulator;
var factory = new ApiFactory(_cb, _qp, _type);
_instrument = factory.GetInstrument();
InitializeQf();
_updater = new EventProxy();
_updater.Start(1);
StartUiProxy().Forget();
}
catch (Exception ex)
{
//Log to Error Log
}
}
private async Task<object> StartUiProxy()
{
while (true)
{
try
{
var #event = await _updater.GetEvent();
switch ((QpSignals)#event.QSignal)
{
case QpSignals.INSTRUMENT_STATUS:
{
var status = #event as UiEvent<InstrumentStatus>;
if (status.Value != InstrumentStatus.Ready)
{
InstrumentCurrentStatus = CurrentInstrumentStatus.NotConnected;
continue;
}
else
{
InstrumentCurrentStatus = CurrentInstrumentStatus.Ready;
return Task.FromResult<object>(InstrumentStatus.Ready);
}
}
case QpSignals.TRAY_STATUS:
{
var status = #event as UiEvent<TrayStatus>;
if (status.QSignal == 6 && status.Value == TrayStatus.Opened)
{
return Task.FromResult<object>(TraySignals.OPEN);
}
else if (status.QSignal == 6 && status.Value == TrayStatus.Opened)
{
return Task.FromResult<object>(TraySignals.CLOSE);
}
return null;
}
case QpSignals.PROCESS_CARTRIDGE:
{
var status = #event as UiEvent<CartridgeStatus>;
if (status.Value == CartridgeStatus.Processing)
{
return status;
}
else if (status.Value == CartridgeStatus.Processed)
{
return status;
}
return null;
}
}
}
catch (Exception ex)
{
//Log to Error Log
}
}
}
private void InitializeQf()
{
QF.Instance.Initialize((int)QpSignals.MAX_SIGNAL - 1);
}
public async Task<bool> Connect()
{
bool status = false;
string[] ports = new string[40];
if (_type == InstrumentTypes.Dgx)
{
ports = await ComPortInfo.GetAvailablePortNamesForDevice("USB Serial Port");
if (ports == null || ports.Length == 0)
{
Exception ex = new Exception("No Serial Devices found.");
//Log
return false;
}
}
try
{
string thermPort = _type == InstrumentTypes.Simulator ? string.Empty : ports[0]; //ports[0] should be changed to combobox selected value
status = _instrument.Connect(ports[0]);
}
catch (Exception ex)
{
Debug.Write(ex.Message);
}
return status;
}
public async Task<CurrentInstrumentStatus> GetCurrentInstrumentStatus()
{
return await Task.FromResult(InstrumentCurrentStatus);
//_cb.InstrumentStatusUpdates(InstrumentStatus.Ready);
//var value = await _updater.GetEvent();
//var status = value as UiEvent<InstrumentStatus>;
//if (status.QSignal == 5 && status.Value == InstrumentStatus.Ready)
//{
// return CurrentInstrumentStatus.Ready;
//}
//return CurrentInstrumentStatus.Error;
}
public async Task<CurrentCartridgeStatus> StartCurrentRun(string Assay, int volume)
{
object value;
UiEvent<CartridgeStatus> status;
do
{
//await Task.Delay(1000);
//_cb.ProcessCartridge(CartridgeStatus.Processing);
if (!IsAlreadyExecuted)
{
_instrument.ProcessCartridge();
}
IsAlreadyExecuted = true;
//value = await StartUiProxy();
value = await _updater.GetEvent();
status = value as UiEvent<CartridgeStatus>;
}
while (status == null);
try
{
//IQEvent value;
if (status.QSignal == 9 && status.Value == CartridgeStatus.Processing)
{
return CurrentCartridgeStatus.Processing;
}
if (status.QSignal == 9 && status.Value == CartridgeStatus.Processed)
{
return CurrentCartridgeStatus.Processed;
}
}
catch (Exception ex)
{
//Log it
}
return CurrentCartridgeStatus.Unidentified;
}
}
Here is the CurrentCartridgeStatus enum
public enum CurrentCartridgeStatus
{
Unidentified = 0,
NotProcessed = 1,
Processing = 2,
Processed = 3
}

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