小编awj*_*awj的帖子

使用HTML Agility Pack查询HTML的ID时发生异常

我正在使用HTML Agility包来解析Visual Studio中的ASPX文件。

我正在搜索具有指定ID属性的元素。

我使用的代码是:

var html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(docText);
if (html.DocumentNode != null)
{
     try
     {
          var tagsWithId = html.DocumentNode.SelectNodes(string.Format("//[@id='{0}']", selector.Id));
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此代码时,它将引发异常“表达式必须计算为节点集”。

谁能告诉我为什么这个“ 必须 ”评估为一个节点集?为什么它不能简单地不返回任何节点(下一行调用tagWithId.Count)?确定由SelectNodes方法返回的HtmlNodeCollection可以包含0个节点吗?

还是由于Xpath表达式格式错误导致的错误?[我正在测试的选择器ID确实以<div id =“ thisId”>存在于文件中。]

甚至有可能直接从Visual Studio加载ASPX文件(我正在构建外接程序),还是会包含XML错误,而我将不得不加载输出HTML流(,在开始时没有页面声明)文件)。

html xpath parsing html-agility-pack

3
推荐指数
1
解决办法
1865
查看次数

使用反射添加事件处理程序时抛出TargetException

我想在WebClient对象回调时调用BackgroundWorker线程.

我目标在此BackgroundWorker上运行的方法未修复,因此我需要以编程方式定位指定的方法.

要实现这一点:事件args对象的一个​​属性传递给WebClient详细信息应该获取哪个方法(e.UserState.ToString()).该方法按预期而获得.

我当时希望将此获取的方法添加为BackgroundWorker.DoWork事件的委托.

// this line gets the targeted delegate method from the method name
var method = GetType().GetMethod(e.UserState.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
    // get the DoWork delegate on the BackgroundWorker object
    var eventDoWork = _bw.GetType().GetEvent("DoWork", BindingFlags.Public | BindingFlags.Instance);
    var tDelegate = eventDoWork.EventHandlerType;
    var d = Delegate.CreateDelegate(tDelegate, this, method);

    // add the targeted method as a handler for the DoWork event
    var addHandler = eventDoWork.GetAddMethod(false);
    Object[] addHandlerArgs = { d };
    addHandler.Invoke(this, addHandlerArgs); …
Run Code Online (Sandbox Code Playgroud)

.net reflection delegates asynchronous backgroundworker

3
推荐指数
1
解决办法
591
查看次数

JavaScript:为什么我不能用.push()链接Array.prototype.filter?

如果Array.prototype.filter返回一个数组,为什么我不能push()立即调用这个返回值?

例:

var arr = ["a", "ab", "c", "ad"];
var arr2 = arr.filter(function(elmnt) { return elmnt.indexOf("a") > -1; });
// result: ["a", "ab", "ad"]

arr2.push("aaa");
// result: ["a", "ab", "ad", "aaa"]
Run Code Online (Sandbox Code Playgroud)

好到目前为止.

但是那个push()电话号码的链接filter()呢?

var arr = ["a", "ab", "c", "ad"];
var arr2 = arr.filter(function(elmnt) { return elmnt.indexOf("a") > -1; }).push("aaa");
// result: 4
Run Code Online (Sandbox Code Playgroud)

为什么链接filter()push()导致我期望的元素数量,而不是这些元素的数组?

javascript arrays filtering method-chaining chaining

3
推荐指数
1
解决办法
2915
查看次数

为什么实体正在急切加载?

我有一个包含多个图层的实体层次结构,其中一个图层包含的对象数量可以达到数十万个.有时我只想要顶级对象,但我发现实体框架正在加载层次结构中的所有内容.

我甚至尝试过明确的延迟加载,但无济于事.

using (var db = new MyEntities())
{
    db.Configuration.ProxyCreationEnabled = true;
    db.Configuration.LazyLoadingEnabled = true;

    var daoDict = (from d in db.stt_dictionary
                   where d.id == dictionaryID && !d.deleted
                   select d).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

在调试时,如果我单步执行此操作然后将鼠标悬停在上面,daoDict我会发现其集合属性(virtual包含数千个对象).

为什么?

c# linq entity-framework lazy-loading dbcontext

3
推荐指数
1
解决办法
43
查看次数

Web API帮助页面未加载文档XML

我试图将ASP.NET帮助页面设置为在现有MVC项目中运行,尽管它指向同一解决方案中来自Web API项目的文档文件。Convention在文档中记录了在Web API内运行的帮助页面,但在这种情况下,我希望在同级MVC项目中使用它。

Web API项目将其文档XML文件输出到MVC项目内的文件夹中。

我已经在MVC项目中安装了Microsoft.AspNet.WebApi.HelpPage NuGet程序包。这将创建一个类\Areas\HelpPage\App_Start\HelpPageConfig.cs,并且在该类的Register方法中,我已XmlDocumentationProvider将该路径传递到Web API的文档文件。

但是,当我加载页面时,除了标题和占位符描述之外,页面都是空的。

在调试该HelpController.Index方法后,我可以在返回的内容IApiExplorer中看到_apiDescriptions均为空。

在MVC项目中运行的帮助页面的屏幕截图

但是,如果我将“帮助页面”直接安装到Web API项目中并调试相同的方法,则可以看到_apiDescriptions它们已经存在。

Web API项目中运行的帮助页面的屏幕快照

谁能解释Web API项目在做什么或已经配置了哪个MVC项目在做什么或没有配置?

asp.net-mvc asp.net-web-api asp.net-web-api-helppages asp.net-web-api2

3
推荐指数
1
解决办法
2843
查看次数

Powershell - 如何将<sectionGroup>添加到web.config

我正在尝试使用Powershell在web.config中sectionGroupconfiguration/configSections元素添加元素.

我现在有

$filePath = [path to my web.config file]

# load the XML from the web.config
$xml = New-Object XML
$xml = [xml](Get-Content $filePath)

# navigate to the <configSections> element
$xmlConfigSections = $xml.SelectSingleNode("//configuration/configSections")

# create the new <sectionGroup> element with a 'name' attribute
$sectionGroup = $xml.CreateElement("sectionGroup")
$xmlAttr = $xml.CreateAttribute("name")
$xmlAttr.Value = "myCustomSectionGroup"
$sectionGroup.Attributes.Append($xmlAttr)

# now add the new <sectionGroup> element to the <configSections> element
$xmlConfigSections.AppendChild($sectionGroup)

#save the web.config
$xml.Save($filePath)
Run Code Online (Sandbox Code Playgroud)

但这会导致该CreateElement方法出现异常:

"指定的节点不能作为此节点的有效子节点插入,因为指定的节点类型错误."

我不明白为什么当我尝试创建元素时抛出这样的异常(异常似乎与附加元素有关). …

powershell web-config configsection

2
推荐指数
1
解决办法
3818
查看次数

T4MVC - 如何重新生成?

我已经通过NuGet 安装了T4MVC.

在安装时,它为项目当时生成了项目的帮助程序类.但是,只要将新项添加到项目中,这些生成的帮助程序类就不会更新.

有几个人遇到了这个问题,答案似乎总是"右键单击T4MVC.tt文件并选择运行自定义工具 "(参见此处的示例).但是,当我右键单击T4MVC.tt文件时,没有运行自定义工具选项 - 实际上,在文件的属性中没有声明自定义工具.

此外,T4MVC.tt是项目中的隐藏文件,因此我希望在此文件上执行此操作似乎很奇怪.

那么有人可以解释如何让T4MVC更新MVC4项目中生成的类吗?

t4mvc asp.net-mvc-4

2
推荐指数
1
解决办法
4201
查看次数

在异步方法上调用.Wait()和Task.Run()之间的区别.Wait()

我在ASP.NET WebForms网站上有一个服务器端点击事件.在这种情况下,我调用一个方法,该方法又调用其异步伙伴方法,添加.Wait()调用.

然后,此方法向下几级(,调用另一个异步方法,调用另一个异步方法,依此类推),最终在HttpClient对象上调用异步方法.在这一点上,线程似乎从兔子洞里消失了; 该方法永远不会回电.

现在,我知道异步系列调用按预期工作,因为同样的代码也是从Web API控制器调用的(控制器方法调用第一个方法的异步版本,而不是同步'伙伴'方法),这是完全的异步,它按预期返回.

所以基本上我有这样的东西,永远不会回来

protected void btn_Click(object sender, EventArgs e)
    > Class1.DoSomething()
        > Class1.DoSomethingAsync.Wait()
            ...
                > await ClassN.Authenticate()
                  {
                    await myHttpClient.PostAsync()  // never returns
                  }
Run Code Online (Sandbox Code Playgroud)

我尝试使用.ConfigureAwait(false)第一个异步方法但没有任何成功.

我也有这个,它确实回来了

Task<IHttpActionResult> MyWebApiMethod()
    > await Class1.DoSomethingAsync()
        ...
            > await ClassN.Authenticate()
              {
                await myHttpClient.PostAsync()  // does return
              }
Run Code Online (Sandbox Code Playgroud)

我发现如果将其更改为以下内容,我可以使第一个版本正常工作:

protected void btn_Click(object sender, EventArgs e)
    > Class1.DoSomething()
        > Task.Run(async () => await Class1.DoSomethingAsync()).Wait()
            ...
                > await ClassN.Authenticate()
                  {
                    await myHttpClient.PostAsync()
                  }
Run Code Online (Sandbox Code Playgroud)

但我不知道为什么.

任何人都可以解释呼叫之间的区别

Class1.DoSomethingAsync.Wait() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asynchronous async-await

2
推荐指数
1
解决办法
4182
查看次数

无法启动WebJob - "不支持Azure存储模拟器"

在开发Azure WebJob的过程中,我遇到了异常

无法验证Microsoft Azure WebJobs SDK存储帐户.不支持Microsoft Azure存储模拟器,请使用Microsoft Azure中托管的Microsoft Azure存储帐户.

这似乎相当普遍,解决方案通常是将连接字符串更新为特定值.但是,在我的情况下,我已经有了这个特定的连接字符串.

此外,我在同一个解决方案中有两个WebJobs:我在两种情况下都使用完全相同的连接字符串,而另一个WebJob连接没有任何问题.

App.config我有

<connectionStrings>
    <add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName= ... ;AccountKey= ... " />
    <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

appSettings下显示相同的连接字符串:

<appSettings>
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;" />
    <!--<add key="StorageConnectionString" value="UseDevelopmentStorage=true" />-->
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我发现进入UseDevelopmentStorage=true没有任何区别.

我也试过在以下方面手动声明这些static void Main:

var config = new JobHostConfiguration {
                                          JobActivator = new WebJobActivator(kernel)
                                      };
config.DashboardConnectionString = "DefaultEndpointsProtocol=https;AccountName= ... ;AccountKey= ... ";
config.StorageConnectionString = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
Run Code Online (Sandbox Code Playgroud)

当我这样做时,会抛出异常

config.StorageConnectionString = ...
Run Code Online (Sandbox Code Playgroud)

我正在运行Azure存储模拟器的v5.2 - 它正在运行.就像我说的那样,其他WebJob能够连接并运行没有任何问题,并且它们都具有相同的App.config文件. …

c# azure azure-storage azure-queues

2
推荐指数
1
解决办法
1434
查看次数

Azure 存储事件触发器 - 捕获正在删除的 blob

我正在研究可用于 Azure 存储的 Webhooks/事件触发器。不幸的是,文档似乎专注于展示如何让 Azure 门户为我构建该功能,这不允许本地测试。

特别是,我正在研究在删除 blob 时进行捕获。

我的使用示例(Azure 函数):

[FunctionName("BlobDelete")]
public static async Task Run([BlobTrigger("...")]
                             CloudBlockBlob blob,
                             string name,
                             TraceWriter log)
{
    ;
}
Run Code Online (Sandbox Code Playgroud)

当我从存储容器中删除 blob 时出现问题:该函数未触发。

但是,我发现如果我点击CTRL+C控制台就会触发该功能。

谁能解释为什么?难道是我的用法不对?

另外,我无法找到触发器的任何文档BlobDelete,我只能找到BlobInput,BlobOutputBlobCopy。我猜测了一下BlobDelete,结果……成功了一半。

c# azure azure-storage azure-blob-storage azure-functions

2
推荐指数
1
解决办法
2411
查看次数