.net-4.0

Equivalent of Task.FromResult() from .NET 4.5 in .NET 4.0

心不动则不痛 提交于 2019-12-06 12:07:38
问题 I cant find information about retargeting my code from .NET 4.5 to 4.0. I have to install this application on Windows XP. my code in .NET 4.5 public async Task <IXLWorksheet> ImportFile(string fileToImport) { ... return await Task.FromResult<IXLWorksheet>(Sheet1) } In .NET 4.0 method FromResult does not exist. Someone knows how it should looks in .NET 4.0?? 回答1: You're returning the awaited result of a task, which is constructed on a result. The solution is rather simple - drop the await :

Does anybody know of good resources for parallel programming patterns/testing using .NET 4.0 tasks?

柔情痞子 提交于 2019-12-06 12:04:04
I recently asked a developer to write a library using the new multithreading capabilities of .NET 4.0. He's done a great job, but I'm concerned that the Task logic is repeated throughout the code and not nicely encapsulated. I'm also concerned that this creates a problem when it comes to testing. Normally, I test by creating a seam in the code by creating an interface and a stub/mock object to run my tests against. I suppose this is possible using this way of doing things. It just seems that the production code logic would be very different than the test logic. Is the solution to make tests

Using x-www-form-urlencoded Content-Type in WCF

自古美人都是妖i 提交于 2019-12-06 12:03:20
I'm writing a .NET client for Menalto Gallery 3 , which uses a RESTful JSON API (see API docs ). I decided to use WCF for my client and it looks like it could greatly simplify my work, were it not for the fact that there's one method that requires a Content-Type of application/x-www-form-urlencoded instead of application/json . I have seen various hacks to send urlencoded data from WCF, for example by using a Stream parameter which enables me to send arbitrary data, but still requires an IClientMessageInspector to set the Content-Type: internal class GalleryClientMessageInspector :

What is the fastest way for checking a big proxy list on a specific web site?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 11:56:14
I have a big list of proxy servers (txt file , Format = ip:port in each line) and wrote the code below for checking them: public static void MyChecker() { string[] lines = File.ReadAllLines(txtProxyListPath.Text); List<string> list_lines = new List<string>(lines); List<string> list_lines_RemovedDup = new List<string>(); HashSet<string> HS = new HashSet<string>(); int Duplicate_Count = 0; int badProxy = 0; int CheckedCount = 0; foreach (string line in list_lines) { string[] line_char = line.Split(':'); string ip = line_char[0]; string port = line_char[1]; if (CanPing(ip)) { if (SoketConnect(ip,

How to change permissions of AppDomain?

北城以北 提交于 2019-12-06 11:47:17
问题 I use special method to create sandobx: internal static class Helper { public static AppDomain CreateSandbox() { Contract.Ensures(Contract.Result<AppDomain>() != null); var platform = Assembly.GetExecutingAssembly(); var name = platform.FullName + ": Sandbox " + Guid.NewGuid(); var setup = new AppDomainSetup { ApplicationBase = platform.Location }; var permissions = new PermissionSet(PermissionState.None); permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read |

How do I lock down a .net 4.0 WCF Data Service

北慕城南 提交于 2019-12-06 11:35:54
I've had a WCF Data service published for about 2 months. It's 100% been hacked already. I even noticed the service published on twitter! Luckily my site was under development and the user entity was only about 80 beta testers. Still this is a pretty big problem. With the power of E.F. Navigation properties anyone can easily write a script to download all my user data and my valuable domain data that no-one else has. I want to provide non-authenticated access and do things like: Limit what columns get exposed (e.g. a users emails) Limit number of requests possible per day (e.g. 10 per request

How do you give a C# Auto-Property a default value using a custom attribute?

ぃ、小莉子 提交于 2019-12-06 11:31:06
How do you give a C# Auto-Property a default value, using a custom attribute? This is the code I want to see: class Person { [MyDefault("William")] public string Name { get; set; } } I am aware that there is no built in method to initialize the default using an attribute - can I write my own custom class that uses my custom attributes to initialize the default? You could use a helper class like that: public class DefaultValueHelper { public static void InitializeDefaultValues<T>(T obj) { var properties = (from prop in obj.GetType().GetProperties() let attr = GetDefaultValueAttribute(prop)

Visual Studio 2010 keeps changing my winforms control

淺唱寂寞╮ 提交于 2019-12-06 11:23:33
I have an odd situation with a user control in VS 2010. The form designer keeps changing my entry and then telling me it does not exist! It compiles and runs the first time, and then if I change something unrelated, it gives me an error in the designer.cs file ( Cannot resolve symbol SomeEntry ). private SomeEntry someEntry; // ... this.someEntry = new **MyNameSpace**.SomeEntry(); if I remove the MyNameSpace. this.someEntry = new SomeEntry(); it works, until I make a change again. If I look at the class when the mouse is over the changed designer file, SomeEntry shows SomeEntry.SomeEntry()

DeploymentItem fails copying directories when there are multiple test projects per solution

一曲冷凌霜 提交于 2019-12-06 10:47:51
I have a number of test classes and methods that copy a particular directory like so: [TestClass, DeploymentItem("LanguageData", "LanguageData")] public class OcrTests { [TestMethod] public void Can_Capture_Field() { // some code that expects the LanguageData directory to be in the test results Out directory } // etc } [TestClass] public class OcrBuilderTests { [TestMethod, DeploymentItem("LanguageData", "LanguageData")] public void Can_Build_Specific_Ocr_Engine_Implementation() { // some more code that expects the LanguageData directory to be in the test results Out directory } // etc } Those

How do I output console to multiple streams at once in C#?

家住魔仙堡 提交于 2019-12-06 10:12:10
I have a program that takes the Console output and writes it to a logfile, however it no longer shows up in the console window. Is there a way to keep it in the window but write it to the log file as well? Update: appLogStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read); TextWriter logtxtWriter = Console.Out; logstrmWriter = new StreamWriter(appLogStream); if(!console) Console.SetOut(logstrmWriter); logstrmWriter.AutoFlush = true; Console.WriteLine("Started at " + DateTime.Now); console is a constant set in the class. It basically tells it whether it is using