Tag: c# 5.0

C#中的重载分辨率,扩展方法和通用性

我在C#源代码中有以下场景: class A{} class Dispatch{} static class DispatchExt { public static void D(this Dispatch d, int a) { Console.WriteLine(“Generic D chosen with a = ” + a.ToString()); } public static void D(this Dispatch d, int a) { Console.WriteLine(“D chosen with a = ” + a.ToString()); } } class Program { static void D(Dispatch d, int […]

ContinueWith失去SynchronizationContext

在下面的代码段中, SynchronizationContext将丢失,因此还有CurrentCulture和CurrentUICulture 。 Log()来自这个答案 。 public async Task Index() { Log(“before GetAsync”); await new HttpClient().GetAsync(“http://www.example.com/”) .ContinueWith(request => { Log(“ContinueWith”); request.Result.EnsureSuccessStatusCode(); }, TaskContinuationOptions.AttachedToParent); return View(); } static void Log(string message) { var ctx = System.Threading.SynchronizationContext.Current; System.Diagnostics.Debug.Print(“{0}; thread: {1}, context: {2}, culture: {3}, uiculture: {4}”, message, System.Threading.Thread.CurrentThread.ManagedThreadId, ctx != null ? ctx.GetType().Name : String.Empty, System.Threading.Thread.CurrentThread.CurrentCulture.Name, System.Threading.Thread.CurrentThread.CurrentUICulture.Name); } […]

有没有办法在C#5中模仿C#6 Null-Conditional运算符

我有一种情况需要在对象初始化程序中分配一些对象的属性。 其中一些对象可以为null,我需要访问它们的属性,问题是它们太多了,使用if / else的东西并不好。 例 visits = visitJoins.AsEnumerable().Select(joined => new VisitPDV() { VisiteId = joined.Visite.VisiteId.ToString(), NomPointDeVente = joined.VisitePdvProduit.PointDeVente.NomPointDeVente, }); joined.VisitePdvProduit可以为null,问题是有几十个这样的赋值(我只用了一个来缩短代码) C# 6 Null-Conditional operator是这种情况的完美解决方案,问题是我在这个项目的C# 5上,有没有办法模仿呢?

在Autofac中注册异步工厂

我有一个从存储库中获取的Wallet类。 我正在尝试在Autofac中正确注册,因此使用钱包的类可以注入适当的实例。 问题是存储库使用异步方法(返回Task)。 Autofac是否支持此类案例? 这不起作用: cb.RegisterType() .As() .SingleInstance(); cb.Register(async c => await c.Resolve().CreateAsync(App.WalletPath)); cb.RegisterType() .AsSelf(). .SingleInstance(); 在应用程序的某个地方我只有: class ViewModel { public ViewModel(Wallet wallet) { //nothing fancy here } } 当调用container.Resolve()我得到一个例外,说钱包没有注册。

将OperationContext传播到异步WCF调用

使用WCF中的C#5 Async-Await,在等待其余代码继续在另一个线程上之后,我们将松开当前操作上下文。 (OperationContext.Current为null)。 我正在开发一个调用另一个外部服务的WCF服务。 并且在外部服务调用中使用了一些自定义绑定扩展来访问操作上下文。 所以我需要在这个调用期间传播Context,它不能将操作上下文复制到局部变量中。 我的配置看起来像这样 其中“ MyBindingExtention”访问operationcontext以获取一些信息。 public async Task GetExternalData(int value) { var oc = OperationContext.Current; //External Web service Call var response = await externalService.GetDataAsync(); return response.text; } 有没有一种好的方法可以将OperationContext传播到外部服务调用中,然后再次进入剩余的代码执行?

使用TcpListener进行异步/等待或开始/结束?

我已经开始构建一个tcp服务器,它将能够接受许多客户端,并同时从所有客户端接收新数据。 到目前为止,我使用IOCP用于tcp服务器,这非常简单和舒适,但这次我想使用Async / Await技术。 那是在C#5.0中发布的。 问题是,当我开始使用async / await编写服务器时,我发现在tcp中有多个用户服务器用例,async / await tech。 并且常规同步方法将起作用。 这是一个更具体的简单示例: class Server { private TcpListener _tcpListener; private List _clients; private bool IsStarted; public Server(int port) { _tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, port)); _clients = new List(); IsStarted = false; } public void Start() { IsStarted = true; _tcpListener.Start(); Task.Run(() => StartAcceptClientsAsync()); } […]

C#5和异步​​计时器

是否有一个新的Timer API允许我这样做? await timer.wait(500); 基本上,要睡眠X ms然后恢复执行其余function

可以/应该将任务包装在C#5.0中,等待TResult中的协变吗?

我非常喜欢使用C#5.0异步编程。 但是,有些地方更新旧代码以与TAP模型保持一致会给我带来问题。 这是其中之一 – 我不确定为什么Task在TResult中不协变,但在尝试更新协变接口以从同步模式转换为异步模式时,它会给我带来问题: 旧代码: public interface IInitializable // ** out generic modifier ** { /// /// Boolean to indicate if class is ready /// bool IsInitialized { get; } /// /// Calls for instance to be initialized using current parameters /// Driver initialization can be done in the default constructor if desired /// […]

等待和等待在C#5.0异步

任务或任务对象是等待的,因此我们可以在返回值为Task或Task 的对象上使用await键。 任务或任务是最常用的等待对象。 我们也可以定义我们自己的等待对象。对象应该具有以下资格。 它有一个GetAwaiter()方法(实例方法或扩展方法); 它的GetAwaiter()方法返回一个awaiter。 在下列情况下,对象是等待者: 它实现了INotifyCompletion或ICriticalNotifyCompletion接口; 它有一个IsCompleted,它有一个getter并返回一个布尔值; 它有一个GetResult()方法,它返回void或结果。 我的问题是为什么微软没有提供限制这些等待对象的界面? 目前实现等待对象的方法有点复杂。

TcpListener:如何在等待AcceptTcpClientAsync()时停止侦听?

我不知道如何在异步方法等待传入连接时正确关闭TcpListener。 我在SO上找到了这个代码,这里是代码: public class Server { private TcpListener _Server; private bool _Active; public Server() { _Server = new TcpListener(IPAddress.Any, 5555); } public async void StartListening() { _Active = true; _Server.Start(); await AcceptConnections(); } public void StopListening() { _Active = false; _Server.Stop(); } private async Task AcceptConnections() { while (_Active) { var client = await _Server.AcceptTcpClientAsync(); […]