小编Jos*_*her的帖子

为什么 .NET “de-CH”文化编号组分隔符在本地和 Azure 上不同?

在本地桌面和 Azure 中运行时,我看到一个不同的 Unicode 字符作为“de-CH”文化的数字组分隔符。

当以下代码在我的桌面上以 .NET Core 3.1 或 .NET Framework 4.7.2 运行时,它的输出2019看起来像一个撇号但不一样。

在 Azure 中运行时,例如在https://try.dot.net或(稍作修改)在 .NET Core 3.1(基于 Windows 的应用服务)上运行的 Azure 函数中0027,它会生成标准 ASCII 撇号。

using System;
using System.Linq;
using System.Globalization;

Console.WriteLine(((int)(CultureInfo
    .GetCultureInfo("de-CH")
    .NumberFormat
    .NumberGroupSeparator
    .Single())) // Just getting the single character as an int
    .ToString("X4") // unicode value of that character
    );
Run Code Online (Sandbox Code Playgroud)

这样做的结果是,尝试使用“de-CH”文化在本地桌面上解析字符串4'200.000(撇号是 Unicode 的0027地方)失败,但它在 Azure 中有效。

为什么会有差异?

.net c# localization azure number-formatting

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

单元测试的可重用设置

我正在使用 xUnit 和 Moq 来编写我的单元测试,并且在我的各种测试中我有很多重复的代码,我想将它们提取为某种可重用的方式。

重复代码

var note = new Note { Id = Guid.NewGuid() };

    var subVersion = new Mock<SubmissionVersion>();
    subVersion.Setup(x => x.Notes.Remove(note));

    var repo = new Mock<IRepository>();
    repo.Setup(x => x.GetById<Note>(note.Id)).Returns(note);
    repo.Setup(x => x.GetById<SubmissionVersion>(It.IsAny<Guid?>())).Returns(subVersion.Object);
Run Code Online (Sandbox Code Playgroud)

鉴于以下测试,我如何清理它们以免重复?

[Fact]
public void Should_CallRepoGetNoteByIdOnce()
{
    // Arrange
    var note = new Note { Id = Guid.NewGuid() };

    var subVersion = new Mock<SubmissionVersion>();
    subVersion.Setup(x => x.Notes.Remove(note));

    var repo = new Mock<IRepository>();
    repo.Setup(x => x.GetById<Note>(note.Id)).Returns(note);
    repo.Setup(x => x.GetById<SubmissionVersion>(It.IsAny<Guid?>())).Returns(subVersion.Object);

    // Act
    SubmissionVersion.DeleteNote(repo.Object, subVersion.Object, note.Id.Value);

    // Assert
    repo.Verify(x …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing xunit xunit.net

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

通过string.format生成的SQL查询字符串中的转义字符

我在c#中有声明:

String sql = String.Format("UPDATE Table SET FIRST_NAME='{0}',LAST_NAME='{1}',BIRTH_DATE='{2}' where CUSTOMER_NUMBER ='{3}'",FirstName, LastName,DateOfBirth,Number);
Run Code Online (Sandbox Code Playgroud)

如果名字,姓氏等有撇号像O'Hare,O'Callahagan,则上述语句不会执行,因为更新语句的语法错误.

如何在string.format中转义撇号?

c# sql string formatting

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

单元测试检索方法 - 冗余?

我的服务层中有以下方法

public ModuleResponse GetModules(ModuleRequest request)
{
    var response = new ModuleResponse(request.RequestId);
    try
    {
        response.Modules = Mapper.ToDataTransferObjects(ModuleDao.GetModules());
        return response;
    }
    catch (Exception ex)
    {
        Log.Error(ex);
        response.Acknowledge = AcknowledgeType.Failure;
        response.Message = "An error occured.";
        return response;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个用xUnit编写的单元测试,如下所示:

[Fact]
public void GetModulesTest()
{
    //Arrange            
    var mockModuleDao = Mock.Create<IModuleDao>();
    var mockLog = Mock.Create<ILog>();
    var mockAuditDao = Mock.Create<IAuditDao>();

    var moduleList = new List<ModuleItem>
    {
        new ModuleItem {Id = 100, Category = "User Accounts", Feature = "Users"},
        new ModuleItem {Id = 101, Category = …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing mocking xunit.net

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

在asp.net web表单中实现autofac

我正在开发 ASP.net Web Form,我想在网站中实现 AutoFac。

我已按照以下链接中的步骤操作: https ://autofaccn.readthedocs.io/en/latest/integration/webforms.html

但我收到了这个错误:

该模块要求 HttpApplication(全局应用程序类)实现 IContainerProviderAccessor。

Global.asax.cs我在我的项目中找不到,只有Global.asax.

Global.asax:

<%@ Application Language="C#" %>
<%@ Import Namespace="Autofac" %>
<%@ Import Namespace="Autofac.Integration.Web" %>
<script RunAt="server">
    public class Global : HttpApplication, IContainerProviderAccessor {

        // Provider that holds the application container.
        static IContainerProvider _containerProvider;

        // Instance property that will be used by Autofac HttpModules
        // to resolve and inject dependencies.
        public IContainerProvider ContainerProvider {
            get { return _containerProvider; }
        }

        void Application_Start(object sender, EventArgs e) …
Run Code Online (Sandbox Code Playgroud)

c# webforms autofac c#-4.0

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