我创建了一个其他网站可以用来在我的数据库中存储错误的Web服务.然后,他们可以访问我的网站查看他们的错误,搜索错误,过滤错误等.但是,我的Web服务出现以下错误:
System.Web.HttpContext无法序列化,因为它没有无参数构造函数.
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.InvalidOperationException:System.Web.HttpContext无法序列化,因为它没有无参数构造函数.
Web服务包含以下功能:
[WebMethod]
public static void LogError(HttpContext context, Exception exception, string APIKey)
{
//Log the error
}
使用Web服务记录异常的外部站点在global.asax文件中包含以下代码:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
WebService.Errors.ErrorHandler.LogError(HttpContext.Current, Server.GetLastError(), "NOLDFHOI");
}
如何从Web站点将HttpContext从其站点获取到我的函数中?
我想编写一个单元测试来测试一个名为UploadedFile的类的功能.
我面临的问题是这个类的静态构造函数使用HttpContext.Current属性,因为我从类库运行我的单元测试,我在测试时没有HttpContext.
看看我的静态构造函数:
static UploadedFile()
{
if (HttpContext.Current == null)
throw new Exception("web server not available");
HttpServerUtility server = HttpContext.Current.Server;
// SET UploadedFileMappingFile Names:
_resourceFileNames = new StringDictionary();
_resourceFileNames[_suppoertedFileStructures] = server.MapPath(SupportedUploadedFileStructures);
_resourceFileNames[_supportedFileStructuresXSD] = server.MapPath(SupportedUploadedFileStructuresXSD);
_resourceFileNames[UploadedFileEnum.UploadedFileFormatENUM.CSV.ToString()] = server.MapPath(UploadedFileColumnMap);
}
Run Code Online (Sandbox Code Playgroud)
我应该在我的测试环境中做什么,这样才HttpContext.Current不会为null,我可以成功设置它:
HttpServerUtility server = HttpContext.Current.Server;
Run Code Online (Sandbox Code Playgroud) 我从这里使用代码,我得到以下错误:无法使用HttpContext.Current.Server.MapPath()
在Visual Studio 2008中,当您缺少引用时,ContextMenuEntry"Solve"会帮助您吗?
我已经发现这HttpContext不是System.Web我IDE中的成员.根据帮助>信息我使用的是.NET 3.5 SP1.
我该如何运行?
在这种情况下你通常如何反应?你在msdn.com找什么东西?
由于我在使用Moq框架(ASP.NET MVC -使用Moq框架测试RenderPartialViewToString()的单元测试RenderPartialViewToString())时遇到问题,我正在考虑直接获取控制器,而不使用Moq进行这些特定测试,但是,如何在不使用任何Moq框架的情况下为我的测试模拟(或设置)HttpContext?
我需要能够做类似的事情,当然没有Moq:
var mockHttpContext = new Mock<ControllerContext>();
mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("n1\\test");
mockHttpContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
如何在不保存到数据库的情况下在ObjectContext中存储临时项?
上下文存储在HttpContext中,按类提供:
public static class HttpContextExtension
{
public static MyEntityDataModelContainer GetMyContext(this HttpContext httpContext)
{
if (httpContext.Items["MyEntityDataModelContainer"] == null)
{
httpContext.Items.Add("MyEntityDataModelContainer", new MyEntityDataModelContainer());
}
return (MyEntityDataModelContainer)httpContext.Items["MyEntityDataModelContainer"];
}
}
Run Code Online (Sandbox Code Playgroud)
有两个空页:1)FirstPage.aspx.cs:
public class FirstPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// crete new item
MyEntity newTemporaryItem = new MyEntity { MyEntityID = Guid.NewGuid() };
// attach them to Context
HttpContext.Current.GetMyContext().MyEntitySet.Attach(newTemporaryItem);
// save changes
HttpContext.Current.GetMyContext().SaveChanges();
// get all attached to Context items
var addedItems = (from se in HttpContext.Current.GetMyContext().ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged)
where …Run Code Online (Sandbox Code Playgroud) public void getContent() {
string VirtualPath = "~/Content.aspx";
var page = BuildManager.CreateInstanceFromVirtualPath( VirtualPath, typeof( Page ) ) as IHttpHandler;
page.ProcessRequest( HttpContext.Current );
}
Run Code Online (Sandbox Code Playgroud)
我正在使用该函数从不同的文件加载内容,但"page.ProcessRequest(HttpContext.Current)"在当前上下文中插入内容,我需要的是返回指定文件内容的函数.
我想知道是否有一种工作方式来创建一个新的HttpContext,因此"page.ProcessRequest"不会在当前响应中插入任何内容.
我们有一个MVC 4项目,并使用Structure Map作为IoC框架.当我们对使用DbContext的服务类进行异步调用时,它会抛出异常:
Value cannot be null.
Parameter name: httpContext
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext
Run Code Online (Sandbox Code Playgroud)
一旦抛出这个:
The operation cannot be completed because the DbContext has been disposed.
Run Code Online (Sandbox Code Playgroud)
我怎么能纠正这个?
我有两个方法,它们使用HttpContext.Current来获取userID.当我单独调用这些方法时,我得到userID,但是当使用Parallel.Invoke()调用相同的方法时,HttpContext.Current为null.
我知道原因,我只是在寻找可以访问HttpContext.Current的工作.我知道这不是线程安全的,但我只想执行读操作
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Display();
Display2();
Parallel.Invoke(Display, Display2);
}
public void Display()
{
if (HttpContext.Current != null)
{
Response.Write("Method 1" + HttpContext.Current.User.Identity.Name);
}
else
{
Response.Write("Method 1 Unknown" );
}
}
public void Display2()
{
if (HttpContext.Current != null)
{
Response.Write("Method 2" + HttpContext.Current.User.Identity.Name);
}
else
{
Response.Write("Method 2 Unknown");
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我是 HangFire 的初学者,期待使用 HangFire 每月在我的网络应用程序中调用一些操作。但是这些操作需要 HttpContext。
那么我的问题是:有没有办法在 HangFire 项目中添加(或创建)一个 httpcontext?
我试图谷歌但没有合适的答案。谢谢你的帮助!
我找到了一个简短的讨论。遗憾地看到,答案是“没有办法”。更新:参考https://discuss.hangfire.io/t/passing-site-url-to-hangfire-recurrent-jobs/2641
当我们想在 ASP.NET Core 的管道中将对象序列化为 JSON 字符串时,我们需要使用HttpContext.Response.Body.WriteAsync,除非我遗漏了一些东西,因为没有Result可以轻松用于分配JsonResult对象的属性。
除非有更好的替代方案,否则使用上述方法究竟是如何实现序列化的?
注意: JSON 序列化程序的选项应与 ASP.NET Core 3.1 中使用的(默认)选项相同。
如果需要(不是在我们的例子中),它们可以通过IServiceCollection.AddJsonOptions中间件进行修改。
例子:
app.Use( next =>
{
return async context =>
{
if (<someFunkyConditionalExample>)
{
// serialize a JSON object as the response's content, returned to the end-user.
// this should use ASP.NET Core 3.1's defaults for JSON Serialization.
}
else
{
await next(context);
}
};
});
Run Code Online (Sandbox Code Playgroud) httpcontext ×10
asp.net ×6
c# ×6
.net ×3
unit-testing ×2
asp.net-core ×1
asp.net-mvc ×1
dbcontext ×1
hangfire ×1
json ×1
structuremap ×1
web-services ×1