我有以下代码:
$.ajax({
url: "http://localhost:15797/api/values",
type: 'get',
contentType: 'application/json',
headers: {
"Authorization": "Bearer " + token
}
})
Run Code Online (Sandbox Code Playgroud)
工作正常,但我想在不使用Ajax的情况下做到这一点,我想要这样的东西:
$.ajax({
url: "http://localhost:15797/api/values",
type: 'get',
contentType: 'application/json',
headers: {
"Authorization": "Bearer " + token
}
})
Run Code Online (Sandbox Code Playgroud)
可能吗?或者只是在没有XMLHttpRequest的情况下做类似的事情?怎么样?
我正在使用 Azure DevOps,我需要将 bash 命令的返回设置为一些变量,例如我有以下内容:
variables:
VERSION: 7.2 # works fine
FILE_VERSION: ${{cat public/VERSION}} # syntax error
Run Code Online (Sandbox Code Playgroud)
我尝试了一些${{}}没有成功的变体,并且找不到正确的语法,但我认为它一定是可能的。
我有一个带有@Controllers和@RestControllers 的Spring MVC应用程序。我当时在想:当我遇到异常时@Controller,它将由我处理;@ControllerAdvice当我遇到异常时@RestController,将由我处理@RestControllerAdvice...但是现在,我认为这不是事情的工作方式,因为我@ControllerAdvice正在捕获所有内容,甚至是由@RestController... 引发的任何异常,我不知道是否应该发生这种情况。这是我的代码:
@ControllerAdvice
public class ExceptionHandlerController {
private final String DEFAULT_ERROR_VIEW = "error/default";
@ExceptionHandler(Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e)
{
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("danger", e.getMessage());
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
}
@RestControllerAdvice
public class ExceptionHandlerRestController {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
return new ResponseEntity<>(" test "+e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Run Code Online (Sandbox Code Playgroud) 我想在 ConfigureServices 中注册一个通用类(需要这个类,因为我想实现模式:存储库和工作单元)以获得依赖注入。但是我不知道怎么做。
这是我的界面:
public interface IBaseRepository<TEntity> where TEntity : class
{
void Add(TEntity obj);
TEntity GetById(int id);
IEnumerable<TEntity> GetAll();
void Update(TEntity obj);
void Remove(TEntity obj);
void Dispose();
}
Run Code Online (Sandbox Code Playgroud)
它的实现:
public class BaseRepository<TEntity> : IDisposable, IBaseRepository<TEntity> where TEntity : class
{
protected CeasaContext context;
public BaseRepository(CeasaContext _context)
{
context = _context;
}
/*other methods*/
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的事情:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by …Run Code Online (Sandbox Code Playgroud) 我有一个接收此Func作为参数的方法
Func<IContext, IReadOnlyCollection<T>> func
Run Code Online (Sandbox Code Playgroud)
碰巧我需要记录上面提到的Func执行的持续时间.我已经有一个函数来记录执行的持续时间,这个方法有这个签名:
T LogExecutionDuration<T>(Func<T> func);
Run Code Online (Sandbox Code Playgroud)
我的问题是:有一种方法可以使用实际方法,还是我需要创建一个新方法来记录它?
我想包装:
Func<IContext, IReadOnlyCollection<T>> func
//into
T LogExecutionDuration<T>(Func<T> func);
//In a way that the "Func<IContext, IReadOnlyCollection<T>> func"
//could be passed as parameter for LogExecutionDuration as Func<T> func
Run Code Online (Sandbox Code Playgroud)
我认为类似的东西是可能的,因为以下是可能的:
public static T Foo<T>(Func<T> func){
return func();
}
public static TResult Foo<T, TResult>(Func<T, TResult> func, T param){
return func(param);
}
var foo1 = Foo(() => someObj.someMethod(someParam));
var foo2 = Foo(someObj.someMethod, someParam);
Run Code Online (Sandbox Code Playgroud)
也许我误解了一些事情,如果是这样,请解释我......
我很简单不知道如何使以下代码工作,它假设在无限循环中永远运行,实际上当我await Task.Delay从方法中删除时它会工作Method_FOO,但我需要Method_FOO异步。
我认为要使这项工作该Thread.Start()方法需要是“可等待的”(不仅仅是它运行的代码),但 Thread.Start 是无效的。我注意到,如果我用 eg.: 阻止执行,Console.ReadLine它会打印Worked字符串,但这不是解决方案,而且在现实生活中很糟糕。
这段代码只是一个例子,但线程需要在一个无限循环中运行(这不是我可以改变的),我需要异步方法,因为我需要消耗一些 websocket,而且看起来没有同步客户端 websocket 类在 C# 中。
但是,这个问题必须有一个简单/体面的解决方案。
public static class Program
{
public static async Task Main(string[] args)
{
var cancellationTokenSource = new CancellationTokenSource();
var thread1 = new Thread(async () => await Run(cancellationTokenSource.Token, "threadName1", Method_FOO));
var thread2 = new Thread(async () => await Run(cancellationTokenSource.Token, "threadName2", Method_FOO));
thread1.Start();
thread2.Start();
}
private static async Task Method_FOO(CancellationToken cancellationToken)
{
Console.WriteLine("It is called...");
await Task.Delay(300, …Run Code Online (Sandbox Code Playgroud) c# ×3
lambda ×2
asp.net-core ×1
async-await ×1
azure-devops ×1
controller ×1
html ×1
http-headers ×1
linq ×1
spring ×1
token ×1
yaml ×1