I encountered a weird problem. The same code runs on my local environment without any errors, but a time out error occurs when running on a test environment. I don’t know if anyone has encountered the same problem.
Just like the picture below, when I click the Batch Sync Product button, a method will be executed,
But there will be a time out error message, which makes me feel very confused
Unfortuantely you gave us no information about your code, but isn’t it the same as in your previous thread, Extending RunBaseBatch Class to Create and Run a Batch?
If so, you seem to have a problem with a web service you’re calling.
You might collect more information about your problem if you run your code in debugger.
class BatchSyncproduct_Class
{
/// <summary>
///单击按钮时批量同步产品,并修改状态字段值为Y
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormControlEventHandler(formControlStr(EcoResProductDetailsExtended, BatchSyncProduct), FormControlEventType::Clicked)]
public static void BatchSyncProduct_OnClicked(FormControl sender, FormControlEventArgs e)
{
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
CLRObject clrObj;
System.Exception ex;
System.Net.WebHeaderCollection httpHeader;
System.IO.Stream requestStream, responseStream;
System.IO.StreamWriter streamWriter;
EcoResProduct EcoResProducts;
EcoResProductTranslation EcoResProductTranslations;
INVENTTABLEMODULE INVENTTABLEMODULEs;
InventItemPurchSetup InventItemPurchSetups;
InventTable InventTables;
InventTable InventTabless;
int64 UpdateCountRows=0;
System.Net.HttpWebRequest requests;
System.Net.HttpWebResponse responses;
CLRObject clrObjs;
System.Net.WebHeaderCollection httpHeaders;
System.IO.Stream requestStreams, responseStreams;
System.IO.StreamWriter streamWriters;
;
try
{
while select ItemId,Product,isSyncToCRM from InventTables where InventTables.isSyncToCRM !="Y"
{
httpHeaders = new System.Net.WebHeaderCollection();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObjs = System.Net.WebRequest::Create("This url is hidden");
requests = clrObjs;
requests.set_Headers(httpHeaders);
requests.Method = "POST";
requests.ContentType = "application/x-www-form-urlencoded";
requestStreams = requests.GetRequestStream();
streamWriters = new System.IO.StreamWriter(requests.GetRequestStream());
streamWriters.Write("UserName=admin&Password=123");
streamWriters.Flush();
streamWriters.Close();
responses = requests.GetResponse();
System.IO.StreamReader streamReads = new System.IO.StreamReader(responses.GetResponseStream());
container test =str2con(streamReads.ReadToEnd());
str test1=conPeek(test,2);
str test2= subStr(test1,10,2000);
str te1='"';
str test3= strRem(test2,te1);
select firstonly RecId from EcoResProducts where EcoResProducts.RecId == InventTables.Product;;
select firstonly Description from EcoResProductTranslations where EcoResProductTranslations.Product == EcoResProducts.RecId;
select firstonly Price from INVENTTABLEMODULEs where INVENTTABLEMODULEs.ItemId== InventTables.ItemId&&INVENTTABLEMODULEs.ModuleType == 2;
select firstonly LeadTime,LowestQty from InventItemPurchSetups where InventItemPurchSetups.ItemId == InventTables.ItemId;
httpHeader = new System.Net.WebHeaderCollection();
new InteropPermission(InteropKind::ClrInterop).assert();
clrObj = System.Net.WebRequest::Create("This url is hidden");
request = clrObj;
httpHeader.Add("Authorization",test3);
request.set_Headers(httpHeader);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
requestStream = request.GetRequestStream();
streamWriter = new System.IO.StreamWriter(request.GetRequestStream());
streamWriter.Write("name="+EcoResProductTranslations.Name+"&stocknum="+InventTables.ItemId+"&sellingprice="+int2Str(real2int(INVENTTABLEMODULEs.price))+"&purchasetime="+int2Str(InventItemPurchSetups.LeadTime)+"&replacementnum="+InventTables.AltItemId+"&new_qdl="+int2Str(real2int(InventItemPurchSetups.LowestQty))+"&describe="+EcoResProductTranslations.Description+"&productstructure=1");
streamWriter.Flush();
streamWriter.Close();
response = request.GetResponse();
System.IO.StreamReader streamRead = new System.IO.StreamReader(response.GetResponseStream());
//info(streamRead.ReadToEnd());
ttsBegin;
select forUpdate InventTabless
where InventTabless.ItemId == InventTables.ItemId;
InventTabless.isSyncToCRM = "Y";
InventTabless.update();
ttsCommit;
UpdateCountRows++;
}
info(strFmt( "产品数据已同步成功,同步了%1条数据到CRM",UpdateCountRows));
}
catch
{
//exception
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
}
}
This is my code. The difference is that it is not run through the RunBaseBatch class this time, but runs directly when I click the button, but this piece of code runs in my local environment without any problems, but this piece of code is under test There will be a timeout error when running on the environment
All right, so we’re you two threads about the same error in the same code. Let’s discuss the timeout error here and abandon the discussion in the other thread.
First of all, you need to find out where the error gets thrown. I guess it’s caused by the web service being unresponsive.
Your own code isn’t very efficient and it surely can be improved (for instance, you could fetch all data by a single query, while you’re making several queries for every item), but I don’t think it’s the cause of the exception.
I have no way to debug my code in the test environment, how can I find out where the error is reported?
You can connect your DEV environment to the Test database and try your process again data from Test environment. See Debug a copy of the production database.
Also, didn’t yopu say that you can reproduce the error in your DEV environment as well just by ruinning your code in batch?
I have used the test environment database in the local development environment, but the time out error cannot be reproduced
Then debugging the batch may be a good idea. It’s likely both caused by the same problem.
Also, what about debugging the service called by your class? Does it get called at all? Is it responsive?
Also, what about debugging the service called by your class? Does it get called at all? Is it responsive?
Sorry, I don’t quite understand the meaning of this sentence
The purpose of your code is calling a web service or something (via HttpRequest). All I know about it is what you told us, which is nothing, therefore I can’t say anything more about it.
Anyway, you’re calling something and you’re getting a timeout exception, therefore it seems likely that this something doens’t respond. Therefore you should check out what’s going on - does it get called at all, or does something fail before even reaching the web service? Or does it get called but it gets stuck or too busy?
I just did a test. When I called this code, it was indeed running, and a timeout error occurred after 102 times of execution.
What does it mean? Does the something time out or not? Remember, you’re trying to locate the source of the error.
Maybe you run out of certain resources. There is indeed bug in your code - you’re not disposing the response (you’re also not disposing the request in case of a failure).
It can be easily done with the ‘using’ statement. It makes sure that resources are disposed regardless if things succeed or fail. For example:
using (HttpWebResponse response = request.GetResponse())
{
using (StreamReader streamRead = new StreamReader(response.GetResponseStream()))
{
}
}
Hello, Martin Dráb
I will now use the code example you gave. There is no problem using it locally. The code has been executed 1000 times in a loop, but when I use the same code in the test environment, the time out error occurs again.
I have now found the reason for the time out error.
The reason for the error seems to be that the token acquisition failed after the loop was executed 100 times.
The error is as follows:
error message “Can read the token but failed validating token with exception 'IDX10205: Issuer validation failed. Issuer: 'https://sts.windows.net/57206206-ec82-4579-9724-0a098ed1b99f/'. Did not match: validationParameters.ValidIssuer: 'null' or validationParameters.ValidIssuers: 'https://wkea-test.sandbox.operations.dynamics.com, 00000015-0000-0000-c000-000000000000, https://wkea-test.sandbox.operations.dynamics.com, https://sts.windows.net/64b7148c-0b9a-41a5-b758-dfd930b73a89/'.'
'System.IdentityModel.Tokens.Jwt'
at System.IdentityModel.Tokens.Validators.ValidateIssuer(String issuer, SecurityToken securityToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
at Microsoft.Dynamics.ApplicationPlatform.SystemSecurity.SignedJwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
The token validation failed. From confirguration allowed ValidIssuers : 'System.Collections.Generic.List`1[System.String]'
ValidAudiences : 'System.Linq.Enumerable+<ConcatIterator>d__59`1[System.String]'”
he token expired because of the session timeout?
You’ve never mentioned any token; your code doesn’t contain any reference to any token either. Therefore I can’t comment on your logic regarding tokens.
By the way, if you missed my previous reply, please read it now and then add proper resource dealllocation to your code. It might solve some of your problems.