小编Muh*_*eed的帖子

可移植类库,相当于Dispatcher.Invoke或Dispatcher.RunAsync

在.NET,Windows 8和Windows Phone 7中,我的代码与此类似:

public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
{
    if (dispatcher.CheckAccess())
    {
        action();
    }
    else
    {
        dispatcher.Invoke(action);
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何在便携式类库中做一些事情?有一个平台无关的实现这将是很好的.我的想法是使用WP7中没有的TPL,但肯定会很快.

// PortableDispatcher must be created on the UI thread and then made accessible 
// maybe as a property in my ViewModel base class.
public class PortableDispatcher
{
    private TaskScheduler taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

    public void Invoke(Action action)
    {
        if (Alread on UI thread. How would I do this.)
        {
            action();
        }

        Task.Factory.StartNew(
            action, 
            CancellationToken.None,
            TaskCreationOptions.None,
            taskScheduler);
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# dispatcher task-parallel-library portable-class-library

10
推荐指数
2
解决办法
4000
查看次数

我应该在Reactive Extensions(Rx)Subject <T>上调用Dispose

我使用Reactive Extensions(Rx)Subject作为C#事件的直接替代品,如下所示:

public class MyClass
{
    private Subject<string> subject;

    public IObservable<string> WhenSomethingHappened
    {
        get { return this.subject.AsObservable(); }
    }

    private void OnSomethingHappened(string something)
    {
        this.subject.OnNext(something);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我从未在我的主题上调用OnCompleted.MyClass应该实现IDisposable并调用this.subject.Dispose吗?这意味着使用Subject的任何实现都应该实现IDisposable.

我问的原因是IDisposable模式有点像疾病,如果有一件事实现它,使用它的一切都必须实现它.

.net c# system.reactive

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

ISO 8601持续时间,以毫秒为单位?

如何使用ISO 8601表达500毫秒的持续时间?对我来说,文档并非100%明确.它只是"PT0.5S"吗?

time duration iso8601

10
推荐指数
2
解决办法
4366
查看次数

从VS 2017 .NET核心项目中的发布目录中排除文件

.gitignorewwwroot项目的文件夹中有一个文件,我试图将其排除在发布之外.以下代码似乎不起作用:

<ItemGroup>
  <Content Include="wwwroot\.gitignore">
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

当我使用该dotnet publish命令发布项目时,.gitignore仍然可以在输出目录中找到该文件.

.net .net-core asp.net-core dotnet-cli visual-studio-2017

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

Health Checks应该调用其他App Health Checks

我有两个我控制的API A和B,都有准备和生活健康检查.A依赖于B.

A
/foo - This endpoint makes a call to /bar in B
/status/live
/status/ready

B
/bar
/status/live
/status/ready
Run Code Online (Sandbox Code Playgroud)

A的准备情况运行状况检查是否应该因为依赖性而调用API B的就绪状况检查?

url api-design health-monitoring kubernetes kubernetes-health-check

10
推荐指数
2
解决办法
501
查看次数

使用 Moq 进行 ASP.NET Core 集成测试和模拟

我有以下使用自定义的ASP.NET Core 集成测试WebApplicationFactory

public class CustomWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint>
    where TEntryPoint : class
{
    public CustomWebApplicationFactory()
    {
        this.ClientOptions.AllowAutoRedirect = false;
        this.ClientOptions.BaseAddress = new Uri("https://localhost");
    }

    public ApplicationOptions ApplicationOptions { get; private set; }

    public Mock<IClockService> ClockServiceMock { get; private set; }

    public void VerifyAllMocks() => Mock.VerifyAll(this.ClockServiceMock);

    protected override TestServer CreateServer(IWebHostBuilder builder)
    {
        this.ClockServiceMock = new Mock<IClockService>(MockBehavior.Strict);

        builder
            .UseEnvironment("Testing")
            .ConfigureTestServices(
                services =>
                {
                    services.AddSingleton(this.ClockServiceMock.Object);
                });

        var testServer = base.CreateServer(builder);

        using (var serviceScope = testServer.Host.Services.CreateScope())
        {
            var serviceProvider = serviceScope.ServiceProvider;
            this.ApplicationOptions …
Run Code Online (Sandbox Code Playgroud)

moq xunit asp.net-core-mvc asp.net-core asp.net-core-testhost

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

使用CompositionScopeDefinition在MEF中定义范围

在我的应用程序的根目录中,我有一个AggregateCatalog和一个CompositionContainer,如下所示:

AggregateCatalog aggregateCatalog = new AggregateCatalog();
CompositionContainer compositionContainer = new CompositionContainer(aggregateCatalog);
Run Code Online (Sandbox Code Playgroud)

我的应用程序加载了包含多个导出的模块,如下图所示.我想使用CompositionScopeDefinition来确定图中圈出的导出范围.

模块导出图

请参阅此处了解类定义.

// Create CompositionScopeDefinition.
TypeCatalog globalParts = new TypeCatalog(typeof(RequestListener));
TypeCatalog scopedParts = new TypeCatalog(typeof(RequestHandler), typeof(DataAccessLayer), typeof(Logger), typeof(DatabaseConnection));
CompositionScopeDefinition compositionScopeDefinition = new CompositionScopeDefinition(
    globalParts,
    new[] { new CompositionScopeDefinition(scopedParts, null) });

// Register CompositionScopeDefinition.
aggregateCatalog.Catalogs.Add(compositionScopeDefinition);

// Create an instance of RequestListener.
RequestListener requestListener = compositionContainer.GetExportedValue<RequestListener>();
Run Code Online (Sandbox Code Playgroud)

但是,这会导致以下异常:

发生System.ComponentModel.Composition.ImportCardinalityMismatchException消息=未找到与约束匹配的导出:ContractName MyNamespace.RequestListener RequiredTypeIdentity MyNamespace.RequestListener InnerException:

如何使用CompositionScopeDefinition将我的作用域导出添加到现有的AggregateCatalog并使用我现有的CompositionContainer初始化它们?

更新

似乎使用AggregateCatalog的问题.如果我将CompositionScopeDefinition直接添加到CompositionContainer,一切正常,但这阻止我将其他目录添加到CompositionContainer.

.net c# prism mef .net-4.5

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

将图像或缩略图添加到Atom 1.0条目

StackOverflow答案表明您应该使用HTML条目内容并使用标准<img>标记链接到您的图像.

<content type="html">
  <![CDATA[
    <a href="http://test.lvh.me:3000/listings/341-test-pics?locale=en">
      <img alt="test_pic" src="http://test.lvh.me:3000/system/images/20/medium/test_pic.jpg?1343246102" />
    </a>
  ]]>
</content>
Run Code Online (Sandbox Code Playgroud)

我还在这里找到了一个名为Yahoo media extensions的东西,它允许你添加自定义的附加元素.

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<!-- ommitted -->
  <entry>
    <!-- ommitted -->
    <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="path_to_image.jpg" />
  </entry>
</feed>
Run Code Online (Sandbox Code Playgroud)

谷歌似乎也有自己类似的扩展.看到这里.

<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
<!-- ommitted -->
  <entry>
    <!-- ommitted -->
    <g:image_link>http://www.google.com/images/google_sm.gif</g:image_link>
  </entry>
</feed>
Run Code Online (Sandbox Code Playgroud)

我自己的直觉告诉我,我应该只需添加链接到图像,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <!-- ommitted -->
  <entry>
    <!-- ommitted -->
    <link rel="enclosure" type="image/png" length="1337"
        href="http://example.org/image.png"/>
  </entry>
</feed>
Run Code Online (Sandbox Code Playgroud)

最大兼容性的正确方法是什么?

image feed google-base yahoo-api atom-feed

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

在ASP.NET MVC 6中更新NuGet包

我刚刚创建了一个新的ASP.NET MVC 6项目,并希望将NuGet包更新到最新版本(默认项目模板通常非常过时).我注意到以下更新是可能的:

  • Antlr.3.4.1.9004> Antlr.3.5.0.2
  • Newtonsoft.Json.6.0.4> Newtonsoft.Json.6.0.8
  • WebGrease.1.5.2> WebGrease.1.6.0

当我尝试使用NuGet包对话框升级这些软件包或更新newhostoft.json的project.json文件中的版本号时,我收到错误.

1)严重性代码描述项目文件行错误未实现(HRESULT异常:0x80004001(E_NOTIMPL))0

2)严重性代码描述项目文件行错误NuGet包恢复一个或多个包失败.在"输出"窗口中查看详细信息 Mvc6ProjectName C:[路径到我的文件夹]\Source\Mvc6ProjectName\project.json 0

3)严重级代码描述项目文件行错误依赖项Newtonsoft.Json> = 6.0.8无法解析Mvc6Boilerplate C:\ Git\ASP.NET-MVC-Boilerplate\Source\MVC6\Mvc6Boilerplate\project.json 19

我也有一个MVC项目,我在那里使用最新的NuGet包,为什么我不能升级这些包呢?这是否与没有更多web.config文件的事实有关,我们可以在其中指定运行时assemblyBinding:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
      <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)

更新1

当我尝试从包管理器控制台更新Newtonsoft.Json时,我收到以下错误:

PM> Update-Package Newtonsoft.Json -ProjectName Mvc6Boilerplate -Version 6.0.8包含Id'Newtoftoft.Json'的包未安装到项目'Mvc6Boilerplate'.

似乎Newtonsoft.Json没有安装在默认的MVC 6项目中,而是作为另一个项目的依赖项安装.此NuGet包实际上并未显示在Nuget包管理器中,但显示在project.json文件中,也显示在References-> DNX和DNX Core树视图项下.

当我尝试显式安装Newtonsoft.Json包时,它确实升级到6.0.8但我得到了上面列出的错误.

c# asp.net asp.net-mvc nuget asp.net-core-mvc

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

使用ROW_NUMBER和PARTITION BY获取第一行和最后一行

样本输入

Name | Value | Timestamp
-----|-------|-----------------
One  | 1     | 2016-01-01 02:00
Two  | 3     | 2016-01-01 03:00
One  | 2     | 2016-01-02 02:00
Two  | 4     | 2016-01-03 04:00
Run Code Online (Sandbox Code Playgroud)

期望的输出

Name | Value | EarliestTimestamp | LatestTimestamp
-----|-------|-------------------|-----------------
One  | 2     | 2016-01-01 02:00  | 2016-01-02 02:00
Two  | 4     | 2016-01-01 03:00  | 2016-01-03 04:00
Run Code Online (Sandbox Code Playgroud)

尝试查询

我想使用ROW_NUMBER()PARTITION BY获得最新的Name,Value但我也想要最早和最新的Timestamp价值:

SELECT
    t.Name,
    t.Value,
    t.????????? AS EarliestTimestamp,
    t.Timestamp AS LatestTimestamp …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server row-number window-functions

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