Estoy en mi extremo de los ingenios en este caso. Ocasionalmente recibo el error anterior de mi servicio web .Net 2.0 asmx. Tengo el XmlInclude adecuada() en su lugar, y que sólo aparece a veces - cuando uno vuelve a edificar y actualizar el sitio, puede que se muestre, no puede, sin orden ni concierto. Si muevo algunos de los XmlIncludes(), los reconstruyo y los realizo, el error por lo general desaparece.System.InvalidOperationException: el tipo [XYZ] no se puede utilizar en este contexto. Error confirmado
Antes de tener el proceso de compilación en su lugar que convierte todo a una DLL, estaba usando el buen método de implementación de xcopy. El error ocurrió entonces, también, pero entonces todo lo que tenía que hacer era añadir un espacio para el archivo que define todo el XmlInclude() llama, e IIS sería recompilar y el error se iría.
Por lo que vale, hay un montón de XmlIncludes definidos, alrededor de 100 o menos.
¿Alguna idea?
He aquí un fragmento:
namespace Courses{
[Serializable]
[XmlInclude(typeof(UserToCourse)),
XmlInclude(typeof(UserToCourseCollection)),
// ...lots more....
XmlInclude(typeof(ReadOnlySearchResultsRecordset<UserToCourse, UserToCourseCollection>)),
XmlInclude(typeof(AllCoursesByTrainingProgramCollection)),
XmlInclude(typeof(StartupObject))]
public partial class ServiceCallResult{
//..snipped class def
}
}
Editar: Parece que la reordenación de los XmlIncludes hace que el error desaparezca, pero puede o no volver la próxima vez que vuelva a compilar y volver a implementar.
Edit # 2: OK, algunos más detalles. Forzar un reciclaje al cambiar el archivo web.config no resuelve el problema ni reinicia completamente IIS. Por algún motivo, mi registro no se escribió correctamente, por lo que aún no tengo el seguimiento de la pila.
Esta vez, el error se estaba produciendo para 2 métodos específicos. Realicé un cambio en el archivo global.asax (para intentar corregir el registro de seguimiento de mi pila), reconstruí y actualicé, y uno de los dos métodos comenzó a funcionar. Luego dividí la clase con XmlIncludes en dos clases parciales, reconstruí, actualicé y ambos métodos comenzaron a funcionar nuevamente. No estoy seguro de si esto es una solución permanente o no en este momento, porque es muy aleatorio; Actualizaré en el próximo ciclo de compilación de nuevo.
Edit # 3: Definitivamente no es una solución permanente, y todavía no estoy enganchado en el lugar correcto para atrapar un rastro de pila completo (aunque mis otros registros están funcionando bien). Ugh. Actualizaré nuevamente la próxima ronda.
Edit # 4: Finalmente tenemos un seguimiento de pila. No captura en Visual Studio, ni en el controlador de excepción global en mi global.asax. Estos son los resultados que se muestran cuando se invoca el método directamente desde el navegador web:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.String[] may not be used in this context.
at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write119_ServiceCallResult(String n, String ns, ServiceCallResult o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write397_ServiceCallResult(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.ServiceCallResultSerializer277.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
Edición # 5:
Esto puede ser un síntoma del error anterior, así que no estoy convencido de que es relevante, pero lo publicaré de todos modos. Si hay que adjuntar a los Asistentes de depuración gestionadas y refrescar un montón, al final me sale:
Managed Debugging Assistant 'StreamWriterBufferedDataLost' has detected a problem in 'C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.EXE'.
Additional Information: A StreamWriter was not closed and all buffered data within that StreamWriter was not flushed to the underlying stream. (This was detected when the StreamWriter was finalized with data in its buffer.) A portion of the data was lost. Consider one of calling Close(), Flush(), setting the StreamWriter's AutoFlush property to true, or allocating the StreamWriter with a "using" statement. Stream type: System.Web.HttpResponseStream
File name: <unknown>
Allocated from:
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.IO.StreamWriter.Init(Stream stream, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(Stream stream, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(Stream stream, Encoding encoding)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
at System.Web.Services.Protocols.SyncSessionlessHandler.ProcessRequest(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.HandlerWrapper.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
at System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(Exception error)
at System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
at System.Web.HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
at System.Web.HttpRuntime.ProcessRequest(HttpWorkerRequest wr)
at Microsoft.VisualStudio.WebHost.Request.Process()
at Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Connection conn)
No estoy seguro de que está relacionado ... podría ser sólo el flujo de error.
Edit # 6:
OK, más información. Utilicé la publicación de blog de Scott Hanselman here para entrar en el ensamblaje generado. Resulta que a pesar del XmlInclude, el ensamblaje generado NO tiene una referencia al tipo, por lo que definitivamente es un error en .NET. Estoy intentando rastrear qué lo desencadena, pero algo en lo que genere los ensamblados de salida (sgen?) Está fallando.
Edición # 7:
FYI para cualquier persona después de esto, me he presentado un informe de error a MS:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523253
Además, exactamente el mismo código actualizado en 2 servidores diferentes: uno puede funcionar, el otro puede que no, es un disparate completo. – jvenema
¿Podría publicar el código que causa el error? Debería tratar de aislar el error en el caso más simple posible. – empi
No puedo, esa es la cuestión ... el mismo código que funciona en una máquina puede funcionar en otra, y puede que no, no hay forma de saberlo. Por lo que puedo decir, está relacionado con algún tipo de almacenamiento en caché de IIS. – jvenema