这是一个与C#MVC2 Jqgrid相关的问题- 进行服务器端分页的正确方法是什么?我在哪里询问并找到了如何在2000行左右的表上提高查询性能.性能从10秒提高到1秒.
现在我正在尝试执行完全相同的查询,其中表有20,000行 - 查询需要30秒.我该如何进一步改进?2万行仍然不是一个巨大的数字.
我有一些可能的想法是:
这是2万行需要30秒的MVC操作:(参数由jqgrid提供,其中sidx =哪个排序列,sord =排序顺序)
public ActionResult GetProductList(int page, int rows, string sidx, string sord,
string searchOper, string searchField, string searchString)
{
if (sidx == "Id") { sidx = "ProductCode"; }
var pagedData = _productService.GetPaged(sidx, sord, page, rows);
var model = (from p in pagedData.Page<Product>()
select new
{
p.Id, p.ProductCode, p.ProductDescription,
Barcode = p.Barcode ?? string.Empty,
UnitOfMeasure = p.UnitOfMeasure != null …Run Code Online (Sandbox Code Playgroud) 给定基本存储库接口:
public interface IPersonRepository
{
void AddPerson(Person person);
List<Person> GetAllPeople();
}
Run Code Online (Sandbox Code Playgroud)
基本实现:
public class PersonRepository: IPersonRepository
{
public void AddPerson(Person person)
{
ObjectContext.AddObject(person);
}
public List<Person> GetAllPeople()
{
return ObjectSet.AsQueryable().ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
你怎么能以有意义的方式对它进行单元测试?由于它跨越边界并从数据库进行物理更新和读取,这不是单元测试,而是集成测试.
或者首先想要对其进行单元测试是不对的?我应该只对存储库进行集成测试吗?
我一直在谷歌上搜索主题,博客经常说要创建一个实现IRepository的存根:
public class PersonRepositoryTestStub: IPersonRepository
{
private List<Person> people = new List<Person>();
public void AddPerson(Person person)
{
people.Add(person);
}
public List<Person> GetAllPeople()
{
return people;
}
}
Run Code Online (Sandbox Code Playgroud)
但是没有单元测试PersonRepository,它测试PersonRepositoryTestStub的实现(不是很有帮助).
是否可以自动显示/隐藏ajax加载gif,同时禁用/启用提交按钮?(当提交按钮是样式<a>而不是输入类型=提交时)
目前提交时我这样做:
$("#save_button_id").click(function () {
if ($('#save_button_id').hasClass('ui-state-disabled')) return false;
Save();
});
function Save() {
StartAjax($("#save_button_id"));
$.ajax({
success: function (data) {
EndAjax($("#save_button_id"));
// etc...
},
error: function (xhr, status, error) {
EndAjax($("#save_button_id"));
// etc ...
}
});
}
function StartAjax(button) {
DisableButtonClick(button);
$("#ajaxLoad").show();
}
function EndAjax(button) {
EnableButtonClick(button);
$("#ajaxLoad").hide();
}
Run Code Online (Sandbox Code Playgroud)
我已经看过一些地方谈论如何使用.ajaxStart()自动显示加载gif,但是是否也可以找到对点击的按钮(样式<a>标签)的引用,并自动禁用/启用它?
这一点就是不必每次都手动输入Start/EndAjax,并确保应用程序在任何地方都是一致的.
编辑
到目前为止,没有一个答案提供自动化 - 任何解决方案(如我上面的当前一个)你必须在每个$ .ajax()之前和之后手动键入开始/结束导致维护问题:很容易忘记放置开始/结束一些$ .ajax()调用旁边,如果你想稍后改变它的工作方式,你需要通过每一个来进行更改.
编辑2 - 澄清点.delegate()建议
你说"你可以将你的事件处理程序附加到任何元素" - 但我想将它附加到每个按钮元素(DRY!):所以我已经修改了你的建议的第一部分:
$('div#buttons a.ui-icon').each(function(index) {
$(this).ajaxStart(function() {
$("#ajaxLoad").show();
});
});
Run Code Online (Sandbox Code Playgroud)
这解决了第一个问题,即如何显示任何按钮的加载.gif,而不必重复键入"$("#ajaxLoad").show()"到处都有$ .ajax()调用.
下一部分是如何在单击时禁用任何按钮(同样没有重复代码) - 你建议使用.delegate().但在您的示例中,每次单击按钮都会调用Save()方法.(我更改了选择器以匹配我的html) …
在ASP.NET MVC 2中,使用Entity Framework 4,我收到此错误"实体对象不能被IEntityChangeTracker的多个实例引用".
对SO的搜索表明,可能是因为我有不同的Entity Framework ObjectContext实例,而它应该只是每个HttpContext的一个ObjectContext实例.
我有这个代码(在我加入之前写的很久)似乎就是这样 - 每个HttpContext都有一个ObjectContext.但我经常收到"IEntityChangeTracker"异常,因此它可能无法正常工作:
// in ObjectContextManager.cs
public const string ConnectionString = "name=MyAppEntities";
public const string ContainerName = "MyAppEntities";
public static ObjectContext GetObjectContext()
{
ObjectContext objectContext = GetCurrentObjectContext();
if (objectContext == null) // create and store the object context
{
objectContext = new ObjectContext(ConnectionString, ContainerName);
objectContext.ContextOptions.LazyLoadingEnabled = true;
StoreCurrentObjectContext(objectContext);
}
return objectContext;
}
private static void StoreCurrentObjectContext(ObjectContext objectContext)
{
if (HttpContext.Current.Items.Contains("EF.ObjectContext"))
HttpContext.Current.Items["EF.ObjectContext"] = objectContext;
else
HttpContext.Current.Items.Add("EF.ObjectContext", objectContext);
}
private static ObjectContext GetCurrentObjectContext()
{ …Run Code Online (Sandbox Code Playgroud) 如何找到此问题的来源,JavaScriptSerializer无法对JavaScriptSerializer序列化的日期值进行反序列化?
在调用应用程序中:
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(model);
// generates this json
{'Guid':'guid','OrderNumber':'1','OrderDate':'\/Date(1299456000000)\/',
'OrderStatus':'Completed','DiscountRate':0.0000}
Run Code Online (Sandbox Code Playgroud)
在接收申请中:
string json = @"{'Guid':'guid','OrderNumber':'1','OrderDate':'\/Date(1299456000000)\/',
'OrderStatus':'Completed','DiscountRate':0.0000}";
var serializer = new JavaScriptSerializer();
var model = serializer.Deserialize(json);
Run Code Online (Sandbox Code Playgroud)
引发String was not recognized as a valid DateTime异常.
如果JavaScriptSerializer序列化日期,为什么JavaScriptSerializer不能反序列化?
当clearInterval()没有停止时,你如何停止计时器?
此代码的目的是将数字从0向上设置为动画,直到它到达结尾(例如,动画从0 ... 75%).但是当我调用clearInterval()时,计时器不会停止:
<div id="foo"></div>
<div id="bar"></div>
animate("99%", $("#foo")); // doesnt stop
animate("75%", $("#bar")); // doesnt stop
function loop(clr, clr2, ele, rand, last, delay) {
clearInterval(clr);
clearInterval(clr2);
inloop(clr, clr2, ele, rand, last, delay);
if (rand >= last) {
clearInterval(clr);
clearInterval(clr2);
}
else {
clr2 = setTimeout(function () {
loop(clr, clr2, ele, rand, last, delay);
}, 2500);
}
}
function inloop(clr, clr2, ele, rand, last, delay) {
ele.html((rand += 1) + "%");
if (rand >= last) {
clearInterval(clr);
clearInterval(clr2); …Run Code Online (Sandbox Code Playgroud) 在Magento站点上调用Web服务时,使用C#ASP.NET MVC:
有时Magento网站会发送错误的HTTP标头,特别是身体大小的标题,表示身体比实际大.例如,它可能会说body-size = 1000,但是主体只包含999个字节.尽管标题不正确,但正文是正确的,所以我仍然希望能够处理它.
现在,当我尝试在C#中读取该响应时
var invoiceInfo = _magentoService.salesOrderInvoiceInfo(sessionId, invoiceId);
Run Code Online (Sandbox Code Playgroud)
它引发了一个异常:
Connection closed by remote host
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为它试图读取第1000个字节,但它不在那里.更糟糕的是,读取正文的代码深埋在.NET框架中,所以我无法改变它:
[System.Web.Services.Protocols.SoapRpcMethodAttribute("urn:Mage_Api_Model_Server_V2_HandlerAction", RequestNamespace="urn:Magento", ResponseNamespace="urn:Magento")]
[return: System.Xml.Serialization.SoapElementAttribute("result")]
public salesOrderInvoiceEntity salesOrderInvoiceInfo(string sessionId, string invoiceIncrementId) {
object[] results = this.Invoke("salesOrderInvoiceInfo", new object[] {
sessionId,
invoiceIncrementId}); // exception thrown here
return ((salesOrderInvoiceEntity)(results[0]));
}
Run Code Online (Sandbox Code Playgroud)
我无法更改Magento网站,或修复导致此问题的任何问题(它是第三方网络服务器).
有什么办法可以改变我自己的C#代码的行为吗?我希望能够以某种方式迫使它在到达身体末端时停止并且如果出现这种情况则忽略此异常
当我在MVC 4中使用下面的Bundling时,我的应用程序会出现几个JavaScript错误,例如'jQuery undefined'
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-{version}.js",
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
Run Code Online (Sandbox Code Playgroud)
但是,当我使用以下方法时,我的应用程序无需JavaScript错误:
bundles.Add(new ScriptBundle("~/bundles/jquery1").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jquery2").Include(
"~/Scripts/jquery-ui-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jquery3").Include(
"~/Scripts/jquery.unobtrusive*"));
bundles.Add(new ScriptBundle("~/bundles/jquery3").Include(
"~/Scripts/jquery.validate*"));
Run Code Online (Sandbox Code Playgroud)
我的问题:问题是什么?
asp.net asp.net-mvc asp.net-mvc-4 bundling-and-minification system.web.optimization
在尝试LightInject IoC容器http://www.lightinject.net/时,它会在解析ISomeService类型时抛出stackoverflow异常:
所有类型都在App_Start中注册:
container.RegisterAssembly("MyApp*.dll");
Run Code Online (Sandbox Code Playgroud)
然后,当我尝试在控制器中解决它时,它会失败并抛出stackoverflow异常:
public SomeController(ISomeService someService)
{
_someService = someService;
}
Run Code Online (Sandbox Code Playgroud)
使用ServiceLocator时也会出现相同的错误:
ServiceLocator.Current.GetInstance<ISomeService>();
Run Code Online (Sandbox Code Playgroud)
我已经跟踪了它,我可以看到它在LightInject ServiceContainer类中失败了,但是我不明白它为什么会失败.
public object GetInstance(Type serviceType)
{
return GetDefaultDelegate(serviceType, true)(constants.Items);
}
Run Code Online (Sandbox Code Playgroud)
在调用GetDefaultDelegate之后,执行路径再次在GetInstance中结束,导致无限循环和堆栈溢出.
编辑2 - 进一步跟踪它,它似乎是由SomeService同时具有构造函数和属性注入引起的:
public class SomeService : ISomeService
{
public IAnotherService AnotherService { get; set; }
public SomeService(IAnotherService anotherService)
{
AnotherService = anotherService;
}
}
Run Code Online (Sandbox Code Playgroud)
依赖IAnotherService AnotherService是通过构造函数和属性注入的,但是无意使用属性注入器.
编辑3
应用程序中有几个层都使用ServiceLocator,所以我最初使用NuGet将LI添加到自己的项目中,所以我有:
MyApp.IoC.LightInject
MyApp.Repositories
MyApp.Services
MyApp.Web.UI
Run Code Online (Sandbox Code Playgroud)
但实际上并不需要将其添加到自己的项目中,因为可以在UI层中设置服务定位器提供程序:
var serviceLocator = new LightInjectServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
Run Code Online (Sandbox Code Playgroud)
所以我刚刚删除了额外的项目,并将NuGet包放入UI层.我还安装了LightInject.Annotation并故意不调用container.EnableAnnotatedPropertyInjection()方法,以确保只使用构造函数注入.它仍在抛出stackoverflow.
要测试解析是否正常,我只是在HomeController.Index()方法中执行此操作:
public ActionResult Index()
{
var a = …Run Code Online (Sandbox Code Playgroud) c# ioc-container inversion-of-control service-locator light-inject
我有这段代码:
try
{
var files = from folder in paths
from file in Directory.EnumerateFiles(path, pattern, searchOption)
select new Foo() { folder = folder, fileName = file };
Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism }, currentFile =>
{
DoWork(currentFile);
});
}
catch (Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)
当我有异常时Directory.EnumerateFiles,我无法在这段代码中捕获此异常.调用此代码段的方法捕获到异常.
从Visual Studio,在调试模式下,Visual Studio捕获异常(例如a DirectoryNotFoundException).
c# ×7
asp.net-mvc ×2
javascript ×2
.net ×1
asp.net ×1
http-headers ×1
jqgrid ×1
jquery ×1
json ×1
light-inject ×1
linq ×1
magento ×1
performance ×1
settimeout ×1
unit-testing ×1
web-services ×1