Showing posts with label Studio. Show all posts
Showing posts with label Studio. Show all posts

Wednesday, April 29, 2015

Custom Action Results in ASP.NET 5 (VNEXT) (MVC6)

Before we start, you should be aware of this. This post is based on a the ASP.NET 5 version, Visual Studio 2015 CTP 6 pulls down, when it creates an ASP.NET 5 Project. Meaning it is a pre-release of ASP.NET 5. Things in ASP.NET 5 can change and outdate this post. It is highly unlikely, but it can happen.

Even thou I keep referring MVC 6, it is still a post regarding ASP.NET 5 or ASP.NET vNext, it's the same, and MVC 6 is a part of ASP.NET MVC 5.

Why would I write a custom ActionResult

Yes, good question. There is plenty supported in ASP.NET 5. But sometimes you ends up in a situation, where you need something special. When I developed Your Favorite Snippet Tool, I needed to transfer binary in a certain way, to provide best user experience. I created a custom ActionResult to handle it.

ActionResult in MVC 6 compared to earlier versions

ActionResults have changed a bit, since the prior versions of MVC. Yes, you still have to inherit from ActionResult and yes you still have to override a ExecuteResult method, when making custom ActionResults.

The most noticeable difference, is that in MVC 6 ExecuteResult have another signature compared to prior version, and there is also a ExecuteResultAsync added.

ExecuteResult for ASP.NET MVC 5 and Prior


public abstract void ExecuteResult(ControllerContext context)

ExecuteResult for ASP.NET MVC 6


public virtual Task ExecuteResultAsync(ActionContext context)
public virtual void ExecuteResult(ActionContext context)

Two thing you might notice, the methods in MVC 6 are using virtual methods, and ActionContext instead of ControllerContext. There is nothing much to say about the contexts, they are very similar. By using virtual method there is no override constrains. It means that you can override either ExecuteResultAsync, ExecuteResult or both, but are not forced to.

Which to override, ExecuteResultAsync or ExecuteResult

It depends, but preferly ExecuteResultAsync, because it is the one which is called. Inside ActionResult, which you have to inherit from, following logic is happening:

 public abstract class ActionResult : IActionResult  
   {  
     public virtual Task ExecuteResultAsync(ActionContext context)  
     {  
       ExecuteResult(context);  
       return Task.FromResult(true);  
     }  
     public virtual void ExecuteResult(ActionContext context)  
     {  
     }  
   }  

So you see, ExecuteResultAsync is still called even thou you just override ExecuteResult. Plus, it will not make sense, to enforce overrides of the 2 methods.


Show me some code

I have made a string writer result, not the most exciting example, but it proves the point.

The custom ActionResult

 using Microsoft.AspNet.Mvc;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace CustomActionResults  
 {  
   internal class StringWriterResult : ActionResult  
   {  
     private byte[] _stringAsByteArray;  
     public StringWriterResult(string stringToWrite)  
     {  
       _stringAsByteArray = Encoding.ASCII.GetBytes(stringToWrite);  
     }  
     public override Task ExecuteResultAsync(ActionContext context)  
     {  
       context.HttpContext.Response.StatusCode = 200;  
       return context.HttpContext.Response.Body.WriteAsync(_stringAsByteArray, 0, _stringAsByteArray.Length);  
     }  
   }  
 }  


String write ActionResult in action:

 using CustomActionResults;  
 using Microsoft.AspNet.Mvc;  
 namespace ActionHandlers.Controllers  
 {  
   public class ExampleController : Controller  
   {  
     public IActionResult Get()  
     {  
       return new StringWriterResult("Hello World!");  
     }  
   }  
 }  

Insert the code in some ASP.NET 5 project, and you should get Hello World!, when hitting ~/Example/Get.


Monday, April 27, 2015

Introducing Your Favorite Snippet Tool

Snippets in Visual Studio and SQL Server Management Studio are a great help, and tremendous time savers. Unfortunately the con about VS and SSMS snippets are: They are tedious to create. I have a feeling, that makes it less appealing to use custom snippets, because it includes working with XML to create them, and VS or SSMS offers no help.

History

Well, a couple of days ago, I decided to make a snippet of some SQL, which I had realized, I had to write regularly in the future. I was pretty tired of writing this SQL, and then I remembered: To create a snippet you have to setup an XML document. Then I got really tired. In hope of an easy solution, I did search the web for an online snippet creator. All i did find was tools, which had to be downloaded. No offence, these downloadables are properly mighty fine, but nowadays I think a tool like a snippet creator should be online. Easy to reach, and not another downloaded tool to soil your computer.

Priorities can be strange some times, and I decided, that I would rather write an online tool myself, which could make snippets, than do another handmade snippet.

So here after a small coding marathon, I'll present to you:

YOUR FAVORITE SNIPPET TOOL(that is the name)



Enjoy!

FAQ

Q: Why is the link www.snippettool.net and not www.yourfavoritesnippettool.com when the tool name is Your Favorite Snippet Tool?
A: For your convenience. It is much easier, to remember www.snippettool.net and type it right. 

Q: The first release is version 0.8.0, is it production ready?
A: Yes. There is some extended features for VB, which will be there in a later release. Further I have some ideas for UI improvements. Also, until I have had some more feedback, it wouldn't be right to release it in version 1.0.0

Q: VSI packages are supported, what about VSIX packages?
A: VSIX does not by default, support snippets. Hacks must be applied to make VSIX work with snippets.

Q:The Visual Studio Content Installer does not install the VSI package to Visual Studio 2xxx, why?
A: That is because, Visual Studio Content Installer is a strange piece of software. Specially, if you have more versions of Visual Studio on your machine.

Q: I found a bug, what to do?
A: I will appreciate, if you would write to me about it. Contact information is at the button of the page. 


Saturday, April 5, 2014

SQL Server 2014: SQL Server Data Tools(SSDT) and Visual Studio 2013 Challenges in Training Kit 70-463 and in general

Level: 2 where 1 is noob and 5 is totally awesome
System: SQL Server 2014

Notice: I'm using the SQL Server 2014 Developer edition, so if you are using a different edition, things might be different.

Some History


I'm currently studying for the exam 70-463 Implementing a Data Warehouse with Microsoft SQL Server 2012, and I'm using the 70-463 training kit for the exam. I have decided to use SQL Server 2014 for the practical training(regarding to SSDT, I now know it is the best to use SQL Server 2014). It has just been released and regarding to exam 70-463 there is no change. People who took the exam for SQL Server 2012 is also certified for SQL Server 2014. For more see FAQ here at http://www.microsoft.com/learning/en-us/sql-certification.aspx.

When using the training kit, you sooner or later realize, it has become a bit outdated. Which is quite natural, because it was published 25. Dec. 2012, and lot have happened since. There has been new releases and updates of Visual Studio, and we even have a new version of SQL Server. Which, all in all is nice.

The first sign of you might be challenged when using the training kit with SQL Server 2014, is in the beginning. The exam kit provides a list of features you have to install, and last on this list is SQL Server Data Tools, also known as SSDT. As you might discover, this feature is not shipped with SQL Server 2014, and it is the most important component for SSIS and the exam 70-463.

SSDT is a Visual Studio plugin, for developing SQL Server Integration Service(SSIS) packages. SSDT is a replacement from Business Intelligence Development Studio, also known as BIDS.Among things, SSDT delivers SSIS packages templates to Visual Studio. SSIS 2012 depends on Visual Studio 2010, Like SSIS 2014 does, but SQL Server 2012 installs the Visual Studio 2010 IDE if none is present. SQL Server 2014 has failed to do so, everytime I have tried to install it. If there is no SSDT in the SQL Server 2014 installation, it might make sense. Also it is okay, because I prefers to use Visual Studio 2013, and if you are a developer like me, I guess you prefer the same.

And Now the Tricky Part


I discovered Microsoft had developed SSDT (SSDT-BI which it is called now) for Visual Studio 2012, it the most easy package to find, and I thought it was the latest. I installed it, and was hit hard in chapter 3 of the training kit 70-463. Because I has installed SSDT-BI 2012 for Visual Studio 2012.

The problem was I have created a SSIS package with the SQL Server Import and Export tool, the version which comes with SQL Server 2014. It stores it SSIS Packages in the SSIS Package format version 8, while the SSDT 2012 stores in the SSIS Package format version 6. The SSIS Package format is apparently not forward compatible. 

The pain I experienced, in chapter 3 of the training kit, was I had to create a package with the SQL Server Import and Export tool (version 2014 in my case) and add it to a SSIS Project created with SSDT 2012. The because the version numbers didn't match, result was this:



Fortunately I succeeded to find SSDT-BI 2014 well hidden at 

It saves SSIS Packages in SSIS Package format version 8, like the rest of the SQL Server 2014 Suite, plus it works with Visual Studio 2013. I'll expect it will work it with the rest of the training kit 70-463, or else I'll provide a solution on this blog. As bonus info, you have to select "Perform a new installation of SQL Server 2014", or you will get some kind of architecture error. Despite the option name, it only installs SSTD-BI 2014.


Enjoy