小编Mar*_*rco的帖子

如何装饰JSON.NET StringEnumConverter

我正在使用一个api,它返回这样的字符串值. some-enum-value

我尝试将这些值放在枚举中,因为默认的StringEnumConverter不能完成这项工作,我尝试用一​​些额外的逻辑来装饰这个转换器.如何确保正确反序列化值?

以下代码是我尝试完成这项工作.然而,reader = new JsonTextReader(new StringReader(cleaned));由于base.ReadJson无法将字符串识别为JSON,因此该行打破了整个事情.

有没有更好的方法来实现这一点,而无需在StringEnumConverter中实现所有的强制逻辑?如何解决我的方法?

public class BkStringEnumConverter : StringEnumConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.String)
        {
            var enumString = reader.Value.ToString();
            if (enumString.Contains("-"))
            {
                var cleaned = enumString.Split('-').Select(FirstToUpper).Aggregate((a, b) => a + b);
                reader = new JsonTextReader(new StringReader(cleaned));
            }
        }
        return base.ReadJson(reader, objectType, existingValue, serializer);
    }

    private static string FirstToUpper(string input)
    {
        var firstLetter = input.ToCharArray().First().ToString().ToUpper();
        return string.IsNullOrEmpty(input)
            ? input
            : firstLetter + …
Run Code Online (Sandbox Code Playgroud)

c# enums json json.net

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

Windsor为多个接口注册单例组件

我想在Castle.Windsor中注册一个带有2个接口的类.

这段代码是否有效...我是否只有两个接口的实例...

Component.For<IEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Component.For<IOutlookEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Run Code Online (Sandbox Code Playgroud)

我需要仔细检查一下,因为我的环境应该始终是同一个实例...

因此,当我使用IEnvironment接口解析时,我应该获得与使用IOutlookEnvironment解析组件时相同的实例

c# castle-windsor

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

在异步任务中使用HttpContext

我有以下mvc动作.

public async Task<JsonResult> DoSomeLongRunningOperation()
{
    return await Task.Run(() =>
    {
        //Do a lot of long running stuff
        //The underlying framework uses the HttpContext.Current.User.Identity.Name so the user is passed on the messagebus.
    }
}
Run Code Online (Sandbox Code Playgroud)

在任务中,HttpContext变为null.我们做了很多尝试,但没有任何事情让我们确信HttpContext总是在我们的新线程中可用.

有没有在异步任务中使用HttpContext的解决方案?

在我们的Io​​cContainer中,我们注册了以下对象,该对象将用户名传递给框架.

public class HttpContextUserIdentityName : ICredentials
{
    public string Name
    {
        get { return HttpContext.Current.User.Identity.Name; }
    }
}
Run Code Online (Sandbox Code Playgroud)

在持久化到数据库之前,在很多地方调用此代码.

我们需要另一种方法来获取用户的用户名启动webrequest或修复HttpContext为null的问题.

因为持久化到数据库发生在任务中,所以在进入任务之前无法访问HttpContext.

我也想不出一个临时持久保存用户名的安全方法,所以我可以实现另一个ICredentials服务对象.

c# asp.net-mvc asynchronous async-await asp.net-mvc-3

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

MVC 3区域路由不起作用

我在我的MVC 3应用程序中创建了一个名为"Blog"的区域.

在global.asax中,我有以下代码.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

    }
Run Code Online (Sandbox Code Playgroud)

这是我所在地区的代码

public class BlogAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Blog"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Blog_default",
            "Blog/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc c#-4.0 asp.net-mvc-3

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

有条件地在 GitHub 操作工作流程中追加字符串

是否可以使用 GitHub Actions 表达式有条件地连接字符串?

例如,就像我在下面的示例中尝试做的那样。

      - name: Release
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist  ${{ if (startsWith(github.ref, 'refs/tags/')) { '--snapshot' }}
Run Code Online (Sandbox Code Playgroud)

github github-actions

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

node child_process.spawn不使用windows上路径中的空格

如何提供路径 child_process.spawn

例如路径:

c:\users\marco\my documents\project\someexecutable

路径由最终用户从配置文件提供.

var child_process = require('child_process');
var path = require('path');
var pathToExecute = path.join(options.toolsPath, 'mspec.exe');
child_process.spawn(pathToExecute, options.args);
Run Code Online (Sandbox Code Playgroud)

目前只有空间使用后的部分 child_process.spawn

我还尝试在路径中添加引号,如下所示:

var child_process = require('child_process');
var path = require('path');
var pathToExecute = path.join(options.toolsPath, 'mspec.exe');
child_process.spawn('"' + pathToExecute + '"', options.args);
Run Code Online (Sandbox Code Playgroud)

但是这会导致ENOENT错误.

node.js

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

如何在Git中删除重复的文件跟踪

我更改了文件夹的名称后,我的一个文件被跟踪了两次.

Git现在跟踪以下两个文件,这两个文件实际上是同一个文件.

SRC /网站/脚本/ common.js

SRC /网站/脚本/ common.js

如何删除第二个被跟踪的?此文件夹不再存在,因为我将其重命名Scriptsscripts.

git github

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

golang autocert acme/autocert:缺少服务器名称

我在我的应用程序中使用 Library autocert 来生成 ssl 证书,问题是 30% 的用户对我的应用程序有问题,我当前的代码是:

fmt.Println("Starting server on " + this.Params.Bind)
if this.Params.SSL {
    fmt.Println("SSL Enabled")
    m := autocert.Manager{
        Prompt:     autocert.AcceptTOS,
        HostPolicy: autocert.HostWhitelist(this.Params.HostsWhitelist...),
        Cache:      autocert.DirCache(this.Params.CertCache),
    }

    log.Fatal(autotls.RunWithManager(r, &m))
} else {
    r.Run(this.Params.Bind)
}
Run Code Online (Sandbox Code Playgroud)

错误是:

2018/12/03 12:37:33 http: TLS handshake error from 68.71.48.249:55885: acme/autocert: missing server name
2018/12/03 12:37:33 http: TLS handshake error from 209.213.121.223:38284: acme/autocert: missing server name
2018/12/03 12:37:33 http: TLS handshake error from 209.213.121.223:38283: acme/autocert: missing server name
2018/12/03 12:37:33 http: TLS handshake error from …
Run Code Online (Sandbox Code Playgroud)

ssl go tls1.2 lets-encrypt

8
推荐指数
0
解决办法
2551
查看次数

FakeItEasy代理方法调用实际实现

我正在尝试将对虚假对象的调用代理到实际实现.这样做的原因是我希望能够使用Machine.Specifications的WasToldTo和WhenToldTo,它仅适用于接口类型的伪造.

因此,我正在执行以下操作来代理对我的真实对象的所有调用.

public static TFake Proxy<TFake, TInstance>(TFake fake, TInstance instance) where TInstance : TFake
{
    fake.Configure().AnyCall().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
    return fake;
}
Run Code Online (Sandbox Code Playgroud)

我会像这样使用它.

var fake = Proxy<ISomeInterface, SomeImplementation>(A.Fake<ISomeInterface>(), new SomeImplementation());

//in my assertions using Machine.Specifications (reason I need a fake of an interface)
fake.WasToldTo(x => x.DoOperation());
Run Code Online (Sandbox Code Playgroud)

然而问题是这只适用于void方法,因为Invokes方法没有对返回值做任何事情.(Action param代替Func)

然后我尝试使用WithReturnValue方法执行此操作.

public static TFake Proxy(TFake fake, TInstance instance) where TInstance : TFake
{
    fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
    fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
    fake.Configure().AnyCall()..WithReturnType().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));
    //etc.
    return fake;
}
Run Code Online (Sandbox Code Playgroud)

然而,Invokes方法仍然不能按我想要的方式工作(仍然是Action而不是Func).所以仍然没有使用返回值.

有没有办法用当前的最新版本实现这一目标?

我已经在FakeItEasy …

c# unit-testing mocking mspec fakeiteasy

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

在复合 GitHub 操作中向 docker 容器提供参数

我正在尝试在复合 GitHub Action 中传递一些动态创建的参数。

\n

然而,文档缺少有关如何在这种情况下将参数传递给 docker 容器的示例。

\n

https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsuses

\n

在这里看看我想要实现的目标。

\n
runs:\n  using: \'composite\'\n  steps:\n    - name: compose arguments\n      id: compose-args\n      shell: bash\n      run: |\n        encoded_github="$(echo \'${{ inputs.github_context }}\' | base64)"\n        encoded_runner="$(echo \'${{ inputs.runner_context }}\' | base64)"\n\n        args=(\'${{ inputs.command }}\')\n        args+=(\'${{ inputs.subcommand }}\')\n        args+=(\'--github-context\')\n        args+=("${encoded_github}")\n        args+=(\'--runner-context\')\n        args+=("${encoded_runner}")\n        args+=(\'${{ inputs.arguments }}\')\n\n        echo "::set-output name=provenance_args::$(echo "[$(printf "\\"%s\\"," ${args[*]})]" | sed \'s/,]$/]/\')"\n    - name: Debug arguments\n      shell: bash\n      run: |\n        echo Running slsa-provenance with following arguments\n        echo ${{ steps.compose-args.outputs.provenance_args }}\n    - …
Run Code Online (Sandbox Code Playgroud)

github docker github-actions

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