-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hybrid Async Scoped MVC 5 + Async Method bug #983
Comments
Hi @leonardolb, I've got trouble reading the images on my mobile phone. Would you mind replacing them with actual text, as was requested in the bug report template you used while filling in your question? Thanks in advance |
And don't forget to post a full stack trace |
Sorry about that!! Im facing this exception:
My stacktrace is:
Everything start at my PersonController "Detail" Action. It async calls "ConsultarCNPJ" in my context class public async Task<JsonResult> Detail(PessoaModel model, PessoaDadoPessoaConfigModel documento)
{
if (model == null)
model = new PessoaModel();
var service = base.CreateContext<IPessoaContext>();
if (documento != null)
{
var consultarCnpj = false;
if (consultarCnpj.False())
model = service.ObterNovo(documento);
else
**_model = await service.ConsultarCNPJ(documento);_**
}
else if (model.State.In(ModelStateType.Normal))
model = service.ObterDetalhes(model);
return AjaxViewResult(new PessoaViewModel(model));
} "ConsultarCNPJ" is a simple method that triggers a mediator (ThirdPartyCommandMediator) and awaits all available commands complete (just one expected for this ConsultarCNPJThirdPartyCommand). When finished, base.CreateBO().List is called to continue some procedures. base.CreateBO().List is not async and it goes to directly to database (oracle). CreateBO method is just a wrapper to simpleinjector Container.Resolve. public async Task<PessoaModel> ConsultarCNPJ(PessoaDadoPessoaConfigModel documento)
{
var command = new ConsultarCNPJThirdPartyCommand
{
CNPJ = documento.dsValor.RemoveSpecialCharacters().RemoveDiacritics()
};
var results = await **ThirdPartyCommandMediator**.Handle(command).ConfigureAwait(false);
var queryData = results[0];
if (queryData.Success)
{
var pessoa = queryData.Command.ReturnValue.Pessoa;
var documentosPessoa = **base.CreateBO<IPersonDataConfigBO>().List**(
new PaisModel { sqPais = PaisModel.Brasil },
TipoPessoa.Juridica,
Situacao.Ativo).Select(sel => new PessoaDadoPessoaConfigModel
{
DadoPessoaConfig = sel,
State = ModelStateType.New
}).ToList();
} I'm using "Lifestyle = Lifestyle.CreateHybrid(new SimpleInjector.Lifestyles.AsyncScopedLifestyle(), new WebRequestLifestyle())" as my lifestyle. Finally, this is the Handler triggered when ThirdPartyCommandMediator.Handle() is invoked public override async Task<ThirdPartyCommandHandlerContext<ConsultarCNPJThirdPartyCommand>>
Handle(ThirdPartyCommandHandlerContext<ConsultarCNPJThirdPartyCommand> commandContext)
{
var targetApp = new SERPROPartyApp(commandContext.Context);
var command = commandContext.Command;
if (targetApp.BaseUrl.IsNullOrWhiteSpace())
return commandContext;
else
{
var client = new SERPRORestClient(targetApp.Username, targetApp.Password, targetApp.BaseUrl);
var dados = await client.Consultar(command.CNPJ).ConfigureAwait(false);
}
}
public async Task<CNPJ> Consultar(string cnpj)
{
var request = new RestRequest("/consulta-cnpj-df/v2/basica/" + cnpj, Method.Get);
var response = await this._client.ExecuteAsync<CNPJ>(request).ConfigureAwait(false);
return response?.Data;
} |
Can you debug through your application and determine exactly after which line of code the scope is gone? e.g. is that after:
|
Yes! var documentosPessoa = base.CreateBO().List in ConsultarCNPJ method. |
Sorry, that's not what I meant. It's clear to me where the exception is thrown, but there is a point in the application that Simple Injector's In order to test this, you might need to make some temporary adjustments to your code; the easiest way to test is by calling into the container to resolve a scoped dependency, for instance just after the lines of code I mentioned above. |
Sorry my misunderstanding! I've debuged my code, scope is gone after
|
Unfortunately I can't debug further into ExecuteAsync as its is a method from RestSharp.RestClient class. |
That likely means that there is something happening within RestSharp.RestClient that causes the asynchronous context not to flow. The most likely case is that it is using an What you can do instead is make sure that all your dependencies for that scope are resolved before this point. This means stepping away from lazy loading your dependencies, as you are doing right now. |
Thanks! I'll step away from lazyloading so! thanks! |
I didn't expect this.
You don't have to worry about that. This only holds when building client applications such as WPF and Win Forms. This doesn't hold for ASP.NET. AFAIK, It will never deadlock on this. |
Maybe i need to remove other ConfigureAwait I have so, maybe they are causing some trouble too i guess |
Hi!
i'm facing this error and I really can't go through it.
My code:
it start at this action:
goes to this block
and exception is thrown at the highlighted part of code.
Can anyone help me?
this is my lifestyle
It's an ASP.NET MVC5 app that runs some background tasks, WebAPI controllers and SignalR.
thanks!!
The text was updated successfully, but these errors were encountered: