有没有办法确定Azure存储帐户的已用和/或剩余容量?我知道当前的大小限制是每个存储帐户100TB,我正在寻找使用/剩余的字节数,或者失败,使用/剩余的百分比.但是,即使在Azure门户中,我也看不到任何监视方式,更不用说通过API以编程方式进行监视.
我目前正在编写大量的async
库代码,并且我知道ConfigureAwait(false)
在每次异步调用之后添加的做法,以避免将延续代码编组回原始(通常是UI)线程上下文.因为我不喜欢未标记的布尔参数,所以我倾向于将其写为ConfigureAwait(continueOnCapturedContext: false)
相反.
我添加了一个扩展方法,使其更具可读性(并在某种程度上减少了输入):
public static class TaskExtensions
{
public static ConfiguredTaskAwaitable<TResult> WithoutCapturingContext<TResult>(this Task<TResult> task)
{
return task.ConfigureAwait(continueOnCapturedContext: false);
}
public static ConfiguredTaskAwaitable WithoutCapturingContext(this Task task)
{
return task.ConfigureAwait(continueOnCapturedContext: false);
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在我可以有类似的东西await SomethingAsync().WithoutCapturingContext()
而不是await SomethingAsync().ConfigureAwait(continueOnCapturedContext: false)
.我认为这是一个改进,但是当我必须async
在同一个代码块中调用多个方法时,即使这种情况开始变得很糟糕,因为我最终得到类似于此的东西:
await FooAsync().WithoutCapturingContext();
var bar = await BarAsync().WithoutCapturingContext();
await MoreFooAsync().WithoutCapturingContext();
var moreBar = await MoreBarAsync().WithoutCapturingContext();
// etc, etc
Run Code Online (Sandbox Code Playgroud)
在我看来,它开始使代码的可读性低得多.
我的问题基本上是这样的:有没有办法进一步减少这种情况(除了缩短扩展方法的名称)?
我刚刚开始使用Visual Studio 2015,发现在调试自动实现的属性时,它与VS2012/VS2013的行为不同.
假设我在类中定义了一个属性:
public int MyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
即.一个自动实现的属性,其中getter和setter由编译器有效生成.
我稍后可能会引用此属性,作为方法调用的一部分,例如:
{
DoSomeStuff(MyProperty);
}
Run Code Online (Sandbox Code Playgroud)
如果我想调试调用DoSomeStuff
并在该行上放置一个断点,然后等待它被命中,F11进入调用,VS2015调试器现在进入getter for MyProperty
(在以前的版本中,用于自动实现的属性) ,它会忽略getter和setter).
我发现了一篇博客文章,描述了将自动获取器和设置器上的断点作为新功能的能力,我可以看到在某些情况下这可能有用.但是,在我的情况下,我对断点或步入这些方法不感兴趣,特别是因为我们的代码库有许多自动实现的属性,并且对它们的访问通常是嵌套的(例如DoSomeStuff(X.Y.Z)
).
简而言之,是否可以在VS2015中为自动实现的属性禁用新的Step Into行为,如果是,如何?
(我注意到调试选项中的"跳过属性和运算符"复选框,但我不想跨越所有属性getter/setter,只是那些用于自动实现属性的属性.)
我一直在阅读Microsoft的模式和实践组(高度可扩展的解决方案的数据访问:使用SQL,NoSQL和Polyglot持久性)中的文档.
在第3章的"从SQL Server数据库中检索数据"一节中,作者讨论了使用Entity Framework从数据库加载实体.这是他们的示例代码:
using (var context = new PersonContext())
{
Person person = null;
using (var transactionScope = this.GetTransactionScope())
{
person = context.Persons
.Include(p => p.Addresses)
.Include(p => p.CreditCards)
.Include(p => p.EmailAddresses)
.Include(p => p.Password)
.SingleOrDefault(p => p.BusinessEntityId == personId);
transactionScope.Complete();
}
// etc...
}
Run Code Online (Sandbox Code Playgroud)
请注意通过该GetTransactionScope
方法使用自定义事务范围,在其基本上下文类中实现,如下所示:
public abstract class BaseRepository
{
private static TransactionOptions transactionOptions = new TransactionOptions()
{
IsolationLevel = IsolationLevel.ReadCommitted
};
protected virtual TransactionScope GetTransactionScope()
{
return new TransactionScope(TransactionScopeOption.Required, transactionOptions);
} …
Run Code Online (Sandbox Code Playgroud) 我遇到了类型推断和C#编译器的问题.阅读了这个问题和这个问题后,我想我明白为什么它不起作用:我想知道的是,如果有任何方法可以解决问题以获得我喜欢的调用语义.
这里有一些代码说明了我的问题(对不起长度,这是我可以减少它的最短时间):
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace StackOverflow
{
interface IQuery<TResult> { }
class MeaningOfLifeQuery : IQuery<int> { }
interface IQueryHandler<TQuery, TResult> where TQuery : class, IQuery<TResult>
{
Task<TResult> ExecuteAsync(TQuery query);
}
class MeaningOfLifeQueryHandler : IQueryHandler<MeaningOfLifeQuery, int>
{
public Task<int> ExecuteAsync(MeaningOfLifeQuery query)
{
return Task.FromResult(42);
}
}
interface IRepository
{
Task<TResult> ExecuteQueryDynamicallyAsync<TResult>(IQuery<TResult> query);
Task<TResult> ExecuteQueryStaticallyAsync<TQuery, TResult>(TQuery query)
where TQuery : class, IQuery<TResult>;
}
class Repository : IRepository
{
public Repository(IServiceProvider serviceProvider)
{
_serviceProvider = …
Run Code Online (Sandbox Code Playgroud) 我正在使用protobuf-net来序列化许多类型,其中一些类型是从基类型继承而来的.我知道Protocol Buffers规范不支持继承,并且protobuf-net中的支持基本上是一种解决方法,因为这样.
而不是使用protobuf-net属性我正在配置自定义RuntimeTypeModel
,并使用Add
和AddSubType
方法.我不太了解的是我应该如何确定传递给AddSubType
方法的字段编号使用哪些数字(也就是在ProtoInclude
属性中使用的数字).
这个问题和其他几个问题并没有真正描述如何选择场数,事实上我已经看到了许多不同的变化:4和5; 7和8; 101&102&103; 20; 500; 显然他们的选择是为了不相互冲突,但他们是如何选择的呢?什么决定从哪个数字开始?
下面的代码是一个人为的例子,但它确实与我的heirarchy(一个Event
有两个派生子类型的基类型)相匹配.
using System;
using System.Collections.Generic;
using ProtoBuf.Meta;
namespace Test
{
public sealed class History
{
public History()
{
Events = new List<Event>();
}
public ICollection<Event> Events { get; private set; }
}
public enum EventType
{
ConcertStarted, ConcertFinished, SongPlayed
}
public class Event
{
public EventType …
Run Code Online (Sandbox Code Playgroud) 运行 WPF 应用程序时,我在输出窗口中看到许多警告消息:
System.Windows.ResourceDictionary Warning: 9 : Resource not found
Run Code Online (Sandbox Code Playgroud)
然而,与我在 Stack Overflow 上看到的所有相关问题不同(比如这个),这些警告都没有告诉我哪个资源键(或键)是/导致了问题。
有没有办法让 Visual Studio 告诉我这些信息?我曾尝试更改调试选项中的 WPF 跟踪设置,但这些都没有效果。奇怪的是,即使相关的跟踪设置设置为“关闭”,我仍然收到资源字典警告。
背景资料
我正在使用一个名为Stylet的 MVVM 库,当一个视图切换到另一个视图时,这些警告似乎会出现。除了轻微(但明显)的延迟外,警告似乎不会影响应用程序的运行。
也就是说,我怀疑警告更有可能是由于另一个库Material Design 引起的,因为我在开发后期添加了这一点,直到在那之后才记得看到警告。
我的代码分为两个不同的 C# 项目:一个用于 UI 内容(控件、样式、转换器、画笔等),另一个用于主应用程序。UI 项目引用了 Stylet 和 Material Design NuGet 包;UI 项目然后由主应用程序项目引用。这两个项目都是 .Net Core 3.1。
我的要求是每当我点击按钮时将进度条的颜色更改为红色.我不想注释掉Application.EnableVisualStyles().
所以我尝试使用SendMessage.我的代码:
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
private const Int32 WM_USER = 0x0400;
private const Int32 CCM_FIRST = 0x2000;
private const Int32 PBM_SETBARCOLOR = WM_USER + 9;
private const Int32 PBM_SETBKCOLOR = CCM_FIRST + 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Invoke((MethodInvoker)delegate
{
SendMessage(this.progressBar1.Handle, PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(Color.Red));
SendMessage(this.progressBar1.Handle, PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(Color.Red));
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Value = progressBar1.Maximum;
});
}
Run Code Online (Sandbox Code Playgroud)
它不起作用.我不知道为什么.你可以帮忙吗?
当Azure WebJobs因任何原因停止或重新启动时,在主机控制台进程终止之前,默认宽限期为5秒.显然,可以通过在WebJob发布到Azure时将"settings.job"文件包含在内来增加此时间段,如此处所述.不幸的是,我无法让这个工作用于我的网站,关机时间总是 5秒,尽管这是一个连续的WebJob,发布到设置为"Always On"的基本应用服务.
根据链接中的示例代码,我的Program.cs如下所示:
class Program
{
static void Main()
{
var host = new JobHost();
host.RunAndBlock();
}
}
Run Code Online (Sandbox Code Playgroud)
和我的Functions.cs看起来像这样:
public class Functions
{
public static async Task ProcessQueueMessageAsync(
[QueueTrigger("testq")] string message, TextWriter log,
CancellationToken cancellationToken)
{
Console.WriteLine("Function started.");
try
{
await Task.Delay(TimeSpan.FromMinutes(10), cancellationToken);
}
catch (TaskCanceledException)
{
Shutdown().Wait();
}
Console.WriteLine("Function completed succesfully.");
}
private static async Task Shutdown()
{
Console.WriteLine("Function has been cancelled. Performing cleanup ...");
await Task.Delay(TimeSpan.FromSeconds(30));
Console.WriteLine("Function was cancelled …
Run Code Online (Sandbox Code Playgroud) 在 C# 中,异步方法(用async
关键字标记的那些)的约定之一是它们的名称应以“Async”后缀结尾:
public async Task MyMethod(/* params */) ... <-- Bad, missing "Async" suffix
public async Task MyMethodAsync(/* params */) ... <-- Good, as per convention
Run Code Online (Sandbox Code Playgroud)
我最近写了很多异步代码,我担心我可能忘记在某些方法上加上“Async”后缀。
是否有任何正则表达式可以用来查找那些标有async
但没有“Async”后缀的方法?如果可能的话,可以反向进行(即找到方法也有一个“异步”后缀,但那些没有标有async
关键字)?
如果这对答案有任何影响,我希望将正则表达式粘贴到 Visual Studio 中的“在文件中查找”对话框中。
我猜像 FxCop、CodeRush 或 ReSharper 这样的工具可能已经就这样的事情发出警告,但是鉴于我的工作场所,后两者不是一个选项,如果我能做到这一点,我宁愿不必安装和配置前者使用简单的正则表达式。
我一直在使用AutoMapper在接口和该接口的具体实现之间进行映射.我假设如果我传入AutoMapper Map<TDestination>
方法的类型与返回类型相同,则返回原始对象(作为一种短路操作).我的假设是错误的:事实上,在看了之后我注意到该方法的文档明确指出:
执行从源对象到新目标对象的映射.源类型是从源对象推断出来的.(大胆强调我的)
我敲了这个快速控制台应用程序只是为了验证:
using System;
using AutoMapper;
namespace ConsoleApplication
{
class Program
{
interface IFoo
{
string Bar { get; }
}
class Foo : IFoo
{
public string Bar { get; set; }
}
static void Main(string[] args)
{
Mapper.CreateMap<IFoo, Foo>();
IFoo a = new Foo { Bar = "baz" };
Foo b = Mapper.Map<Foo>(a);
Console.WriteLine(Object.ReferenceEquals(a, b)); // false
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我知道这个行为,我可以针对我的特定用例优化它,但我想知道是否有另一种使用AutoMapper的方法,它将以上述方式"短路"(即给我如果类型与我想要的目标类型相同,则返回原始对象?
我有这样的代码:(将自定义对象的集合加载到内存和列表框项目中)
public class Product : INotifyPropertyChanged
{
// these four doesn't matter, just Product's simple data
public string nDB_No { get; set; }
public string fdGrp_Cd { get; set; }
public string long_Desc { get; set; }
public int refuse { get; set; }
// I do not load this Collection right away, only after explicit call
public ObservableCollection<Ingredient> ingredients { get; set; }
public Product() {sets all null}
public static ObservableCollection<Product> LoadProductsFromList(List<string> productList) {gets products data from SQLServer DB} …
Run Code Online (Sandbox Code Playgroud) 在 PowerShell 中,假设我有一个字符串对象的集合:
'a b c'.split()
Run Code Online (Sandbox Code Playgroud)
我可以将它们通过管道传输到输出流:
'a b c'.split() | Write-Host
Run Code Online (Sandbox Code Playgroud)
所以我在不同的行上得到“a”、“b”和“c”。
现在:如何更改此设置以使每行(字符串)都有一个前导制表符?
我知道选项卡由 't 表示,所以我的第一次尝试是这样的:
'a b c'.split() | Write-Host "`t$_"
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
Write-Host :输入对象无法绑定到命令的任何参数,因为该命令不采用管道输入,或者输入及其属性与采用管道输入的任何参数都不匹配。
好吧,让我们拿出选项卡并只使用 $_ ,我认为它代表管道返回的“当前”对象。
'a b c'.split() | Write-Host $_
Run Code Online (Sandbox Code Playgroud)
同样的错误:
Write-Host :输入对象无法绑定到命令的任何参数,因为该命令不采用管道输入,或者输入及其属性与采用管道输入的任何参数都不匹配。
显然我在这里从根本上误解了一些东西,所以我希望有人能指出那是什么!另外,忽略 的使用Write-Host
:我只是将它用于说明目的,我的目的是Write-Verbose
最终通过管道传递。
c# ×11
azure ×2
wpf ×2
async-await ×1
automapper ×1
datatemplate ×1
debugging ×1
inheritance ×1
listbox ×1
piping ×1
powershell ×1
progress-bar ×1
protobuf-net ×1
regex ×1
stopwatch ×1
winforms ×1