小编Eut*_*rpy的帖子

C奇怪的宏语法

我找到了这个C代码示例,我绝对感到困惑:

#include <stdio.h>
#define M(a,b) a%:%:b

main()
{
  int a=1, b=2, ab[]={10,20}, c;
  printf( "%d", M(a,b)<:a:> );
  printf( "%d", M(a,b)<:a:>?a:b );
  printf( "%d", c=M(a,b)<:a:>?a:b );
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释这应该做什么吗?它甚至没有在Visual Studio中编译,但我在线运行(在ideone.com上)并打印2011,这也增加了混乱.

c macros preprocessor-directive

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

Azure密钥保管库:访问被拒绝

我有以下代码从Azure密钥保险库获取密码:

public static async Task<string> GetToken(string authority, string resource, string scope)
    {
        var authContext = new AuthenticationContext(authority);
        ClientCredential clientCred = new ClientCredential(...); //app id, app secret
        AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

        if (result == null)
            throw new InvalidOperationException("Failed to obtain the JWT token");

        return result.AccessToken;
    }

    public static string GetSecret(string secretName)
    {
        KeyVaultClient keyVaultClient = new KeyVaultClient(GetToken);
        try
        {
            return keyVaultClient.GetSecretAsync("my-key-vault-url", secretName).Result.Value;
        }
        catch(Exception ex)
        {
            return "Error";
        }
    }
Run Code Online (Sandbox Code Playgroud)

我得到的错误是"访问被拒绝",这(我认为)意味着id,secret和vault的url都没问题.但是,我不知道我能做些什么来修复此错误,Azure门户中是否有可能阻止我读取密码的设置?

c# azure azure-keyvault

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

ASP.Net Core 异常处理中间件

我正在尝试在我的 ASP.Net Core 3.0 Web API 项目中使用中间件进行异常处理:

public class ErrorHandlingMiddleware
{
    private readonly RequestDelegate next;

    public ErrorHandlingMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await next(context);
        }
        catch (Exception ex)
        {
            await HandleException(context, ex);
        }
    }

    private static Task HandleException(HttpContext context, Exception ex)
    {
        HttpStatusCode code = HttpStatusCode.InternalServerError; // 500 if unexpected

        // Specify different custom exceptions here
        if (ex is CustomException) code = HttpStatusCode.BadRequest;

        string result = JsonConvert.SerializeObject(new { error = ex.Message …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-middleware asp.net-core-webapi

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

在启动时获取单例服务实例

我有以下情况:

启动.cs

services.AddSingleton<IConfigurationManager, ConfigurationManager>();
ConfigurationManager configurationManager = new ConfigurationManager();
services.AddDbContext<MyContext>(options => 
    options.UseSqlServer(configurationManager.DatabaseConnectionString));
Run Code Online (Sandbox Code Playgroud)

因此,为了创建我的上下文,我需要它configurationManager提供给我的连接字符串。但是,我仍然想保留ConfigurationManager即服务。

有没有一种方法可以在不显式实例化的情况下执行此操作,configurationManager或者是否可以像这样保留它?

c# dependency-injection dbcontext entity-framework-core asp.net-core

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

在 Azure 门户中跟踪 System.Diagnostics.Trace 日志

在我的网络应用程序中,我使用

System.Diagnostics.Trace.WriteLine(...);
Run Code Online (Sandbox Code Playgroud)

用于诊断。应用程序发布到 Azure 部署槽。我想知道在 Azure 门户(或 Visual Studio 的云资源管理器)中哪里可以找到这些日志。

c# system.diagnostics azure azure-application-insights visual-studio-2017

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

ASP.NET Core Web API 自定义路由不起作用

我有一个 ASP.NET Core Web API 项目和以下控制器:

[Route("/api/v0.1/controller")]
[ApiController]
public class MyController : ControllerBase
{
   [HttpGet("/test")]
   public ActionResult Test() { return null; }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行该项目时,在/api/v0.1/controller/test处,我得到“找不到页面”,并且我看不到我在哪里犯了错误。

c# asp.net-core asp.net-core-webapi asp.net-core-routing

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

C# 流读取器 ReadToEnd() 缺少最后一个字符

我正在尝试使用 AES 解密 C# 中的字符串:

public static string AesDecrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
    string plaintext = null;

    // Create an Aes object with the specified key and IV
    using Aes aesAlg = Aes.Create();
    aesAlg.Padding = PaddingMode.Zeros;
    aesAlg.Key = Key;
    aesAlg.IV = IV;

    // Create a decryptor to perform the stream transform
    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

    // Create the streams used for decryption
    using MemoryStream msDecrypt = new MemoryStream(cipherText);
    using CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
    using …
Run Code Online (Sandbox Code Playgroud)

c# streamreader

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

C++循环遍历对象并获取它们的地址

我有以下代码片段:

vector<shape*> triangle_ptrs;
for (auto x : triangles)
    triangle_ptrs.push_back(&x);
Run Code Online (Sandbox Code Playgroud)

triangle是一个派生自shape类的类,它triangles是一个std::vector三角形:

std::vector<triangle> triangles;
Run Code Online (Sandbox Code Playgroud)

我需要保存三角形的地址,但是当我遍历集合时,它们的地址似乎是相同的.我该如何解决这个问题?

c++ pointers stdvector

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

C++拷贝构造函数:尝试引用已删除的函数

我有一个名为的类classA,如下所示:

class classA {
    private:        
        char* data; 
    public:
        classA(const classA&) = delete;
        ~classA();      
    };

~classA()
    {
        delete[] data;
    }
Run Code Online (Sandbox Code Playgroud)

在另一个类中,让我们调用它classB,作为成员,我有一个共享指针classA:

class classB
    {
    private:
        std::shared_ptr<classA> ptrA;
    public: 
        classB(std::shared_ptr<classA>);
    };

classB(std::shared_ptr<classA> sp) : ptrA(sp)
    {}
Run Code Online (Sandbox Code Playgroud)

这是我实例化我的方式classB:

classA ca;
classB cb(std::make_shared<classA>(ca));
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

试图引用已删除的功能

显然,我试图引用我定义的复制构造函数deleted(有一个原因,不应复制此类的对象).但我很困惑为什么复制构造函数被调用,因为我传递共享指针,以及如何避免这种情况.

c++ copy-constructor shared-ptr

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

C# 将 IFormFile 读入 byte[]

我正在尝试IFormFile从这样的 HTTP POST 请求中读取接收到的信息:

 public async Task<ActionResult> UploadDocument([FromForm]DataWrapper data)
    {
        IFormFile file = data.File;
        string fileName = file.FileName;
        long length = file.Length;
        if (length < 0)
            return BadRequest();
        using FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
        byte[] bytes = new byte[length];
        fileStream.Read(bytes, 0, (int)file.Length);

        ...

    }
Run Code Online (Sandbox Code Playgroud)

但在这行执行后,出了点问题:

fileStream.Read(bytes, 0, (int)file.Length);
Run Code Online (Sandbox Code Playgroud)

的所有元素bytes都为零。

此外,在我的 Visual Studio 项目中创建了同名文件,我不希望发生这种情况。

c# filestream iformfile

3
推荐指数
2
解决办法
5141
查看次数