小编Jer*_*ian的帖子

有什么区别设置在Visual Studio中嵌入Interop类型是真还是假?

在Visual Studio中,当向项目添加一个引用时,属性窗口有一个选项Embed Inteop Types,我们应该将其设置为TrueFalse?有什么不同?

由于我们有很多项目,其中一些项目已经设置参考False,其他项目设置为True,它完全搞砸了.并且bulid服务器也有相同的警告:

"为嵌入式互操作程序集创建引用"是什么意思?

因此,我们打算改变所有的Embed Inteop TypesFalse,我们会得到什么风险?

c# visual-studio

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

async/await单元测试代码覆盖率

如何为async/await方法编写单元测试,我正在使用Visual Studio 2013.

假设我们有一个异步方法:

public async Task DoSomethingAsync()
{
    ...
    await _service.DoInternalAsync();
    ...
}
Run Code Online (Sandbox Code Playgroud)

由于我使用的是最新版本的Visual Studio,因此它对异步方法单元测试有很好的支持:

[TestMethod]
public async Task DoSomthingAsyncTest()
{
    ...
    await _objectUnderTest.DoSomethingAsync();
    // how to verify the result??? here is what I did
    _service.Verify(_ => _.DoInternalAsync());
}
Run Code Online (Sandbox Code Playgroud)

基本上,我有两个问题:

  1. 如代码中所述,如何验证Task结果?我做得对吗?
  2. 如果我运行该测试,VS会说测试通过.但是当我检查代码覆盖率时,await _service.DoInternalAsync()似乎没有涵盖句子,从代码覆盖率结果来看,它提示MoveNext()句子中有6个未覆盖的块.它内有什么问题?

c# unit-testing asynchronous async-await visual-studio-2013

10
推荐指数
1
解决办法
4161
查看次数

删除ObservableCollection中的一个Item

我有一些方法,如:

public void RemoveItem(ObservableCollection<SomeClass> collection, SomeClass instance)
{
    if(collection.Contains(instance))
    {
        collection.Remove(instance);
    }
}
Run Code Online (Sandbox Code Playgroud)

首先,即使集合包含实例,if句子仍然返回false.

其次,我删除了if句子,只是让集合可以删除实例.并且在执行之后,集合仍保留其原始项目,其中仍包含实例.

是引用问题,但如何解决?我只是想从的ObservableCollection中删除一个项目,并保持其显着的功能(这困扰了我在这里).

.net c# wpf

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

如何在.NET客户端中通过SignalR发送大数据

我们有一个.NET客户端,它使用SignalR来调用Server方法,但参数看起来非常大,对于这种情况如何解决呢?

客户代码:

public async Task FooAsync()
{
    var hubConnection = new HubConnection(...);
    await hubConnection.Start();

    var hubProxy = hubConnection.CreateHubProcx("ValueHub");
    //the content is very long, about 11776065 bytes (11MB)
    var content = File.ReadAllText(...);
    hubProxy.Invoke("Send", content);
    ...
}
Run Code Online (Sandbox Code Playgroud)

服务器代码:

[HubName("ValueHub")]
public class ValueHub : Hub
{
    public void Send(string json)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

从异常堆栈和源代码中,我发现SignalR内部使用HttpClientFormUrlEncodedContent类型HttpContent,也许限制来自这里.

System.UriFormatException was unhandled
  HResult=-2146233033
  Message=Invalid URI: The Uri string is too long.
  Source=System
  StackTrace:
       at System.UriHelper.EscapeString(String input, Int32 start, Int32 end, Char[] dest, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net signalr signalr.client

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

ReferentialConstraint中的依赖属性以1对1的关系映射到存储生成的列错误

在EntityFramework代码的第一个模型中,存在1:1的关系:

public class Child1
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public Child2 Child2 { get; set; }
}

public class Child2
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [ForeignKey("Child1")]
    public int Id { get; set; }

    public string Name { get; set; }

    public Child1 Child1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将一些数据插入数据库时​​,它抛出异常:

{"A dependent property in a ReferentialConstraint is mapped to a 
  store-generated column. Column: 'Id'."}
Run Code Online (Sandbox Code Playgroud)

似乎我不能使用自动生成的Id Child2,如何保持此功能并同时成功建立关系?

entity-framework ef-code-first

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

具有瞬态故障处理的Azure Web角色阻止例外:完全限定后路径太长

我们将云瞬态故障处理块应用于云服务的Web角色,我们甚至没有编写一个单行代码.当我们尝试在本地调试我们的云服务时,会在网页上显示异常,如下所示:

完全合格后路径太长.确保完整路径少于260个字符,目录名少于248个字符.

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.IO.FileLoadException:完全限定后路径太长.确保完整路径少于260个字符,目录名少于248个字符.

程序集加载跟踪:以下信息有助于确定无法加载程序集"Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus"的原因.

=== Pre-bind state information ===
LOG: DisplayName = Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ServiceBus | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, …
Run Code Online (Sandbox Code Playgroud)

asp.net enterprise-library azure azure-web-roles azure-cloud-services

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

为 Azure 虚拟机运行启动任务的正确方法是什么

我们在 Azure 虚拟机中运行节点应用程序,同时我们也想在某个时间借助 Azure 自动化(或通过管理门户)重新启动 VM。

但是VM重启后如何重启node应用呢?

我们尝试了很多方法来实现这一点,包括添加任务计划程序、将命令添加到注册表项 ( LocalMachine\..\Run)、使用 VM 的自定义脚本扩展......

以上都失败了。我们想要的是在虚拟机重启后,节点应用程序可以自动启动。如果我们然后使用预定义的帐户远程访问 VM,则上述一些方法可以工作。然而,这不是风景,我们只想在一开始就远程一次,而不是每次重启。

那么,实现此目的的正确方法是在VM自动重启后启动进程或执行命令而无需手动登录?

powershell startupscript azure azure-virtual-machine azure-automation

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

如何编写包含=的systemd环境变量值

在systemd单位文件中,我有一个环境,内容为key=IamValue=abc,如您所见,值是IamValue=abc其中包含=

在这种情况下,如何编写单位文件?

我尝试了以下操作,但似乎无效:

[Unit]
Description=...

[Service]
WorkingDirectory=...
ExecStart=...
Restart=always
RestartSec=10                                          
SyslogIdentifier=...
User=root
Environment=key="IamValue=abc"
Run Code Online (Sandbox Code Playgroud)

linux systemd

4
推荐指数
1
解决办法
848
查看次数

如果不等待任务怎么办?

这是我的代码:

private static Stopwatch _stopwatch;

static void PrintException(Exception ex)
{
    Console.WriteLine(_stopwatch.Elapsed);
    Console.WriteLine(ex);
}

static void ThrowException1()
{
    throw new InvalidAsynchronousStateException();
}

static void ThrowException2()
{
    throw new NullReferenceException();
}

static async Task ExecuteTask1()
{
    await Task.Delay(1000);
    ThrowException1();
}

static async Task ExecuteTask2()
{
    await Task.Delay(2000);
    ThrowException2();
}

static async Task Execute()
{
    var t1 = ExecuteTask1();
    var t2 = ExecuteTask2();

    try
    {
        await t2;
    }
    catch (NullReferenceException ex)
    {
        // the NullReferenceException will be captured
        Console.WriteLine("==============");
        PrintException(ex);
    }
}

static …
Run Code Online (Sandbox Code Playgroud)

c# async-await

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

将流从Invoke-RestMethod写入PowerShell中的映像文件

我有一个Asp.Net Web API,可以将图像流返回给客户端.

public HttpResponseMessage GetImage(string imageId)
{
    ...
    var response = new HttpResponseMessage();
    response.Content = new StreamContent(fileStream);
    return response;
}
Run Code Online (Sandbox Code Playgroud)

使用fiddler,我可以看到图像已成功下载.我想要实现的是使用PowerShell将其保存到本地映像.

$result = Invoke-RestMethod ...
Run Code Online (Sandbox Code Playgroud)

我得到了$result,但不知道如何将其保存到本地磁盘,我尝试过这些方法:

# 1
$result | Out-File ".../test.png"

# 2: this exception said the $result is a string, cannot convert to Stream
$fs = New-Object IO.FileStream "...\test.png","OpenOrCreate","Write"
([System.IO.Stream]($result)).CopyTo($fs)
Run Code Online (Sandbox Code Playgroud)

使用PowerShell将图像保存到本地的正确方法是什么?

powershell stream

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

ASP.NET Core Options pattern with name split by single underscore

I am trying to load my app settings with ASP.NET Core Options pattern.

The appsettings.json contains:

{
  "TEst": "hello",
  "TEST_ABC": "2" 
}
Run Code Online (Sandbox Code Playgroud)

POCO class:

public class AppSetting
{
    public string Test { get; set; }

    public string TestAbc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Bind to configuration:

services.Configure<AppSetting>(Configuration);
Run Code Online (Sandbox Code Playgroud)

While access AppSetting instance in controller, I can only get config Test as hello. TestAbc is set to null.

It seems Options pattern couldn't convert this kind of naming configuration, is …

c# asp.net-core asp.net-core-configuration

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

如何通过JavaScript访问Azure存储队列

出于测试目的,我们希望直接使用JavaScript访问Azure存储队列,而不是准备新的Web服务.

这可能吗?我们应该怎么做才能实现这一点,因为我找不到Azure存储的JavaScript API的官方文档.

azure azure-storage azure-queues

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

Azure存储表查询实体是否确实存在数量限制?

MSDN,似乎查询服务返回的实体数量有限制:

对Table服务的查询一次最多可返回1,000个实体,并且最多可执行五秒.

但是当我写一个示例来展示这个问题时,我没有发现返回实体数量的任何限制,这是我的关键代码:

public class DataProvider
{
    public static string PartitionKey
    {
        get { return "PartitionKey"; }
    }

    public static IEnumerable<CustomerEntity> MoreThanThousandData()
    {
        var result = new List<CustomerEntity>();
        for (int i = 0; i < 1200; i++)
        {
            result.Add(new CustomerEntity(PartitionKey, Guid.NewGuid().ToString())
            {
                Name = Guid.NewGuid().ToString(),
                Age = new Random().Next(10, 70)
            });
        }

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

将1200个实体插入表中:

public class AfterOptimize
{
    public void InsertDataToTable()
    {
        var cloudData = DataProvider.MoreThanThousandData();
        Console.WriteLine("Plan to insert {0} entities to the table.", cloudData.Count()); …
Run Code Online (Sandbox Code Playgroud)

azure azure-storage azure-table-storage

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