小编Chi*_*han的帖子

如何在不使用Try/Catch的情况下检查字符串是否是JavaScript中的有效JSON字符串

就像是:

var jsonString = '{ "Id": 1, "Name": "Coke" }';

//should be true
IsJsonString(jsonString);

//should be false
IsJsonString("foo");
IsJsonString("<div>foo</div>")
Run Code Online (Sandbox Code Playgroud)

解决方案不应包含try/catch.我们中的一些人打开"中断所有错误",他们不喜欢调试器打破那些无效的JSON字符串.

javascript json

495
推荐指数
14
解决办法
51万
查看次数

EF Code First - 超时已过期.完成前经过了超时时间

为这个措辞奇怪的问题道歉.我不知道实际问题是什么,但希望有人可以给我一些见解.

尝试运行迁移时出现以下错误:

Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
Run Code Online (Sandbox Code Playgroud)

值得注意的是,在我的笔记本电脑上,这种情况并没有发生,但在我的虚拟机(天蓝色 - 大)上,这种情况发生率为100%.

我使用的是Ef 6.0.0 -rc1.请注意,更新EF不是一个选项.如果更新到EF 6.0.0或6.0.1,我将收到以下错误,100%失败率:

Code First添加迁移期间的错误

我也计时了错误.触发错误大约需要1.5分钟.当使用-Verboseflag 运行时,它试图创建200个带索引的表.复制sql查询并在SSMS中将其排除需要5秒.

我厌倦但没有用的一些事情:

1)ObjectContext.CommandTimeout = 36000 // 10 hours!如下所示设置:

/sf/answers/436421541/

2)在"web.config"中设置连接字符串中的超时:

connectionString="Data Source=localhost;Initial Catalog=myDB;Integrated Security=SSPI;Connection Timeout=36000"

3)设置"machine.config"事务maxTimeout:

<system.transactions> <machineSettings maxTimeout="00:00:00" /> </system.transactions>

4)在sql server上设置"远程查询超时"

USE MyDB;
GO
EXEC sp_configure 'remote query timeout', 0 ;
GO
RECONFIGURE ; …
Run Code Online (Sandbox Code Playgroud)

c# sql-server entity-framework ef-migrations

22
推荐指数
3
解决办法
3万
查看次数

Angular2 patchValue将值推入数组

它看起来像Angular2的FormGroup.patchValue()不会将新元素推送到数组中.

例如这样的事情:

ngOnInit() {

    this.form = this.formBuilder.group({
        animal: [''],
        school: this.formBuilder.group({
            name: [''],
        }),
        students: this.formBuilder.array([this.formBuilder.control('Bob')])
    });

    setTimeout(() => this.form.patchValue({
      animal: 'cat'
      school : {name: 'Fraser'},
      students: ['Bob gets edited', 'This will not show']
    }), 250);

}
Run Code Online (Sandbox Code Playgroud)

只会更新 "学生"中的第一个元素,但不会插入第二个元素.

我需要做什么才能让它显示两个元素?

这里的人物.

typescript angular2-forms angular

16
推荐指数
3
解决办法
2万
查看次数

CouchDB视图非常慢

我有一个大小为8.2 GB的CouchDB(v0.10.0)数据库,包含3890000个文档.

现在,我有以下作为视图的地图

function(doc) {emit([doc.Status], doc);
Run Code Online (Sandbox Code Playgroud)

它需要永远加载(4小时仍然没有结果).

以下是一些可能有助于描述情况的额外信息:

  1. 视图不是临时视图.视图是在插入3890000文档之前定义的.

  2. 服务器上没有任何东西.它是一个ubuntu盒子,只安装了默认值.

  3. 我看到我的CPU正在移动并努力工作(有时会达到100%).记忆也在移动,但没有增加.

所以我的问题是:

  1. 背景中实际发生了什么?
  2. 这是"一次性"的事情,我必须等待一次,以后会以某种方式运作吗?

非常感谢,

performance couchdb

15
推荐指数
2
解决办法
7512
查看次数

改善jQuery模板性能

更新

显然,可以编译jQuery模板,它有助于使用此处显示的if语句来执行模板的性能.

但是,如图所示这里,预编译的jQuery的模板不适合我的情况,因为我的模板不包含逻辑块做多.

对于那些建议使用另一个模板引擎的人来说,理想情况下我只想使用jQuery模板,因为团队中的每个人都只知道jQuery.还有是比较了几个模板引擎测试用例.


嗨,

就在今天,我被告知使用jQuery模板存在性能问题.

为了比较,我使用了jQuery模板和旧的字符串追加方法来向表中添加行.结果可以在这里看到.使用jQuery模板比字符串追加方法慢大约65%,哎哟!

我想知道可以做些什么来提高jQuery模板脚本的性能.

可以在提供的链接中查看完整脚本.但基本思路如下:

模板:

<script type="x-jquery-tmpl" id="tmplRow">
<tr>
    <td><input type="checkbox" value="${id}" /></td>
    <td>${firstName} ${lastName}</td>
    <td class="edit">
        <a>Edit</a>
        <input style="display:none;" type="hidden" value="Blah" />
        <input class="cancel" type="button" value="cancel" />
    </td>
    <td class="select">
        <a>Select</a>
        <select style="display:none;">
            <option>0</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
        </select>
        <input class="cancel" type="button" value="cancel" />
    </td>
    <td>More string</td>
    <td>More string</td>
    <td>More string</td>
    <td>More string</td>
    <td>More string</td>
    <td>More string</td>
</tr> …
Run Code Online (Sandbox Code Playgroud)

performance jquery jquery-templates

12
推荐指数
2
解决办法
6186
查看次数

如何模拟采用动态对象的方法调用

说我有以下内容:

public interface ISession 
{
   T Get<T>(dynamic filter); }
}
Run Code Online (Sandbox Code Playgroud)

我有以下代码要测试:

var user1 = session.Get<User>(new {Name = "test 1"});
var user2 = session.Get<User>(new {Name = "test 2"});
Run Code Online (Sandbox Code Playgroud)

我该怎么嘲笑这个电话?

使用Moq,我厌倦了这样做:

var sessionMock = new Mock<ISession>();
sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new User{Id = 1});
sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new User{Id = 2});
Run Code Online (Sandbox Code Playgroud)

那没用.返回的结果为null

我还尝试使用Rhino Mocks执行以下操作:

var session = MockRepository.GenerateStub<ISession>();
session.Stub(x => x.Get<User>(new {Name = "test 1"})).Return(new User{Id=1});
Run Code Online (Sandbox Code Playgroud)

也没有运气.再次空虚.

那我该怎么做呢?

谢谢,

c# rhino-mocks moq mocking

11
推荐指数
1
解决办法
9043
查看次数

使用WPF中的Backgroundworker为主应用程序弹出MessageBox

在WPF应用程序中,我使用BackgroundWorker定期检查服务器上的条件.虽然工作正常,但我希望弹出一个MessageBox,如果在检查过程中出现故障,请通知用户.

这就是我所拥有的:

public static void StartWorker()
{
    worker = new BackgroundWorker();

    worker.DoWork += DoSomeWork;
    worker.RunWorkerAsync();
}

private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
    while (!worker.CancellationPending)
    {
        Thread.Sleep(5000);

        var isOkay = CheckCondition();

        if(!isOkay)
           MessageBox.Show("I should block the main window");                
    }   
}
Run Code Online (Sandbox Code Playgroud)

但是这个MessageBox不会阻塞主窗口.我仍然可以点击我的WPF应用程序并使用MessageBox更改我喜欢的任何内容.

我该如何解决这个问题?谢谢,


编辑:

作为参考,这是我最终做的事情:

public static void StartWorker()
{
    worker = new BackgroundWorker();

    worker.DoWork += DoSomeWork;
    worker.ProgressChanged += ShowWarning;
    worker.RunWorkerAsync();
}

private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
    while (!worker.CancellationPending)
    {
        Thread.Sleep(5000);

        var isOkay = CheckCondition();

        if(!isOkay)
           worker.ReportProgress(1); …
Run Code Online (Sandbox Code Playgroud)

c# wpf multithreading messagebox backgroundworker

9
推荐指数
3
解决办法
2万
查看次数

如何在Asp.Net(而不是MVC)中使用Autofac注册HttpContextBase

这是一个运行.Net 3.5的Asp.net应用程序(不是MVC)

我这样做了:

 protected void Application_Start(object sender, EventArgs e)
 {

 ...

       builder.Register(c => new HttpContextWrapper(HttpContext.Current))
          .As<HttpContextBase>()
          .InstancePerHttpRequest();
 }
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

我得到的错误:

从请求实例的作用域中看不到具有匹配"httpRequest"的标记的作用域.这通常表示注册为每HTTP请求的组件正被SingleInstance()组件(或类似场景)重新请求.在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,从不从容器本身请求.

所以我发现了这个:https://stackoverflow.com/a/7821781/305469

而我这样做了:

       builder.Register(c => new HttpContextWrapper(HttpContext.Current))
          .As<HttpContextBase>()
          .InstancePerLifetimeScope();
Run Code Online (Sandbox Code Playgroud)

但是现在当我这样做时:

public class HttpService : IHttpService
{
    private readonly HttpContextBase context;

    public HttpService(HttpContextBase context)
    {
        this.context = context;
    }

    public void ResponseRedirect(string url)
    {
        //Throws null ref exception
        context.Response.Redirect(url);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了一个空参考例外.

奇怪的是,context.Response不是null,它是在我调用它时抛出的.Redirect().

我想知道是否使用.InstancePerLifetimeScope(); 是问题.

顺便说一下,我尝试使用Response.Redirect(),它完美无缺.

那可能是什么问题呢?

谢谢,

c# asp.net autofac

7
推荐指数
1
解决办法
3573
查看次数

Visual Studio引用未在多个项目环境中复制

我在VS 2010中有一个解决方案文件,它有多个项目.

现在,我有一个对这个dll的引用调用MySql.Data.Entity.dll.

例如,我在解决方案中设置了以下项目:

  • 域(对MySql.Data.Entity.dll的引用)
  • Domain.Test(对项目"Domain"的引用)

我希望Domain.Test复制来自Domain的所有引用,因此我将该dll上的Property设置为"Copy Local - True".它没有复制到Domain.Test项目.

我从以前遇到过这个问题.我做的是这样的:

namespace Domain.Helpers
   {
        /// <summary>
        /// To trick VS to deploy required assemblies
        /// </summary>
        class BuildTricker
        {
            public void AssemblyTricker()
            {
                new LinFu.DynamicProxy.CallAdapter(null);
                new NHibernate.ByteCode.LinFu.ProxyFactory();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

工作得很好.如果我"使用"该类,它将知道将其复制到"Domain.Test".

问题是我无法从这个MySql.Data.Entity.dll初始化/"使用".

注意:只是在Using语句中添加命名空间不会起作用,你必须"使用"dll中的一个类才能复制它.

我目前手动将此dll引用到需要它的所有项目中.

所以,我的问题是.我的配置有问题吗?或者这是一个VS限制?

谢谢,

reference visual-studio-2010 visual-studio

6
推荐指数
1
解决办法
942
查看次数

动态加载jQuery模板

对于jQuery模板:

http://api.jquery.com/category/plugins/templates/

我希望能够从服务器动态加载模板,而不是在页面上预定义它.

我在项目中看到的演示使用的是预定义模板.经过一些研究,我发现它是可能的.

我尝试这样做但它不起作用:

<script src="child.html" type="text/x-jquery-tmpl"></script>
Run Code Online (Sandbox Code Playgroud)

我试过这样做但它不起作用:

$(function () {
    $.get("child.html", function (data) {
        //Add template
        $.template("tmplChild", data);
    });

    //template binds before async call is done
    $.tmpl("tmplChild").appendTo("body");
});
Run Code Online (Sandbox Code Playgroud)

最后,我将其归结为以下黑客攻击:

so.html(这是主页):

<html>
<head>
<title></title>
</head>
<body>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>

<script type="text/javascript" src="so.js"></script>

<script type="text/javascript">

    $(function () {
        initTemplates(templateReady);
    });

    function templateReady() {
        $.tmpl("tmplChild").appendTo("body");
    }

</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

child.html(这是子模板)

<h1>Child Loaded</h1>
Run Code Online (Sandbox Code Playgroud)

so.js(这是我ajaxly加载js模板的hack)

function initTemplates(callback) {
    var templateUrl = "child.html";
    var templateName = "tmplChild";

    initTemplate(templateUrl, …
Run Code Online (Sandbox Code Playgroud)

javascript jquery jquery-templates

5
推荐指数
1
解决办法
5783
查看次数