Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

1
如何从C#类生成.proto文件或使用“代码优先gRPC”?
我想在ASP.NET Core Web应用程序中将gRPC与.NET Core 3一起使用。如何从现有的C#类和模型对象生成必要的.proto文件?我不想重写反映现有代码的.proto文件,而是希望从类和模型对象自动生成.proto文件。 我调用此方法来注册我的服务类。 builder.MapGrpcService<MyGrpcService>(); public class MyGrpcService { public Task<string> ServiceMethod(ModelObject model, ServerCallContext context) { return Task.FromResult("It Worked"); } } ModelObject具有[DataContract]和[DataMember]具有订单属性。 这可能吗?我在网上看到的每个示例都以.proto文件开头。我已经在MyGrpcService该类中定义了所需的服务方法。但这也许只是标准的做事方式的倒退... 像旧的.NET远程处理之类的东西非常理想,您可以从远程端点请求一个接口,并且它神奇地用于gRPC来回通信,但这也许太简单了。

2
C#不能使`notnull`类型为可空
我正在尝试创建类似于Rust Result或Haskell的类型Either并且我已经了这一点: public struct Result<TResult, TError> where TResult : notnull where TError : notnull { private readonly OneOf<TResult, TError> Value; public Result(TResult result) => Value = result; public Result(TError error) => Value = error; public static implicit operator Result<TResult, TError>(TResult result) => new Result<TResult, TError>(result); public static implicit operator Result<TResult, …

2
不需要分配结构类型的out参数
当我在代码审查期间不小心在函数中注释一行时,我注意到我的代码中有一些奇怪的行为。复制非常困难,但在此我将描述一个类似的示例。 我有这个测试课: public class Test { public void GetOut(out EmailAddress email) { try { Foo(email); } catch { } } public void Foo(EmailAddress email) { } } 没有分配给的电子邮件GetOut,通常会引发错误: 必须在控制离开当前方法之前将out参数'email'分配给 但是,如果EmailAddress在单独程序集中的结构中,则不会创建错误,并且一切都可以编译。 public struct EmailAddress { #region Constructors public EmailAddress(string email) : this(email, string.Empty) { } public EmailAddress(string email, string name) { …
9 c#  .net 

1
在Netcore API 3.1中读取AuthorizationFilterContext
我有一个正在运行的netcore 2.2项目,在该项目中,我实施了一个自定义策略来检查API密钥。 在startup.cs中,我要像这样添加此策略 //Add Key Policy services.AddAuthorization(options => { options.AddPolicy("AppKey", policy => policy.Requirements.Add(new AppKeyRequirement())); }); 在我的AppKeyRequirement中,我从AuthorizationHandler继承并像这样解析传入请求中的键 protected override Task HandleRequirementAsync(AuthorizationHandlerContext authContext, AppKeyRequirement requirement) { var authorizationFilterContext = (AuthorizationFilterContext)authContext.Resource; var query = authorizationFilterContext.HttpContext.Request.Query; if (query.ContainsKey("key") && query.ContainsKey("app")) { // Do stuff 在netcore 3.1中不起作用 我收到以下错误: 无法将类型为“ Microsoft.AspNetCore.Routing.RouteEndpoint”的对象转换为类型为“ Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext”的对象。 在核心3及更高版本中执行此操作的正确方法是什么? 正如Kirk Larkin指出的那样,.net 3.0及更高版本中的正确方法是将IHttpContextAccessor注入到Auth处理程序中并使用它。 …

1
析构函数的垃圾收集器行为
我有一个简单的类,定义如下。 public class Person { public Person() { } public override string ToString() { return "I Still Exist!"; } ~Person() { p = this; } public static Person p; } 在主要方法中 public static void Main(string[] args) { var x = new Person(); x = null; GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine(Person.p == null); …

1
.Net Core 3 IStringLocalizer.WithCulture(CultureInfo)已过时
我已经将项目从.Net Core 2.2升级到.Net Core 3.0。 在尝试解决所有警告和错误之后,我现在尝试为该警告的解决方案提供资金: 'IStringLocalizer.WithCulture(CultureInfo)' is obsolete: 'This method is obsolete. Use `CurrentCulture` and `CurrentUICulture` instead.' 我正在使用它来更改每个登录用户的网站语言。我有此实现可更改每个用户的网站文化: public class CultureLocalizer : ICultureLocalizer { private readonly IStringLocalizer localizer; public CultureLocalizer(IStringLocalizerFactory factory) { var type = typeof(Resources.PageResources); var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName); localizer = factory.Create("PageResources", assemblyName.Name); } // if we …

1
从Azure应用配置动态更新.net核心配置
我正在尝试做的事情: 我试图使用.net核心2.1 mvc Web应用程序设置Azure应用配置,并在Azure应用配置中使用一个前哨键,目的是能够以天蓝色更改键,而没有一个键将在我的应用程序中更新,直到哨兵值更改。从理论上讲,这应该允许我安全地热交换配置。 我的问题是: 执行此操作时,没有可用的WatchAndReloadAll()方法来监视IWebHostBuilder上的哨兵,并且备用的Refresh()方法似乎并未刷新状态。 背景信息以及我尝试过的操作: 上周,我参加了圣地亚哥的VS Live,并观看了有关Azure App Configuration的演示。我在尝试使应用程序在隐含它时刷新配置值时遇到了一些问题,因此我也参考了此演示,介绍了如何执行此操作。相关部分的时间大约是10分钟。但是,该方法在IWebHostBuilder上似乎不可用。 我正在参考的文档: 在官方文档中没有对此方法的引用,请参阅doc quickstart .net core和doc dynamic configuration .net core 我的环境: 使用从Visual Studio Enterprise 2019运行的dot net core 2.1,以及适用于Microsoft.Azure.AppConfiguration.AspNetCore 2.0.0-preview-010060003-1250的最新预览nuget程序包 我的代码: 在演示中,他们通过CreateWebHostBuilder(string [] args)方法创建了一个IWebHostBuilder,如下所示: public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { var settings = config.Build(); config.AddAzureAppConfiguration(options …

1
请求标头未转发到IdentityServer4
我正在使用ocelot作为我的微服务的API网关,并使用IdentityServer4进行身份验证。在ocelot配置文件中,我添加了“ AuthenticationOptions”并设置了api密钥。在启动中,添加身份服务器。在身份服务器中,我使用标头中的值来动态构建连接字符串。当我发送获取令牌的请求时,可以在身份服务中访问标头。但是当我发送带有令牌的下一个请求时,原始标头不可用。在身份服务中只能看到“主机”标头。 在将请求路由到身份服务器时,是否可以保留原始标头? Startup.cs(添加身份服务器) services .AddAuthentication() .AddIdentityServerAuthentication("APIParts", options => { options.Authority = "http://localhost:60168"; options.RequireHttpsMetadata = false; options.ApiName = "Parts"; options.SupportedTokens = SupportedTokens.Both; }); ocelot.json ReRoutes": [ { "DownstreamPathTemplate": "/connect/token", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 60168 } ], "UpstreamPathTemplate": "/token", "UpstreamHttpMethod": [ "Post" ] }, { "DownstreamPathTemplate": "/api/Parts/Inventory", …

1
防止控制台应用程序(.NET Core)打印“代码为0退出”。在VS2019中
当我在Visual Studio社区2019(版本16.3.1)中使用Ctrl + F5(无需调试即可启动)启动控制台应用程序(.NET Core)时,以下消息将附加在控制台窗口的末尾: C:\ HelloWorld \ bin \ Debug \ netcoreapp3.0 \ HelloWorld.exe(进程1672)退出,代码为0。 using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } } 有什么方法可以防止Visual Studio 2019打印此消息?我尝试通过更改以下选项来防止该解决方案出现在“输出”窗口中,该选项包括:“工具”>“选项”>“调试”>“输出”窗口>“进程退出消息” =“关闭”,但是在控制台窗口中无效。 注意:此消息不会在Visual Studio 2017中显示。它只会在Visual Studio 2019中显示,并且仅在.NET Core应用程序中显示。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.