问题
I'm currently writing a web application that provides some database manipulation. The system also needs to be capable working with different databases.The problem is that certain fields in the database have to be updated with data from a RESTful api. To perform these updates, I use a Quartz.NET schedule that makes a job for every table that needs to be updated.This job checks if it needs to update the relevant database. If it does, it gets the information from the REST-service. When I don't use multithreading, there are no problems with accessing the REST-serivce. But when I use Quartz.net, the server returns a 401 error for some requests. This behavior is inconstistent and doesn't always happen. I'm using RestSharper to make these calls with a custom Authenticator for digest authentication. I got this solution here:
http://www.ifjeffcandoit.com/2013/05/16/digest-authentication-with-restsharp/
I also use the auth fixer in that post.
Creating the jobs:
foreach (KeyValuePair<string, int> tabel in DataParser.getAllTabellenMetRefresh())
{
// define the job and tie it to our class
IJobDetail job = JobBuilder.Create<LocatieUpdater>()
.WithIdentity("dbUpdaterJob"+count, "group1")
.UsingJobData("type", tabel.Key)
.Build();
// Trigger the job to run now, and then every x seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger"+count, "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(tabel.Value)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
The response in this piece of code receives the 401.
var client = new RestClient(gegevens["Url"]);
client.Authenticator = new DigestAuthenticator(ConfigAcces.getCredentials()[0], ConfigAcces.getCredentials()[1]);
var request = new RestRequest(gegevens["request"], Method.GET);
request.AddParameter("fields", "all");
request.AddParameter(velden["TagId"], id);
IRestResponse response = client.Execute(request);
I looked at the request with fiddler, but didn't really see anything usefull. Any help or tips would be very welcome. Thanks in advance!
回答1:
Not entirely sure how this fixed it, but all authentication seems to work by disabling the use of the SyncronisationContext and making the method wait for the task.
var request = new RestRequest(gegevens["request"], Method.GET);
request.AddParameter("fields", "all");
request.AddParameter(velden["TagId"], id);
client.UseSynchronizationContext = false;
IRestResponse response = await client.ExecuteTaskAsync(request);
来源:https://stackoverflow.com/questions/29211964/authentication-failing-with-multiple-threads-c-sharp