C# 类型';System.Web.HttpContextWrapper';在组装中';System.Web';未标记为可序列化

C# 类型';System.Web.HttpContextWrapper';在组装中';System.Web';未标记为可序列化,c#,smo,C#,Smo,我正在尝试在web应用程序中使用Microsoft.SqlServer.Management.Smo.Server执行一些查询。但它抛出以下异常: Type 'System.Web.HttpContextWrapper' in assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable. 以下是我的代码(简化以获得更多

我正在尝试在web应用程序中使用Microsoft.SqlServer.Management.Smo.Server执行一些查询。但它抛出以下异常:

Type 'System.Web.HttpContextWrapper' in assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.
以下是我的代码(简化以获得更多许可):

这是堆栈跟踪:

[SerializationException: Type 'System.Web.HttpContextWrapper' in assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.]
   System.AppDomain.get_Id() +0
   <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie) +136
   <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) +36
   <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) +359
   <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +76

[ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
]
   <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException) +53
   <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +191
   .cctor() +33

[TypeInitializationException: The type initializer for '<Module>' threw an exception.]
   Microsoft.SqlServer.Management.Common.ExecuteBatch.GetStatements(String sqlCommand) +0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +142
   System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) +1573
   Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse) +472
   Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry) +84
   Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand) +18
   MyApplication.Controllers.HomeController.Index() in D:\MyApplication\Controllers\HomeController.cs:62
   lambda_method(Closure , ControllerBase , Object[] ) +87
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +229
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +72
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +68
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +68
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128
[SerializationException:程序集'System.Web,版本=4.0.0.0,区域性=中立,PublicKeyToken=b03f5f7f11d50a3a'中的类型'System.Web.HttpContextWrapper'未标记为可序列化。]
System.AppDomain.get_Id()+0
.DoCallBackInDefaultDomain(IntPtr函数,Void*cookie)+136
.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport*)+36
.LanguageSupport._初始化(LanguageSupport*)+359
.LanguageSupport.Initialize(LanguageSupport*)+76
[MultReloDeXPcExc:C++模块在初始化默认AppDe域时加载失败。
]
.ThrowModuleLoadException(字符串errorMessage,Exception innerException)+53
.LanguageSupport.Initialize(LanguageSupport*)+191
.cctor()+33
[TypeInitializationException:“”的类型初始值设定项引发异常。]
Microsoft.SqlServer.Management.Common.ExecuteBatch.GetStatements(字符串sqlCommand)+0
[TargetInvocationException:调用的目标已引发异常。]
System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数)+0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象obj,对象[]参数,对象[]参数)+128
System.Reflection.RuntimeMethodInfo.Invoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性)+142
System.RuntimeType.InvokeMber(字符串名称、BindingFlags BindingFlags、绑定器绑定器、对象目标、对象[]提供的参数、参数修改器[]修饰符、CultureInfo区域性、字符串[]namedParams)+1573
Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(字符串查询、ExecutionTypes executionType、Int32&statementsToReverse)+472
Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand,ExecutionTypes executionType,布尔重试)+84
Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteOnQuery(字符串sqlCommand)+18
D:\MyApplication\Controllers\HomeController.cs:62中的MyApplication.Controllers.HomeController.Index()
lambda_方法(闭包、控制器基、对象[])+87
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)+229
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)+35
System.Web.Mvc.Async.AsyncControllerActionInvoker.b_u39(IAsyncResult asyncResult,ActionInvoking innerInvokeState)+39
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)+70
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+42
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()+72
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.c__DisplayClass46.b__3f()+387
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+42
System.Web.Mvc.Async.c__显示类2b.b__1c()+38
System.Web.Mvc.Async.c__显示类21.b__1e(IAsyncResult asyncResult)+188
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+38
System.Web.Mvc.Controller.b_u1d(IAsyncResult asyncResult,ExecuteCorerate innerState)+29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+68
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+39
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+38
System.Web.Mvc.MvcHandler.b_uu5(IAsyncResult asyncResult,ProcessRequestState innerState)+43
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+68
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+602
System.Web.HttpApplication.executesteppl(IExecutionStep步骤)+195
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+128
代码在干净的项目(web应用程序或控制台应用程序)中运行时没有任何问题。但在我真正的项目中,抛出异常。我尝试了软件包的
140.17283.0
版本和
150.18068.0-preview

[SerializationException: Type 'System.Web.HttpContextWrapper' in assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.]
   System.AppDomain.get_Id() +0
   <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie) +136
   <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) +36
   <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) +359
   <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +76

[ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
]
   <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException) +53
   <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) +191
   .cctor() +33

[TypeInitializationException: The type initializer for '<Module>' threw an exception.]
   Microsoft.SqlServer.Management.Common.ExecuteBatch.GetStatements(String sqlCommand) +0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +142
   System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) +1573
   Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse) +472
   Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry) +84
   Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand) +18
   MyApplication.Controllers.HomeController.Index() in D:\MyApplication\Controllers\HomeController.cs:62
   lambda_method(Closure , ControllerBase , Object[] ) +87
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +229
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +72
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +68
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +68
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128