我正在使用一个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) 我想在Castle.Windsor中注册一个带有2个接口的类.
这段代码是否有效...我是否只有两个接口的实例...
Component.For<IEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Component.For<IOutlookEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Run Code Online (Sandbox Code Playgroud)
我需要仔细检查一下,因为我的环境应该始终是同一个实例...
因此,当我使用IEnvironment接口解析时,我应该获得与使用IOutlookEnvironment解析组件时相同的实例
我有以下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的解决方案?
在我们的IocContainer中,我们注册了以下对象,该对象将用户名传递给框架.
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服务对象.
我在我的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) 是否可以使用 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) 如何提供路径 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错误.
我更改了文件夹的名称后,我的一个文件被跟踪了两次.
Git现在跟踪以下两个文件,这两个文件实际上是同一个文件.
SRC /网站/脚本/ common.js
SRC /网站/脚本/ common.js
如何删除第二个被跟踪的?此文件夹不再存在,因为我将其重命名Scripts为scripts.
我在我的应用程序中使用 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) 我正在尝试将对虚假对象的调用代理到实际实现.这样做的原因是我希望能够使用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 …
我正在尝试在复合 GitHub Action 中传递一些动态创建的参数。
\n然而,文档缺少有关如何在这种情况下将参数传递给 docker 容器的示例。
\nhttps://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsstepsuses
\n在这里看看我想要实现的目标。
\nruns:\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) c# ×5
github ×3
asp.net-mvc ×2
async-await ×1
asynchronous ×1
c#-4.0 ×1
docker ×1
enums ×1
fakeiteasy ×1
git ×1
go ×1
json ×1
json.net ×1
lets-encrypt ×1
mocking ×1
mspec ×1
node.js ×1
ssl ×1
tls1.2 ×1
unit-testing ×1