<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1184</guid>
      <link>https://shazwazza.com/post/model-binding-with-fromservices-in-aspnet-5/</link>
      <category>ASP.Net</category>
      <title>Model binding with FromServices in ASP.Net 5</title>
      <description>&lt;p&gt;Here’s a new nifty feature I found in ASP.Net 5 – you can construct your model during model binding with IoC without any additional work. This is available on the dev branch on GitHub and is based on something called &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Core/ModelBinders/ServicesModelBinder.cs" target="_blank"&gt;&lt;em&gt;ServicesModelBinder&lt;/em&gt;&lt;/a&gt;. This is actually pretty cool because it means that you can have a model wired up with all of it’s dependencies based on IoC and then bound to your controller action’s parameters.&lt;/p&gt; &lt;h2&gt;FromServices attribute&lt;/h2&gt; &lt;p&gt;There’s a new attribute called &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/FromServicesAttribute.cs" target="_blank"&gt;&lt;em&gt;FromServices&lt;/em&gt;&lt;/a&gt; which is what you use to enable this functionality. For example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; async Task&amp;lt;ActionResult&amp;gt; GetProduct(
    [FromServices]ProductModel product)
{

}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;What this attribute does is tell MVC to bind the model using &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/IServiceActivatorBinderMetadata.cs" target="_blank"&gt;&lt;em&gt;IServiceActivatorBinderMetadata&lt;/em&gt;&lt;/a&gt; which is what the &lt;em&gt;FromServices&lt;/em&gt; attribute implements. This in turn tells MVC to lookup the model binder that is aware of &lt;em&gt;IServiceActivatorBinderMetadata &lt;/em&gt;which happens to be the &lt;em&gt;ServicesModelBinder.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;ServicesModelBinder&lt;/h2&gt;
&lt;p&gt;This model binder is pretty simple, it’s just going to resolve the model type from your IoC container. That could be pretty useful if you need to build up your model properties based on other services. I think some people might argue that this isn’t great practice because this is putting the binding logic in to the model itself instead of using a separate class to perform the binding logic. I suppose it’s up to the individual developer as to what their preference is. You can of course still create your own &lt;em&gt;&lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinder.cs" target="_blank"&gt;IModelBinder&lt;/a&gt;&lt;/em&gt; and use the &lt;em&gt;&lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs" target="_blank"&gt;ModelBinderAttribute&lt;/a&gt;&lt;/em&gt; to keep the model binding logic in the binder itself.&lt;/p&gt;
&lt;h2&gt;Incoming route values&lt;/h2&gt;
&lt;p&gt;Since the model is being created from IoC, how do you get the current route values to build up your model? To do that you’d put a constructor dependency on &lt;em&gt;IContextAccessor&amp;lt;ActionContext&amp;gt;. &lt;/em&gt;This will give you all of the current route values, &lt;em&gt;HttpContext&lt;/em&gt;, etc… basically everything you’d need to pull the data out of the current request to build your model. &lt;/p&gt;
&lt;h2&gt;Example&lt;/h2&gt;
&lt;p&gt;Given the above GetProduct example, the &lt;em&gt;ProductModel&lt;/em&gt; class could look like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ProductModel
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ProductModel(IContextAccessor&amp;lt;ActionContext&amp;gt; action, IProductService prodService)
    {
        &lt;span class="rem"&gt;//TODO: Do some error checking...&lt;/span&gt;
        var productId = action.Value.RouteData.Values[&lt;span class="str"&gt;"product"&lt;/span&gt;];
        Value = prodService.Get(productId);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; IProduct Value { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This is pretty simple – the &lt;em&gt;IProduct&lt;/em&gt; is looked up from the &lt;em&gt;IProductService&lt;/em&gt; based on the incoming ‘product’ route value. Then in the controller you could just do: &lt;em&gt;product.Value&lt;/em&gt; to get the value for &lt;em&gt;IProduct&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;You then need to ensure that both IProductService and ProductModel are registered as services in your container and it’s really important that the &lt;em&gt;ProductModel&lt;/em&gt; is registered as a Transient object &lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1293</guid>
      <link>https://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/</link>
      <category>ASP.Net</category>
      <title>ASP.Net 5 - Re-learning a few things (part 1)</title>
      <description>&lt;p&gt;&lt;a href="http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview" target="_blank"&gt;ASP.Net 5&lt;/a&gt; (aka vNext) is now in Beta and the &lt;a href="http://www.visualstudio.com/en-us/news/vs2015-preview-vs" target="_blank"&gt;Visual Studio 2015 preview&lt;/a&gt; is now out! So, &lt;a href="http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview" target="_blank"&gt;what is this new ASP.Net&lt;/a&gt;? The 2 biggest features is that it’s &lt;a href="https://github.com/aspnet" target="_blank"&gt;totally open source&lt;/a&gt; and it will support a cross platform runtime = great!! But it’s worth knowing that this is more or less a rebuild of ASP.Net and there’s quite a few things that we’ve become accustomed to that will now be totally different.&lt;/p&gt; &lt;h2&gt;Configuration files&lt;/h2&gt; &lt;p&gt;You know all of that junk in the web.config and perhaps in a bunch of other *.config files? Well that is gone! I think this is wonderful news because creating those configuration sections was a huge pain. Even better news is how easy creating custom configuration inputs will be in&amp;nbsp; ASP.Net 5 using the new &lt;u&gt;&lt;em&gt;&lt;a href="https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Framework.ConfigurationModel/IConfiguration.cs" target="_blank"&gt;IConfiguration&lt;/a&gt; &lt;/em&gt;&lt;/u&gt;and &lt;em&gt;ConfigurationModel &lt;/em&gt;sources&lt;em&gt;. &lt;/em&gt;OOTB Microsoft is releasing support for JSON, XML and INI files for configuration but of course you can easily create your own.&amp;nbsp; The repository for the configuration bits is &lt;a href="https://github.com/aspnet/Configuration" target="_blank"&gt;here&lt;/a&gt; and a nice tutorial can be found &lt;a href="http://whereslou.com/2014/05/23/asp-net-vnext-moving-parts-iconfiguration/" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;So what about configuration transforms?? That is also a thing of the past. In ASP.Net 5, “configuration” will mostly be done with code found in your Startup.cs file, anything that you want to enable in your application is done in this class. Any package that is installed in your app, you will need to opt-in to use it in your Startup.cs file. In some cases however, a configuration file might need to be updated/transformed… this could be due to an upgrade of a package. The good news is that the configuration sources in ASP.Net 5 are writable (&lt;em&gt;&lt;a href="https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Framework.ConfigurationModel/Sources/IConfigurationSource.cs#L15" target="_blank"&gt;IConfigurationSource&lt;/a&gt;&lt;/em&gt;) which means during startup or first access to your config, you could detect what needs to be updated to support your new version, make the updates in code and commit (&lt;em&gt;&lt;a href="https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Framework.ConfigurationModel/Sources/ICommitableConfigurationSource.cs" target="_blank"&gt;ICommitableConfigurationSource&lt;/a&gt;&lt;/em&gt;) the changes.&lt;/p&gt; &lt;p&gt;Wait… isn’t that going to restart the app domain??&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;em&gt;&lt;font size="2"&gt;NOTE: If you are using IIS, there can still be a web.config which can be used to configure IIS settings under the system.webserver section.&lt;/font&gt;&lt;/em&gt;&lt;/p&gt; &lt;h2&gt;AppDomain restarts&lt;/h2&gt; &lt;p&gt;This is something that we’ve all become familiar with… you want to restart your app, just bump/touch the web.config and your app domain is restarted. This is something we’ll all have to un-learn. In ASP.Net 5 auto app domain restarts don’t happen. First, there is no web.config (or global.asax for that matter) so there are no files to bump/touch. Next, a big reason why auto app domain restarts can’t occur is because ASP.Net 5 will be able to be run on various different web servers which don’t really know about what it means to restart an app domain. For example if you’ve been playing around with vNext before you had a chance to use VS 2015, you might be familiar with the command line &lt;em&gt;“k web” &lt;/em&gt;&lt;a href="https://github.com/aspnet/Home#running-the-samples" target="_blank"&gt;(see docs for details)&lt;/a&gt;. This command will start up a simple web server: &lt;em&gt;Microsoft.AspNet.Server.WebListener&lt;/em&gt; which will serve web requests. In order for app domain restarts to occur, it would need to know how to restart itself after it’s been shutdown which isn’t exactly possible with a simple command line process. Instead if you made any changes to your code and wanted to restart your app, you’d kill the process (ctrl + c) and just call &lt;em&gt;k web&lt;/em&gt; again. Another thing to be aware of is that when you kill a process like this, your app domain does not gracefully shutdown/unwind, it’s simply terminated.&lt;/p&gt; &lt;p&gt;But not to worry! If you have a web app that requires restarting (i.e. maybe it installs plugins, etc…) and needs to gracefully unwind, it’s still possible and actually much more pleasant since you’ll be in full control of how/when it happens. In order for this to work you’ll need to be running a web server that knows how to start itself back up again - like IIS! The way to gracefully shutdown your app domain is by using: &lt;a href="https://github.com/aspnet/KRuntime/blob/dev/src/Microsoft.Framework.Runtime.Interfaces/IApplicationShutdown.cs" target="_blank"&gt;&lt;em&gt;IApplicationShutdown&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;when you want to gracefully shutdown your app. You could even use that in combination with an &lt;a href="https://github.com/aspnet/KRuntime/blob/dev/src/Microsoft.Framework.Runtime.Interfaces/IFileWatcher.cs" target="_blank"&gt;&lt;em&gt;IFileWatcher&lt;/em&gt;&lt;/a&gt; and your own configuration files&lt;em&gt;&amp;nbsp;&lt;/em&gt;… if you really wanted to mimic an app domain restart by bumping/touching a file.&lt;/p&gt; &lt;h2&gt;Deployments, bin folder and App_Code&lt;/h2&gt; &lt;p&gt;Speaking of app domain restarts, how will ASP.Net 5 work when I put a new assembly in the /bin folder or add a new class to App_Code?? These are a couple more things that need to be un-learned. There really isn’t a /bin folder anymore (well, there is but it only contains one very special assembly if you are running IIS) and there isn’t any App_Code folder.&amp;nbsp; So where does all of this go? &lt;/p&gt; &lt;p&gt;When you publish a web project in VS 2015 (which uses &lt;em&gt;kpm pack) &lt;/em&gt;you end up with a very different looking deployment structure. There’s two folder: &lt;em&gt;approot&lt;/em&gt; and &lt;em&gt;wwwroot&lt;/em&gt;. &lt;/p&gt; &lt;p&gt;&lt;em&gt;wwwroot &lt;/em&gt;– is the content of your website, nothing more. Even things like configuration files don’t exist here, it’s just content that your webserver can serve. &lt;/p&gt; &lt;p&gt;&lt;em&gt;approot&lt;/em&gt; – this is the brains of your website. It includes all of the binaries, config files, source code, etc… that is used to run your website.&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/imranbaloch/aspnet-vnext-folder-structure" target="_blank"&gt;Here’s a blog post that describes the deployed file structure&lt;/a&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Did you say source code?? Yup! By default kpm pack will deploy your site with all of it’s packages and the source code for all of your projects. The &lt;a href="http://msdn.microsoft.com/en-au/vstudio/roslyn.aspx" target="_blank"&gt;Roslyn compiler&lt;/a&gt; will take care of everything for you when your site needs to start serving requests. You can of course opt-out of this and have your site deployed as a compiled package.&lt;/p&gt; &lt;p&gt;Did you say Package?? Yup, as in Nuget package! Instead of a /bin folder full of assemblies, all of your dependencies will actually be Nuget references and stored in &lt;em&gt;approot/packages &lt;/em&gt;and if you choose to deploy your website without source, it will be compiled into a Nuget package and deployed in the packages folder as well.&lt;/p&gt; &lt;h2&gt;More to come….&lt;/h2&gt; &lt;p&gt;So there’s a a few of the things that are pretty different in ASP.Net 5, there’s still more to come and hopefully I’ll find some time to write them all up!&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
  </channel>
</rss>