OData Simple.OData.Client V3 sends other request - c#

I am using Simple.OData.Client V3 for getting some data from odata service, which is hosted remotely. Here is what I am doing but for unknown reasons odata client is sending another request
OData client initialization
public ODataClient CreateClient(bool isPost = true)
{
var uri = new Uri(ServiceAddress);
return new ODataClient(new ODataClientSettings(uri)
{
Credentials = new NetworkCredential(UserName, Password),
PayloadFormat = !isPost ? ODataPayloadFormat.Json : ODataPayloadFormat.Atom
});
}
Get request
public async Task<GetCustomersResponse> GetCustomers()
{
var client = CreateClient(false);
var x = ODataDynamic.Expression;
var response = await client.For("Catalog_Контрагенты").Top(10).FindEntriesAsync();
var raw = JsonConvert.SerializeObject(response).ToString();
var obj = JsonConvert.DeserializeObject<List<CustomerItem>>(raw);
return new GetCustomersResponse()
{
Items = obj
};
}
instead of sending
http://odataservice/Catalog_Контрагенты
it sends
http://odataservice/Catalog_АвансовыйОтчетПрисоединенныеФайлы
I also tried sending request in this way and it works correctly
public async Task<GetCustomersResponse> GetCustomers()
{
var client = CreateClient(false);
var response = await client.FindEntriesAsync("Catalog_Контрагенты?$top=10");
var raw = JsonConvert.SerializeObject(response).ToString();
var obj = JsonConvert.DeserializeObject<List<CustomerItem>>(raw);
return new GetCustomersResponse()
{
Items = obj
};
}
but I don't understand what is wrong with previous sample.
Same thing happens when I am trying to update or add new customer
It sends other request.
UPDATE 1
This is Fidller request when it sends incorrect request
GET http://hostname/odata/standard.odata/Catalog_%D0%90%D0%B2%D0%B0%D0%BD%D1%81%D0%BE%D0%B2%D1%8B%D0%B9%D0%9E%D1%82%D1%87%D0%B5%D1%82%D0%9F%D1%80%D0%B8%D1%81%D0%BE%D0%B5%D0%B4%D0%B8%D0%BD%D0%B5%D0%BD%D0%BD%D1%8B%D0%B5%D0%A4%D0%B0%D0%B9%D0%BB%D1%8B?$top=10 HTTP/1.1
Accept: application/json, application/xml, application/text
Prefer: return-no-content
Authorization: Basic d2FuZGlvOjEyMzQ=
Host: hostname
Response
HTTP/1.1 200 OK
Content-Length: 185
Content-Type: application/json;charset=utf-8
Server: Microsoft-IIS/7.5
DataServiceVersion: 3.0
X-Powered-By: ASP.NET
Date: Thu, 05 May 2016 06:58:27 GMT
{
"odata.metadata": "http://hostname/odata/standard.odata/$metadata#Catalog_АвансовыйОтчетПрисоединенныеФайлы",
"value": []
}
And here is request that works correctly
GET http://hostname/odata/standard.odata/Catalog_%D0%9A%D0%BE%D0%BD%D1%82%D1%80%D0%B0%D0%B3%D0%B5%D0%BD%D1%82%D1%8B?$top=10 HTTP/1.1
Accept: application/json, application/xml, application/text
Prefer: return-no-content
Authorization: Basic d2FuZGlvOjEyMzQ=
Host: hostname
Response
HTTP/1.1 200 OK
Content-Length: 28626
Content-Type: application/json;charset=utf-8
Server: Microsoft-IIS/7.5
DataServiceVersion: 3.0
X-Powered-By: ASP.NET
Date: Thu, 05 May 2016 09:40:21 GMT
{
"odata.metadata": "http://hostname/odata/standard.odata/$metadata#Catalog_Контрагенты",
"value": [{
"Ref_Key": "f9210ba9-cbf8-11e1-8023-00155d01bf09",
"DataVersion": "AAAAAAAki8I=",
"DeletionMark": false,
"Parent_Key": "ca28c1de-af9e-11e1-a90b-00155d01bf04",
"IsFolder": false,
"Code": "002879 ",
"Description": "შპს პრაიმ ქემიქალს1",
"ИНН": "404867569",
"КодПоОКПО": "",
"КПП": "",
"НаименованиеПолное": "შპს პრაიმ ქემიქალს",
"БанковскийСчетПоУмолчанию_Key": "00000000-0000-0000-0000-000000000000",
"ДоговорПоУмолчанию_Key": "f9210baa-cbf8-11e1-8023-00155d01bf09",
"КонтактноеЛицо_Key": "00000000-0000-0000-0000-000000000000",
"ФизическоеЛицо_Key": "00000000-0000-0000-0000-000000000000",
"СчетУчетаРасчетовСПокупателем_Key": "552c3f13-4ae6-48a3-a9e2-5ce660958242",
"СчетУчетаАвансовПокупателя_Key": "d38b5a6a-be8b-4c2b-8702-57ea8e02e3e6",
"СчетУчетаРасчетовСПоставщиком_Key": "9d195613-e9b0-4dd9-959d-72fd653ac7fc",
"СчетУчетаАвансовПоставщику_Key": "5060faf9-602e-478c-be88-7145c6f48586",
"Ответственный_Key": "00000000-0000-0000-0000-000000000000",
"Комментарий": "",
"ВестиРасчетыПоДоговорам": true,
"ВестиРасчетыПоДокументам": true,
"ВестиРасчетыПоЗаказам": true,
"ВестиУчетОплатыПоСчетам": true,
"ЮрФизЛицо": "ЮрЛицо",
"АдресЭП": "",
"Нерезидент": false,
"КонтактнаяИнформация": [],
"ДополнительныеРеквизиты": [],
"Parent#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/Parent",
"ДоговорПоУмолчанию#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/ДоговорПоУмолчанию",
"СчетУчетаРасчетовСПокупателем#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/СчетУчетаРасчетовСПокупателем",
"СчетУчетаАвансовПокупателя#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/СчетУчетаАвансовПокупателя",
"СчетУчетаРасчетовСПоставщиком#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/СчетУчетаРасчетовСПоставщиком",
"СчетУчетаАвансовПоставщику#navigationLinkUrl": "Catalog_Контрагенты(guid'f9210ba9-cbf8-11e1-8023-00155d01bf09')/СчетУчетаАвансовПоставщику"
}
metadatainfo

Simple.OData.Client has bug related to non-latin words and will be fixed in adapter.
Here is this issue odata client sends wrong request

Related

Getting StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers error when Calling a API via code

I try to call an external api, when I am using the Postman, it is working and returning value as follows:
Post to URL: https://test.com/api/v1/users/check
Data Raw Jason to post:
{
"access_token":"4444-EA444B6-2844C7-A09C-44B05CA78E42A3",
"email":"test#test.com",
"create_user": true,
"first_name": "test4",
"last_name": "test",
"phone": 3104054512
}
So this is working and returning me response model.
but when try this code to call the api:
Controller:
[Route("CreateUser")]
public Task<UserReturn> CreateUser([FromBody] User user)
{
return homebirdRepository.CreateUser(user);
}
public async Task<UserReturn> CreateUser(User userCheck)
{
using (GetWSObject<UserReturn> addObjectInt = new GetWSObject<UserReturn>())
{
return await addObjectInt.PostWSObjectModel("api/v1/users/check", userCheck, "API_URI");
}
}
public async Task<T> PostWSObjectModel(string uriActionString, Object model, string apiKey)
{
T returnValue = default(T);
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(WebConfigurationManager.AppSettings[apiKey]);
var content = new StringContent(JsonConvert.SerializeObject(model));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await client.PostAsync(uriActionString, content);
response.EnsureSuccessStatusCode();
var test = response.Content.ReadAsStringAsync().Result;
returnValue = JsonConvert.DeserializeObject<T>(((HttpResponseMessage)response).Content.ReadAsStringAsync().Result);
}
return returnValue;
}
catch (Exception e)
{
throw (e);
}
}
This code is returning me this error:
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content:
System.Net.Http.StreamContent,
Headers:
{
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-
With
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Cache-Control: no-cache, private
Date: Sat, 28 Dec 2019 00:00:04 GMT
Set-Cookie:laravel_session=eyJpdiI6IlI2MUdzOFJmS0RcL1k1VmJCeTc4bk1nPT0iLCJ2YWx1ZSI6IlZXNW11MGw2bXk0ajFEaTM2VnhmbUZjQnFzdnRDRHV5ejJMaDRqTVJYQm1yclNyUUkweDNRMUhpZDZwblpES1MiLCJtYWMiOiI0NmFiODA4YzEyNTkxZDllNDViNGUwOGIzYjY2ZWYxZGQwNzI1NmZmYzYxYTBkZGU0M2NmMDBlYzIzN2E3OTFjIn0%3D; expires=Sat, 28-Dec-2019 02:00:04 GMT; Max-Age=7200; path=/; httponly
Server: Apache
Content-Length: 21
Content-Type: text/html; charset=UTF-8
}}
You most likely have a header, cookie, etc. that is not set in C# which is set in Postman. There are several ways you can determine which properties are being set in Postman. I've answered a similar question here. Fiddler or some other separate tool shouldn't be necessary.
you must add the Authorization header, this one is added and calculated by postman, you can copy/post. the following if you are using a Basic authentication.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "value to copy from postman");

Salesforce marketing cloud API does not return an access/authorization token

I have an application that requests the marketing cloud for an access token to use in subsequent requests to access the API. This was designed last year and working fine till last week. The same request has been failing since 02/22. Here is the sample code to request the token:
public static async Task<string> GetAuthorizationToken(string ClientId, string ClientSecret)
{
string strAuthorizationToken = string.Empty;
HttpClient client = new HttpClient();
var dictParams = new Dictionary<string, string>()
{
{ "clientId", ClientId }, {"clientSecret", ClientSecret }
};
var content = new FormUrlEncodedContent(dictParams);
var response = await client.PostAsync("https://auth.exacttargetapis.com/v1/requestToken", content);
if (response.IsSuccessStatusCode)
{
var strresponse = await response.Content.ReadAsStringAsync();
//dynamic objResult = JsonConvert.DeserializeObject<dynamic>(strresponse);
//strAuthorizationToken = objResult.accessToken;
}
return strAuthorizationToken;
}
GetAuthorizationToken("*********", "*******").GetAwaiter().GetResult();
Here is the error I am getting from the API:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
X-Mashery-Responder: 02-26
Vary: Origin
X-Mashery-Message-ID: f4cec199-2e7e-49e2-88e0-6673ffe849ed
strict-transport-security: max-age=15552000; preload
Content-Security-Policy: upgrade-insecure-requests
x-xss-protection: 1; mode=block
x-frame-options: DENY
x-content-type-options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Connection: close
Cache-Control: no-store, must-revalidate, no-cache, max-age=0, private
Date: Tue, 26 Feb 2019 16:42:29 GMT
Server: Apache
Content-Length: 223
Content-Type: application/json; charset=UTF-8
}}
I need to know what has been changed or what I need to fix in this code to be able to access the API again. Please help how I can make this work again.
Thanks in advance.
I am posting the answer as it is resolved and might be helpful for someone else who has a similar issue with sending http requests.
I had to add the following line of code to specify the security protocol to use:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;

ASP.NET Web API receives a null as parameter in POST when called from R

I have created a Web API in ASP.NET MVC 4 using Visual Studio 2012. I am using a POST method to receive data sent by the user/calling application.
[HttpPost]
public HttpResponseMessage Post(Object model)
{
HttpResponseMessage hrm;
try
{
if (model == null)
{
hrm = new HttpResponseMessage(HttpStatusCode.NotFound);
hrm.Content = new StringContent("Null request data");
hrm.ReasonPhrase = "Incomming contract info is NULL";
return hrm;
}
InputData IDat = JsonConvert.DeserializeObject<InputData>(model.ToString());
...
When I test the POST to the API using HttpClient.PostAsJsonAsync with the following code,
HttpClient hc = new HttpClient();
string resp = "";
if (idat.GetType().Name == "InputData")
{
hc = new HttpClient();
hc.BaseAddress = new Uri("http://localhost:49688/");
hc.DefaultRequestHeaders.Accept.Clear();
hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = hc.PostAsJsonAsync("API/ ComputeFR", idat).Result;
resp = response.Content.ReadAsStringAsync().Result;
...
The web API receives the JSON, model above is not null, and it shows the JSON sent by the calling routine. The JSON sent looks like this:
{
"UserId": "test",
"pwd": "test123",
"ContractParam": [
{
"ContractID": "00000",
"Units": [
{
"UnitID": "11111111",
"TypeCode": 25,
"PracticeCode": 35,
"InsuredShare": 1.0,
"SaleDate": "20180315"
}
],
"Intervals": [
{
"IntervalStartDate": "20171101",
"IntervalEndDate": "20171130"
}
]
}
]
}
When I call the web API with a POST (httr package) using R as below, the Sample1.txt file contains the above JSON.
myjson <- read_file("C:\\Testing\\Sample1.txt")
# JSON encoded
url1 <- "http://localhost:49688/API/ComputeFR"
postresult <- POST(url1, body = myjson, verbose())
output <- content(postresult)
verbose() in R generates the following:
-> POST /API/ComputeFR HTTP/1.1
-> Host: localhost:49688
-> User-Agent: libcurl/7.53.1 r-curl/2.4 httr/1.3.1
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 831
->
>> {
>> "UserId": "test",
>> "pwd": "test123",
>> "ContractParam": [
>> {
>> "ContractID": "00000",
>> "Units": [
>> {
>> "UnitID": "11111111",
>> "TypeCode": 25,
>> "PracticeCode": 35,
>> "InsuredShare": 1.0,
>> "SaleDate": "20180315"
>> }
>> ],
>> "Intervals": [
>> {
>> "IntervalStartDate": "20171101",
>> "IntervalEndDate": "20171130"
>> }
>> ]
>> }
>> ]
>> }
The Web API then returns the following:
<- HTTP/1.1 404 Incoming contract info is NULL
<- Cache-Control: no-cache
<- Pragma: no-cache
<- Content-Length: 17
<- Content-Type: text/plain; charset=utf-8
<- Expires: -1
<- Server: Microsoft-IIS/10.0
<- X-AspNet-Version: 4.0.30319
<- X-SourceFiles: =?UTF-8?B?QzpcQ3JvcENvZGVcRmxleFJhdGVXZWJBcGlcRmxleFJhdGVXZWJBcGlcYXBpXEZsZXhSYXRl?=
<- X-Powered-By: ASP.NET
<- Date: Wed, 10 Jan 2018 15:28:41 GMT
The web API receives the request (I can see the headers and the content length equal what is sent from the R call). However the model is always null.
Can someone please help me understand the issue and suggest a solution for this. Thank you very much for your time and help.
Try setting the Content-Type header to "application/json" in your R request. PostAsJsonAsync is likely doing this for you in your C# client. Content-Type tells the endpoint what kind of data you're sending. Accept says what you expect in response.

Ajax success function won't execute

I have an apache cordova project where I'm trying to post form data from the client to the database. I can see the json I send going out to my API which then sends the data to the DAL and then to the database. The entry correctly updates the table and then returns a 1 or -1 based on whether or not a previous entry exists. This value is then sent to the client. I can see the exact response being sent back, but my ajax success function won't fire.
Ajax:
$("#submit").click(function () {
var info = {
Username: $("#username").val(),
Password: $("#password").val(),
Firstname: $("#firstname").val(),
Lastname: $("#lastname").val(),
Email: $("#email").val()
};
info = JSON.stringify(info);
event.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
async: false,
url: "http://localhost:57207/api/User/RegisterUser",
data: info,
success: function(data) {
console.log(data);
},
error: function(input) {
console.log(JSON.stringify(input));
}
})
});
Controller:
[System.Web.Http.HttpPost]
public JObject RegisterUser(JObject obj)
{
RegisterUsers user = new RegisterUsers();
user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
JObject jobj = new JObject();
jobj.Add("output", temp.ToString());
return jobj;
}
I have been using fiddler to check my response and request, and here is the raw data:
Request:
POST http://localhost:57207/api/User/RegisterUser HTTP/1.1
Host: localhost:57207
Connection: keep-alive
Content-Length: 125
Accept: text/html, */*; q=0.01
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:4400/test.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
{"Username":"johndoe","Password":"password!!!","Firstname":"john","Lastname":"doe","Email":"johndoe#random.com"}
Response:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRGV2ZWxvcG1lbnRcVGV4dDJQaG9uaWNzXFJlc3RBUElcYXBpXFVzZXJcUmVnaXN0ZXJVc2Vy?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
Access-Control-Allow-Headers: Content-Type, soapaction
Date: Wed, 08 Feb 2017 19:52:43 GMT
Content-Length: 15
{"output":"-1"}
My error function is the one that fires, and here is the output:
console.log:
{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:57207/api/User/RegisterUser'."}
Update:
I was able to set the async to true and my new error message is this:
{"readyState":0,"status":0,"statusText":"error"}
Try instead returning an IHttpActionResult or IActionResult, Ok() for a 200 response.
public IActionResult RegisterUser(JObject obj)
{
RegisterUsers user = new RegisterUsers();
user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
JObject jobj = new JObject();
jobj.Add("output", temp.ToString());
return Ok(jobj);
}
If it continues to fail, try just return a simple string like return Ok("Hello World!");. If it succeeds with simple string, then the serialization of the object may be failing or is coming out as invalid JSON.

Asp.Net MVC and ajax async callback execution order

I have been sorting through this issue all day and hope someone can help pinpoint my problem. I have created a "asynchronous progress callback" type functionality in my app using ajax. When I strip the functionality out into a test application I get the desired results. See image below:
Desired Functionality
When I tie the functionality into my single page application using the same code I get a sort of blocking issue where all requests are responded to only after the last task has completed. In the test app above all request are responded to in order. The server reports a ("pending") state for all requests until the controller method has completed. Can anyone give me a hint as to what could cause the change in behavior?
Not Desired
Desired Fiddler Request/Response
GET http://localhost:12028/task/status?_=1383333945335 HTTP/1.1
X-ProgressBar-TaskId: 892183768
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:12028/
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Connection: Keep-Alive
DNT: 1
Host: localhost:12028
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcVEVNUFxQcm9ncmVzc0Jhclx0YXNrXHN0YXR1cw==?=
X-Powered-By: ASP.NET
Date: Fri, 01 Nov 2013 21:39:08 GMT
Content-Length: 25
Iteration completed...
Not Desired Fiddler Request/Response
GET http://localhost:60171/_Test/status?_=1383341766884 HTTP/1.1
X-ProgressBar-TaskId: 838217998
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:60171/Report/Index
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Connection: Keep-Alive
DNT: 1
Host: localhost:60171
Pragma: no-cache
Cookie: ASP.NET_SessionId=rjli2jb0wyjrgxjqjsicdhdi; AspxAutoDetectCookieSupport=1; TTREPORTS_1_0=CC2A501EF499F9F...; __RequestVerificationToken=6klOoK6lSXR51zCVaDNhuaF6Blual0l8_JH1QTW9W6L-3LroNbyi6WvN6qiqv-PjqpCy7oEmNnAd9s0UONASmBQhUu8aechFYq7EXKzu7WSybObivq46djrE1lvkm6hNXgeLNLYmV0ORmGJeLWDyvA2
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcSUxlYXJuLlJlcG9ydHMuV2ViXHRydW5rXElMZWFybi5SZXBvcnRzLldlYlxfVGVzdFxzdGF0dXM=?=
X-Powered-By: ASP.NET
Date: Fri, 01 Nov 2013 21:37:48 GMT
Content-Length: 25
Iteration completed...
The only difference in the two requests headers besides the auth tokens is "Pragma: no-cache" in the request and the asp.net version in the response.
Thanks
Update - Code posted (I probably need to indicate this code originated from an article by Dino Esposito )
var ilProgressWorker = function () {
var that = {};
that._xhr = null;
that._taskId = 0;
that._timerId = 0;
that._progressUrl = "";
that._abortUrl = "";
that._interval = 500;
that._userDefinedProgressCallback = null;
that._taskCompletedCallback = null;
that._taskAbortedCallback = null;
that.createTaskId = function () {
var _minNumber = 100,
_maxNumber = 1000000000;
return _minNumber + Math.floor(Math.random() * _maxNumber);
};
// Set progress callback
that.callback = function (userCallback, completedCallback, abortedCallback) {
that._userDefinedProgressCallback = userCallback;
that._taskCompletedCallback = completedCallback;
that._taskAbortedCallback = abortedCallback;
return this;
};
// Set frequency of refresh
that.setInterval = function (interval) {
that._interval = interval;
return this;
};
// Abort the operation
that.abort = function () {
// if (_xhr !== null)
// _xhr.abort();
if (that._abortUrl != null && that._abortUrl != "") {
$.ajax({
url: that._abortUrl,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId }
});
}
};
// INTERNAL FUNCTION
that._internalProgressCallback = function () {
that._timerId = window.setTimeout(that._internalProgressCallback, that._interval);
$.ajax({
url: that._progressUrl,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId },
success: function (status) {
if (that._userDefinedProgressCallback != null)
that._userDefinedProgressCallback(status);
},
complete: function (data) {
var i=0;
},
});
};
// Invoke the URL and monitor its progress
that.start = function (url, progressUrl, abortUrl) {
that._taskId = that.createTaskId();
that._progressUrl = progressUrl;
that._abortUrl = abortUrl;
// Place the Ajax call
_xhr = $.ajax({
url: url,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId },
complete: function () {
if (_xhr.status != 0) return;
if (that._taskAbortedCallback != null)
that._taskAbortedCallback();
that.end();
},
success: function (data) {
if (that._taskCompletedCallback != null)
that._taskCompletedCallback(data);
that.end();
}
});
// Start the progress callback (if any)
if (that._userDefinedProgressCallback == null || that._progressUrl === "")
return this;
that._timerId = window.setTimeout(that._internalProgressCallback, that._interval);
};
// Finalize the task
that.end = function () {
that._taskId = 0;
window.clearTimeout(that._timerId);
}
return that;
};
Update 1 - Many thanks to John Saunders. I was able to locate this article that explains what John was implying in his comment about serialized access
"It blocks parallel execution and forces parallel requests to be executed one after another because the access to ASP.NET Session state is exclusive per session"
I found a fix at last. The session state can be controlled at the Controller and/or Controller Method level. Since the authorization is being verified at a higher level there is no need to use session in what I am doing. I simply disable it for the unit of work.
[SessionState(SessionStateBehavior.Disabled)]
public class _TestController : ProgressWorkerController

Categories