简体   繁体   English

System.InvalidOperationException:类型[XYZ]可能不在此上下文中使用。 BUG确认

[英]System.InvalidOperationException: The type [XYZ] may not be used in this context. BUG Confirmed

I'm at my wits end on this one. 在这个问题上,我的斗智斗勇。 I occasionally get the error above from my .Net 2.0 asmx web service. 我偶尔会从我的.Net 2.0 asmx Web服务中得到上述错误。 I've got the proper XmlInclude() in place, and it only appears sometimes - when I rebuild and update the site, it may show up, it may not, no rhyme or reason. 我有适当的XmlInclude(),它只出现在有时 - 当我重建和更新网站时,它可能会显示,它可能不会,没有押韵或理由。 If I move some of the XmlIncludes() around, rebuild, and push the changes up, the error usually goes away. 如果我移动一些XmlIncludes(),重建并推送更改,则错误通常会消失。

Prior to having the build process in place that converts everything to a DLL, I was using the good ol xcopy deployment method. 在将所有内容转换为DLL的构建过程之前,我使用了良好的ol xcopy部署方法。 The error happened then, too, but then all I had to do was add a space to the file that defined all the XmlInclude() calls, and IIS would recompile and the error would go away. 然后发生了错误,但是我所要做的就是在定义所有XmlInclude()调用的文件中添加一个空格,IIS将重新编译,错误就会消失。

For what its worth, there are a LOT of XmlIncludes defined, about 100 or so. 对于它的价值,有很多XmlIncludes定义,大约100左右。

Any ideas? 有任何想法吗?

Here's a snippet: 这是一个片段:

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
    }
}

Edit: It appears that rearranging the XmlIncludes makes the error go away, but it may or may not come back the next time I recompile and redeploy. 编辑:重新排列XmlIncludes似乎使错误消失,但下次重新编译和重新部署时它可能会或可能不会返回。

Edit #2: OK, some more details. 编辑#2:好的,更多细节。 Forcing a recycle by changing the web.config doesn't solve the issue, nor does completely restarting IIS. 通过更改web.config强制回收不能解决问题,也不会完全重新启动IIS。 For some reason, my log didn't write out properly, so I don't have the stack trace yet. 由于某种原因,我的日志没有正确写出,所以我还没有堆栈跟踪。

This time, the error was occurring for 2 specific methods. 这次,2个特定方法发生了错误。 I made a change to the global.asax (to attempt to fix my stack trace logging), rebuilt and updated, and one of the two methods started working. 我对global.asax进行了更改(尝试修复我的堆栈跟踪日志记录),重建并更新,并且两种方法中的一种开始工作。 I then split the class with the XmlIncludes on it into 2 partial classes, rebuilt, updated, and both methods started working again. 然后我将带有XmlIncludes的类拆分为2个部分类,重建,更新,两个方法再次开始工作。 I'm not sure if this is a permanent fix or not at this moment, because it's so random; 我不确定这是不是永久性修复,因为它是如此随机; I'll update on the next build cycle again. 我将再次更新下一个构建周期。

Edit #3: Definitely not a permanent fix, and I'm still not hooked into the right place to catch a full stack trace (although my other logs are all working fine). 编辑#3:绝对不是一个永久修复,我仍然没有挂钩到正确的位置来捕获完整的堆栈跟踪(虽然我的其他日志都工作正常)。 Ugh. 啊。 I'll update again next round. 我将在下一轮再次更新。

Edit #4: Finally have a stack trace. 编辑#4:最后有一个堆栈跟踪。 It doesn't catch in Visual Studio, nor in the global exception handler in my global.asax. 它不会在Visual Studio中捕获,也不会在我的global.asax中的全局异常处理程序中捕获。 Here are the results as displayed when invoking the method directly from the web browser: 以下是直接从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()

Edit #5: 编辑#5:

This may be a symptom of the above error, so I'm not convinced it's relevant, but I'll post it anyway. 这可能是上述错误的症状,所以我不相信它是相关的,但无论如何我都会发布它。 If I attach to the Managed Debug Assistants and refresh a bunch, I eventually get: 如果我附加到托管调试助手并刷新一堆,我最终得到:

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)

I'm not sure it's related...could be just the error stream. 我不确定它是否相关......可能只是错误流。

Edit #6: 编辑#6:

OK, more info. 好的,更多信息。 I've used Scott Hanselman's blog post here to step into the generated assembly. 我用斯科特Hanselman的博客文章在这里步入生成的程序集。 It turns out that despite the XmlInclude, the generated assembly DOES NOT have a reference to the type in it, so this is definitely a bug in .NET. 事实证明,尽管有XmlInclude,生成的程序集也没有对其中的类型的引用,所以这绝对是.NET中的一个错误。 I'm attempting to track down what triggers it, but something in whatever generates the output assemblies (sgen?) is failing. 我试图追踪触发它的是什么,但是产生输出组件(sgen?)的东西都失败了。

Edit #7: 编辑#7:

FYI for anyone following this, I've submitted a bug report to MS: 对于此后的任何人,我都向MS提交了一份错误报告:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523253 https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523253

My best guess (and it is a guess) is that touching the file is a red herring. 我最好的猜测(这是一个猜测)是触摸文件是一个红色的鲱鱼。 I think it more likely that any recompilation will clear this up. 我认为任何重新编译都更有可能清除这一点。 For example, you could touch the web.config to force a recycle to prove or disprove my assumption when this happens. 例如,您可以触摸web.config以强制循环来证明或反驳我的假设。

The reason I suspect this is that the error you are receiving relates to a serialization problem. 我怀疑这是因为您收到的错误与序列化问题有关。 The XML Serializer is confused into thinking it cannot serialize one of your types. XML Serializer很困惑,认为它无法序列化您的某个类型。 Because you've ruled out a lot of the common suspects such as object arrays or inclusion of other types that are inherently not serializable, I suspect a compliation race condition such as a circular reference between two of your assemblies is to blame. 因为你已经排除了许多常见的嫌疑,例如对象数组或包含其他本质上不可序列化的类型,我怀疑一个称赞竞争条件,例如你的两个程序集之间的循环引用是罪魁祸首。 This particular case would be cleared up by a second compilation. 这种特殊情况将通过第二次汇编清除。

Note: A good tool to detect circular references is NDepend . 注意:检测循环引用的一个好工具是NDepend

If it is not a circular reference, are you doing any code gen, using any build providers or using reflection to load any assemblies in your app or anything else that might be even slightly exotic? 如果它不是循环引用,您是在使用任何代码生成器,使用任何构建提供程序还是使用反射来加载应用程序中的任何程序集或其他任何可能有点异国情调的程序集?

Edit: 编辑:

Based on your comment, you are not doing anything exotic. 根据你的评论,你没有做任何异国情调。 So, please check for circular references and (just thought of this) conflicting dependencies between your assemblies. 因此,请检查循环引用和(只是想到这一点)程序集之间存在冲突的依赖关系。 For example, SubSonic references a number of assemblies , and if you reference one of these assemblies of a different version, it could explain how it works once and fails another time with the same code. 例如, SubSonic引用了许多程序集 ,如果您引用其中一个不同版本的程序集,它可以解释它是如何工作一次的,并使用相同的代码再次失败。

OK, I've got a solution that works, although I'm still not sure why. 好的,我有一个有效的解决方案,虽然我还不确定为什么。 This is definitely a bug in the generated proxy assembly. 这绝对是生成的代理程序集中的错误。 I found that the generated assembly sometimes just misses some of the types included in the XmlIncludes. 我发现生成的程序集有时会错过XmlIncludes中包含的某些类型。 Oddly, it seemed to be certain types specifically, though I can't find a pattern to why those ones and not others. 奇怪的是,它似乎是特定的类型,虽然我找不到一个模式为什么那些而不是其他人。

The solution was to create a partial class definition (it's even in the same file!) that had the XmlIncludes for only those particular types that were consistently causing problems. 解决方案是创建一个部分类定义(它甚至在同一个文件中!), 它只包含那些一直导致问题的特定类型的XmlIncludes。 Since then, I haven't seen the error at all. 从那以后,我根本没有看到错误。

Definitely a bug somewhere in the generator, although what's triggering it I don't know. 绝对是发电机某处的一个错误,虽然触发它的是什么,我不知道。 Hopefully this'll help someone else down the road though. 希望这会帮助其他人在路上。

EDIT I belive I've confirmed what caused the problem. 编辑 我相信我已经确认是什么导致了这个问题。 I had a reference to SharpZipLib, built for the .NET framework v1.1, in my .NET 2.0 project. 我在.NET 2.0项目中引用了为.NET框架v1.1构建的SharpZipLib。 When I built the app_code.dll, it added a reference to mscorlib 1.0.5. 当我构建app_code.dll时,它添加了对mscorlib 1.0.5的引用。 Apparently, having this additional reference was enough to cause sgen to improperly generate the temporary DLLs used when invoking the web service. 显然,有这个额外的引用足以导致sgen不正确地生成调用Web服务时使用的临时DLL。 So, if you have the problem...load up all referenced assemblies in ILDASM, double click the manifest, and confirm that none of them reference .NET 1.1. 因此,如果您遇到问题...在ILDASM中加载所有引用的程序集,请双击清单,并确认它们都不引用.NET 1.1。 If they do...you're hosed. 如果他们这样做......你就被软管了。

Apparently, this did not actually fix it, as the issue is still in place, just with a different type failing. 显然,这实际上没有解决它,因为问题仍然存在,只是一个不同的类型失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidOperationException:实体类型User不是当前上下文的模型的一部分 - System.InvalidOperationException: The entity type User is not part of the model for the current context EntitySet System.InvalidOperationException - “实体类型不是当前上下文模型的一部分” - EntitySet System.InvalidOperationException - “the entity type is not part of the model for the current context” 启动线程返回System.InvalidOperationException:当前的SynchronizationContext不能用作TaskScheduler - Start thread returns System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler DateTime,类型&#39;System.InvalidOperationException&#39;的例外 - DateTime, an exception of type 'System.InvalidOperationException' &#39;accessibilityobject&#39;引发了&#39;system.invalidoperationexception&#39;类型的异常 - 'accessibilityobject' threw an exception of type 'system.invalidoperationexception' System.InvalidOperationException:集合已修改; 枚举操作可能无法执行 - System.InvalidOperationException: Collection was modified; enumeration operation may not execute System.InvalidOperationException: &#39;集合已修改; 枚举操作可能无法执行。 - System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' System.InvalidOperationException 保存 - System.InvalidOperationException on saving Nunit System.InvalidOperationException - Nunit System.InvalidOperationException System.InvalidOperationException Android - System.InvalidOperationException Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM