I am encountering an intermittent issue in my ASP.NET MVC 5 application, where the following error occurs only in production, but not in my development environment (localhost):
A transport-level error has occurred when sending the request to the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
This error occurs when trying to execute the CadastrarOcorrenciaSolicitacao
and CadastrarAnexos
methods, with the CadastrarOcorrenciaSolicitacao
sometimes failing as well.
Here is the relevant code that is failing:
[HttpPost]
[ValidateInput(false)]
public JsonResult CadastraOcorrencia(FormCollection formValues, HttpPostedFileBase[] file, bool[] n, string inp_datini = "", string inp_datfim = "")
{
if (!User.Identity.IsAuthenticated)
return null;
object objJson;
JsonMessage response = new JsonMessage();
if (!Request.IsAjaxRequest())
{
objJson = new
{
isValid = false,
Msg = "Operação Cancelada! Erro 001.1 - Erro ao Cadastrar Ocorrência"
};
return Json(objJson, JsonRequestBehavior.AllowGet);
}
response.Msg = "";
int codigo = int.Parse(formValues["sesCOD"]);
var tituloOcorrencia = formValues["Item3.Titulo"];
var TextoOcorrencia = formValues["Item3.TextoOcorrencia"];
int statusOcorrencia = int.Parse(formValues["Item3.StatusCodigo"] as string);
int tipoOcorrencia = int.Parse(formValues["Item3.TipoOcorrenciaCodigo"] as string);
string posicaoCliente = "True";
int itemOcorrencia = int.Parse(formValues["Item3.OcorrenciaItem"] as string);
int prioridade = 0;
int.TryParse(formValues["Item3.Prioridade"] as string, out prioridade);
string dataPRVFechamento = formValues["Item3.DataPrevisaoFechamentoFMT"];
DateTime? dataHoraPrvFechamento = string.IsNullOrEmpty(dataPRVFechamento) ? (DateTime?)null : DateTime.ParseExact(dataPRVFechamento, "dd/MM/yyyy HH:mm", null);
DateTime? dat_ini = null;
DateTime? dat_fim = null;
DateTime dat_ini_ = new DateTime();
DateTime dat_fim_ = new DateTime();
if (!string.IsNullOrWhiteSpace(inp_datini))
{
bool res = DateTime.TryParse(inp_datini, out dat_ini_);
if (res)
{
dat_ini = dat_ini_;
}
}
if (!string.IsNullOrWhiteSpace(inp_datfim))
{
bool resp = DateTime.TryParse(inp_datfim, out dat_fim_);
if (resp)
{
dat_fim = dat_fim_;
}
}
if (TextoOcorrencia.Contains("ó"))
{
TextoOcorrencia = TextoOcorrencia.Replace("ó", "ó");
}
if (TextoOcorrencia.Contains("Ô"))
{
TextoOcorrencia = TextoOcorrencia.Replace("Ô", "Ô");
}
string xamlText = HtmlToXamlConverter.ConvertHtmlToXaml(TextoOcorrencia, false);
RtfConvert rc = new RtfConvert();
string textRtf = rc.XamlToRtf(xamlText);
byte[] obyte = Encoding.UTF8.GetBytes(textRtf);
var OUT_ITEM = new ObjectParameter("OUT_ITEM", typeof(System.Int32));
switch (posicaoCliente)
{
case "True": posicaoCliente = "S"; break;
case "False": posicaoCliente = "N"; break;
default: posicaoCliente = "N"; break;
}
using (var dbContext = new LazcoInternoEntities())
{
try
{
dbContext.CadastrarOcorrenciaSolicitacao("MNT", codigo, itemOcorrencia, User.Identity.Name, tituloOcorrencia,
statusOcorrencia, tipoOcorrencia, obyte, posicaoCliente, prioridade, dat_ini, dat_fim, dataHoraPrvFechamento, OUT_ITEM);
}
catch (Exception exceptionMessage)
{
// Log exception here...
objJson = new
{
isValid = false,
Msg = "Operação Cancelada! Erro 001.3 - Erro ao Cadastrar Ocorrência.",
Dev = SqlUtils.ExceptionDetails(exceptionMessage)
};
return Json(objJson, JsonRequestBehavior.AllowGet);
}
}
byte[] fileData = null;
using (var dbContext = new LazcoInternoEntities())
{
try
{
if (itemOcorrencia == 0 && Request.Files.Count > 0)
{
for (int i = 0; i < Request.Files.Count; i++)
{
var arq = Request.Files[i];
if (arq.ContentLength > 0 && arq.FileName != "")
{
fileData = new byte[arq.ContentLength];
using (var memoryStream = new MemoryStream())
{
arq.InputStream.CopyTo(memoryStream);
fileData = memoryStream.ToArray();
}
dbContext.CadastrarAnexos("MNT", codigo, 0, arq.FileName, "Arquivo de Solicitação na Web(Ocorrência)", "WEB", fileData, User.Identity.Name, Convert.ToInt32(OUT_ITEM.Value.ToString()));
}
}
}
}
catch (Exception e)
{
// Log exception here...
var objJsonFile = new
{
isValid = false,
Msg = "Operação Cancelada! Erro 001.2 - Ocorreu um Erro ao Salvar o Anexo!",
Dev = SqlUtils.ExceptionDetails(e)
};
return Json(objJsonFile, JsonRequestBehavior.AllowGet);
}
}
objJson = new
{
isValid = true,
Msg = "Ocorrência Cadastrada com Sucesso!"
};
return Json(objJson, JsonRequestBehavior.AllowGet);
}
Problem:
- The issue occurs when trying to execute the
CadastrarOcorrenciaSolicitacao
orCadastrarAnexos
methods, and the error mentioned happens on production (not on localhost). - The error seems related to the physical connection to the database, possibly due to database connection pooling, session issues, or long-running transactions.
What I have tried:
- I'm using
using
statements to ensuredbContext
is disposed of correctly. - I’m also catching exceptions and logging them, which might help track down the root cause.
- No issues are observed on localhost, but the error happens in production consistently.
Has anyone encountered this issue before and can suggest a solution or any pointers to investigate further?
What did you try and what were you expecting?
I tried uploading files to a stored procedure (CadastrarAnexos
) in an ASP.NET MVC 5 application. I expected the files to be uploaded successfully without any errors when calling the stored procedure.
However, I encountered the error message:
A transport-level error has occurred when sending the request to the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
This error occurs in the production environment while executing the file upload process, particularly when the stored procedure CadastrarAnexos
is called. The error does not occur in the local development environment.
What did you expect to happen?
I expected that the file upload to the stored procedure would execute without any errors and the files would be saved successfully in the database.
What actually resulted?
Instead, the error
A transport-level error has occurred when sending the request to the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
occurred, leading to failure in executing the stored procedure and uploading the files.
StackTrace:
" at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.Execute(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.EntityCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.EntityCommand.ExecuteNonQuery()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunctionCommand(EntityCommand entityCommand)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass49.<ExecuteFunction>b__47()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(String functionName, ObjectParameter[] parameters)
at LazcoInternoDbFirst.Models.LazcoInternoEntities.CadastrarAnexos(String iNP_STATUS, Nullable`1 iNP_CODIGO, Nullable`1 iNP_SEQUEN, String iNP_NOME, String iNP_ANEDES, String iNP_LOCAL, Byte[] iNP_OBJETO, String iNP_USUARI, Nullable`1 iNP_OCOITE)
at LazcoInternoDbFirst.Controllers.AtendimentoController.CadastraOcorrencia(FormCollection formValues, HttpPostedFileBase[] file, Boolean[] n, String inp_datini, String inp_datfim)"
Class: LazcoInternoEntities - Models\LazcoEntity.Context.cs
namespace LazcoInternoDbFirst.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;
public partial class LazcoInternoEntities : DbContext
{
public LazcoInternoEntities()
: base("name=LazcoInternoEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
public virtual DbSet<LZ_MINT_USU01> LZ_MINT_USU01 { get; set; }
//all stored procedure function imports "public virtual"
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744734262a4590647.html
评论列表(0条)