Pages

Wednesday, July 15, 2015

scriptcs c# scripts

http://scriptcs.net/




Unleash your C# from Visual Studio.

What is it?

scriptcs makes it easy to write and execute C# with a simple text editor.
While Visual Studio, and other IDEs, are powerful tools, they can sometimes hinder productivity more than they promote it. You don’t always need, or want, the overhead of a creating a new solution or project. Sometimes you want to just type away in your favorite text editor.
scriptcs frees you from Visual Studio, without sacrificing the advantages of a strongly-typed language.
  • Write C# in your favorite text editor.
  • Use NuGet to manage your dependencies.
  • The relaxed C# scripting syntax means you can write and execute an application with only one line of code.
  • Script Packs allow you to bootstrap the environment for new scripts, further reduces the amount of code necessary to take advantage of your favorite C# frameworks.

Getting scriptcs

Releases and nightly builds should be installed using Chocolatey. Information on installing Chocolatey is available at their website.

Installing scriptcs

Once Chocolatey has been installed, you can install the latest stable version of scriptcs from your command prompt:
cinst scriptcs
Chocolatey will install scriptcs to %APPDATA%\scriptcs\ and update your PATH accordingly.
Note: You may need to restart your command prompt after the installation completes.

Staying up-to-date

With Chocolatey, keeping scriptcs updated is just as easy:
cup scriptcs

Nightly builds

Getting Started

Using the REPL

The scriptcs REPL can be started by running scriptcs without any parameters. The REPL allows you to execute C# statements directly from your command prompt.
C:\>; scriptcs
scriptcs (ctrl-c or blank to exit)

 var message = "Hello, world!";
 Console.WriteLine(message);

Writing a script

  • In an empty directory, create a new file named app.csx:
  • scriptcs app.cs

Friday, May 15, 2015

EF6 code first

install-package EntityFramework
or
update-package EntityFramework




Install EF Power Tools  to visualize code first

Context class


namespace EF6Tutorial {
    public class BooksContext:DbContext {
        public BooksContext()
            : base("BooksConnection") {

        }
        public DbSet Titles {get;set;}</div> <div>         public DbSet<genre> Genres {get;set;}</div> <div>         public DbSet<author> Authors {</div> <div>             get;</div> <div>             set;</div> <div>         }</div> <div>         public DbSet<titlemetadata> TitleMetadatas {</div> <div>             get;</div> <div>             set;</div> <div>         }</div> <div> <br></div> <div> <br></div> <div>         protected override void OnModelCreating(DbModelBuilder modelBuilder) {</div> <div> <br></div> <div>             modelBuilder</div> <div>      .Entity<author>()</div> <div>      .Property(t => t.AuthorName).HasMaxLength(500).HasColumnAnnotation( IndexAnnotation.AnnotationName, </div> <div>         new IndexAnnotation(</div> <div>             new IndexAttribute("IX_AuthorName", 1)));</div> <div> <br></div> <div>             base.OnModelCreating(modelBuilder);</div> <div>         }</div> <div>      public static void Initialise(){</div> <div> <br></div> <div>          Database.SetInitializer(</div> <div>              new MigrateDatabaseToLatestVersion<BooksContext,Configuration>()</div> <div>              </div> <div>              );</div> <div>      </div> <div>      }  </div> <div> <br></div> <div>     }</div> </div> <div> <br></div> <div> ================</div> <div> <b style="background-color: rgb(255, 255, 0);">PM> enable-migrations </b></div> <div> <br></div> <div> <div>   internal class Configuration : DbMigrationsConfiguration<EF6Tutorial.BooksContext></div> <div>     {</div> <div>         public Configuration()</div> <div>         {</div> <div>           </div> <div>             AutomaticMigrationsEnabled = true;</div> <div>             AutomaticMigrationDataLossAllowed=true;</div> <div>             ContextKey = "EF6Tutorial.BooksContext";</div> <div>         }</div> <div> <br></div> <div>         protected override void Seed(EF6Tutorial.BooksContext context)</div> <div>         {</div> <div>             //  This method will be called after migrating to the latest version.</div> <div> <br></div> <div>             //  You can use the DbSet<t>.AddOrUpdate() helper extension method </div> <div>             //  to avoid creating duplicate seed data. E.g.</div> <div>             //</div> <div>             //    context.People.AddOrUpdate(</div> <div>             //      p => p.FullName,</div> <div>             //      new Person { FullName = "Andrew Peters" },</div> <div>             //      new Person { FullName = "Brice Lambson" },</div> <div>             //      new Person { FullName = "Rowan Miller" }</div> <div>             //    );</div> <div>             //</div> <div>         }</div> <div>     }</div> </div> </div>

Tuesday, May 12, 2015

async , await .NET 4.5


you still can use Task.Wait method


 The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.
The asynchronous method in which await is used must be modified by the async keyword. Such a method, defined by using the async modifier, and usually containing one or more await expressions, is referred to as an async method.


private async Task SumPageSizesAsync()
{
    // To use the HttpClient type in desktop apps, you must include a using directive and add a
    // reference for the System.Net.Http namespace.
    HttpClient client = new HttpClient();
    // . . .
    Task getContentsTask = client.GetByteArrayAsync(url);
    byte[] urlContents = await getContentsTask;

    // Equivalently, now that you see how it works, you can write the same thing in a single line.
    //byte[] urlContents = await client.GetByteArrayAsync(url);
    // . . .
}

Tuesday, December 2, 2014

NuGet commands


  • PM> Get-Package Entityframework -ListAvailable
  • PM> Update-Package Entityframework
  • PM> Update-Package reinstall Entityframework


async await .NET 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Workshop.Samples {
    using CL=System.Net.WebClient;
    public class TaskSample:SampleBase {
             
        public override string Name {
            get {
                return "Asynch calls";
            }
        }

        public override void Run() {

            DownLoadAsynchSample();

           // DownLoadSample();
           
            //AsyncCall();
            //RunParallelTasksWithWait();
            // RunParallelTasks();

        }

        private void DownLoadSample() {

            CL client=new CL();
           

            var st=   client.DownloadString("http://www.sublimetext.com/support");
            Console.WriteLine("Download data: {0} more",st.Substring(0,200));
            Console.WriteLine("-------------------------");
          

        }
        private async void DownLoadAsynchSample() {
           // var url="http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%202.0.2.dmg";
            var url="http://www.sublimetext.com/support";

            CL webClient=new CL();
            Console.WriteLine("Before DownloadStringTaskAsync called");
          var result=   await webClient.DownloadStringTaskAsync(url);

            Console.WriteLine("After DownloadStringTaskAsync called");
           
            Console.WriteLine("Before Result called");

           
            Console.WriteLine("After Result called");


            Console.WriteLine("Download data: {0} more",result.Substring(0,100));

        }
        private async void AsyncCall() {
            Console.WriteLine("Do Async call");
          await  LongProcessAsync(5,"From asyncCall A");
          await LongProcessAsync(5,"From asyncCall B");
          await LongProcessAsync(5,"From asyncCall C");
           LongProcess(4,"Independent call");

            // LongProcessAsync(2,"From asyncCall B");
        }
        private void RunParallelTasksWithWait() {
            var tasks=new List(){
               new Task(() => {
                LongProcess(2,"Process A");
            }),
                 new Task(() => {
                LongProcess(3,"Process B");
            }),
                    new Task(() => {
                LongProcess(4,"Process C");
            }),
            };


            foreach(var task in tasks) {

                task.Start();
                task.Wait();
            }
        }

        private void RunParallelTasks() {
            var tasks=new List(){
               new Task(() => {
                LongProcess(2,"Process A");
            }),
                 new Task(() => {
                LongProcess(3,"Process B");
            }),
                    new Task(() => {
                LongProcess(4,"Process C");
            }),
            };


            foreach(var task in tasks) {

                task.Start();
            }
        }

        public void LongProcess(int seconds,string processName) {
            Console.WriteLine("Starting {0}",processName);
            Thread.Sleep(1000*seconds);
            Console.WriteLine("End {0}",processName);

        }

        public async Task LongProcessAsync(int seconds,string processName) {

            await Task.Run(() => {
                Console.WriteLine("Starting {0}",processName);
                Thread.Sleep(1000*seconds);
                Console.WriteLine("End {0}",processName);
            });



        }
    }
}