Being a c# and Silverlight kinda guy, the time needed to get up and running was incredibly small. As always, Microsoft are very good on the Tool-side of things and the "F5" experience works very well in the Windows 8 area.
Our services are WCF based, and we support both REST and SOAP.
In this case, we decided to use the SOAP way since our REST client helper library did not exist for Win8.
"Add New Service Reference" and boom we got ourselves some client code.
To make this work though, we had to put some time into the connectivity/authorization part.
We use a cookie to store authentication token (you know, standard ASP.NET authorization) and we needed to pass along that cookie with other calls. We found that the best way to do so was to use the OperationContextScope and a previously saved cookiecontainer from the authorization call.
// prepares a soap client with the stored cookie
container
var docClient = CreateDocumentServiceClient();
// call webservice
using (new OperationContextScope(docClient.InnerChannel))
{
var doc = docClient.GetDocumentAsync(docId);
}
{
var doc = docClient.GetDocumentAsync(docId);
}
(...not gonna go into details of how to store a cookiecontainer etc, but I'm sure your friend Google could aid you if needed.)
In many of our calls, we needed to check something after the async call, so we started using await
private async void DoStuffWithDoc()
{
var docClient = CreateDocumentServiceClient();
using (new
OperationContextScope(docClient.InnerChannel))
{
await var doc = docClient.GetDocumentAsync(docId);
if (doc.YadaYada)
{
// more code here
}
}
}
This mostly worked fine, but after a while we started noticing Exceptions, especially when doing other things in parallel. Often, it was "This OperationContextScope is being disposed on a different thread than it was created". When looking at the documentation for OperationContextScope it states that:
Do not use the asynchronous “await” pattern within a OperationContextScope block. When the continuation occurs, it may run on a different thread and OperationContextScope is thread specific. If you need to call “await” for an async call, use it outside of the OperationContextScope block
|
Aha! So, how do you solve this?
The await keyword will split up your code and basically create a new method of all the code after the await keyword (and that "new method" will be executed in a separate thread sometimes). Because the end of the using statement is after the await keyword, the Dispose of the OperationContextScope instance will take place in the "new method" that might run in a separate thread.
The solution is very easy, just make sure you do not use await inside a OperationContextScope. Duh!
So, all of our methods doing any kind of webservice call with an OperationContextScope needed to be refactored looking something like this:
private async void DoStuffWithDoc(string docId)
{
var doc =
await GetDocumentAsync(docId);
if (doc.YadaYada)
{
// more code here
}
}
public
Task<Document>
GetDocumentAsync(string docId)
{
var
docClient = CreateDocumentServiceClient();
using (new
OperationContextScope(docClient.InnerChannel))
{
return docClient.GetDocumentAsync(docId);
}
}
See what we did there?
The await keyword has been moved outside the OperationContextScope to the calling method instead, and we immediately return after the call using the OperationContextScope.
Problem solved!
This was awesome! After hours of googling, I'm happy to have found your post. Solved my problem quite exactly - thank you so much!
ReplyDelete-Eric
Thanks! Glad I could help. / Jon
DeleteThanks! It worked :)
ReplyDeleteGreat! Thanks for the feedback :)
DeleteWhat if you need to capture some headers in the Incoming response and return them to the caller method? How do you handle that?
ReplyDeleteHave not had the need to do that, but you could probably either use an out argument to the function, and/or use a return value that has some parts filled in directly after the using scope and some parts filled in by the sub function (in my case GetDocumentAsync)
Deletei noticed your one method is 'async void' which is a no-no unless it is an async event. All async methods should at least return Task (if void return). it should be changed to:
ReplyDeleteprivate async Task DoStuffWithDoc(string docId)
Yep, good point!
DeleteThanks! It helped me a lot!
ReplyDeleteçekmeköy
ReplyDeletekepez
manavgat
milas
balıkesir
5CJDNZ
Adana
ReplyDeleteErzurum
Sinop
istanbul
Düzce
GD4
https://titandijital.com.tr/
ReplyDeletekilis parça eşya taşıma
bursa parça eşya taşıma
ığdır parça eşya taşıma
bitlis parça eşya taşıma
4EGR
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
2B4V
izmir evden eve nakliyat
ReplyDeletemalatya evden eve nakliyat
hatay evden eve nakliyat
kocaeli evden eve nakliyat
mersin evden eve nakliyat
HBDB
hatay evden eve nakliyat
ReplyDeleteısparta evden eve nakliyat
erzincan evden eve nakliyat
muğla evden eve nakliyat
karaman evden eve nakliyat
ONA3
87471
ReplyDeleteÇankırı Lojistik
Ünye Çelik Kapı
Malatya Evden Eve Nakliyat
Artvin Parça Eşya Taşıma
Çerkezköy Petek Temizleme
Binance Referans Kodu
Çerkezköy Evden Eve Nakliyat
Maraş Şehir İçi Nakliyat
Urfa Parça Eşya Taşıma
7AE64
ReplyDeleteDenizli Evden Eve Nakliyat
Alya Coin Hangi Borsada
Tekirdağ Lojistik
Karaman Parça Eşya Taşıma
Bursa Lojistik
Pancakeswap Güvenilir mi
Düzce Parça Eşya Taşıma
Şırnak Parça Eşya Taşıma
Hakkari Şehirler Arası Nakliyat
B06FB
ReplyDeleteOrdu Evden Eve Nakliyat
Aksaray Parça Eşya Taşıma
Rize Şehirler Arası Nakliyat
Tesla Coin Hangi Borsada
Çerkezköy Ekspertiz
Kastamonu Parça Eşya Taşıma
Kırklareli Evden Eve Nakliyat
Çankırı Parça Eşya Taşıma
Lovely Coin Hangi Borsada
3AD82
ReplyDeleteAdıyaman Evden Eve Nakliyat
Afyon Şehirler Arası Nakliyat
Big Wolf Coin Hangi Borsada
Trabzon Şehir İçi Nakliyat
Bilecik Evden Eve Nakliyat
Aksaray Lojistik
Kocaeli Parça Eşya Taşıma
Nexa Coin Hangi Borsada
Konya Lojistik
EB21D
ReplyDeleteSatoshi Coin Hangi Borsada
Çerkezköy Oto Boya
Edirne Evden Eve Nakliyat
Sinop Parça Eşya Taşıma
Kırıkkale Parça Eşya Taşıma
Tekirdağ Fayans Ustası
Kilis Şehir İçi Nakliyat
Kütahya Şehirler Arası Nakliyat
Bibox Güvenilir mi
DF244
ReplyDeletebinance indirim
E67DB
ReplyDeleteCoin Kazma
Coin Çıkarma Siteleri
Binance'de Kaldıraç Var mı
Btcturk Borsası Güvenilir mi
Kripto Para Kazma Siteleri
Binance Yaş Sınırı
Coin Nasıl Çıkarılır
Binance Sahibi Kim
Coin Kazanma
86153
ReplyDeleteKripto Para Üretme Siteleri
Binance Para Kazanma
Coin Madenciliği Siteleri
Bitcoin Nasıl Kazılır
Binance Borsası Güvenilir mi
Yeni Çıkacak Coin Nasıl Alınır
Kripto Para Kazanma Siteleri
Coin Madenciliği Nedir
Kripto Para Nedir
A0152
ReplyDeleteCoin Kazanma
Kripto Para Madenciliği Nedir
Binance Madencilik Nasıl Yapılır
Binance Kaldıraçlı İşlem Nasıl Yapılır
Bitcoin Madenciliği Nasıl Yapılır
Bitcoin Mining Nasıl Yapılır
Bitcoin Çıkarma Siteleri
Bitcoin Hesap Açma
Kripto Para Madenciliği Siteleri
44C60
ReplyDeletebinance referans kodu
referans kimliği nedir
resimli magnet
resimli magnet
referans kimliği nedir
binance referans kodu
binance referans kodu
binance referans kodu
resimli magnet
EB710
ReplyDeleteMexc Borsası Kimin
Dxgm Coin Hangi Borsada
Kwai Beğeni Satın Al
Nexa Coin Hangi Borsada
Binance Hesap Açma
Bitcoin Nasıl Para Kazanılır
Facebook Grup Üyesi Hilesi
Satoshi Coin Hangi Borsada
Binance Referans Kodu