Sunday, March 25, 2018

ASP.Net core 2 caching

With classic Asp.Net you used the HtppContext in the System.Web namespace for caching. Since Asp.Net Core was created to be cross platform caching is done differently.

I am using Visual Studio for Mac to do this but it will work the same in Visual Studio 2017 (for windows).

Lets create a new Asp.Net core application





Once the app is created go to the startup.cs class. and change the ConfigureServices method to this

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddMemoryCache();
        }
  


Now to use this we need to get access to the IMemoryCache in the controllers constructor.  In the demo I will use the about page in the project created

        IMemoryCache caching= null;


        public HomeController(IMemoryCache cache)
        {
            caching = cache;
        }

To use it. I am just going to cache the DateTime

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";
            DateTime currentTime;
            if (!caching.TryGetValue<DateTime>("currentTime", out currentTime))
            {
                currentTime = DateTime.Now;
                caching.Set<DateTime>("currentTime", currentTime, DateTimeOffset.Now.AddMinutes(10));
            }
        
                
            ViewBag.Time = currentTime;

            return View();
        }

To cache something use the Set method.  I would recommend using the version that allow you to specify the type of object.

When setting the cache you pass in the key, what you want cached and optionally how long you want it cached.  I set it to 10 minutes in this example.

Getting items from the cache you use TryGetValue it will return true if the value was found and it passes it out in an out parameter.

If you need to remove something use the cache Remove method where you pass in the key of the object you want to remove.

To show the item is cached I changed the About.cshtml to show the current date time and the cached value

@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>

<p>Use this area to provide additional information.</p>
@DateTime.Now
<br/>
@ViewBag.Time





Please be sure to check out my course on Packt about asp.net core to learn more about asp.net core










Mac for windows users

I recently bought a Mac to start developing iPhone apps

Here are some of the things I found that are different in Mac than a Windows PC

Copy is Command + c
Paste is Command + v
Undo is Command + z

You can use Command + Shift + 4 to take a screen shot like with the snippet tool in windows.  The images saves to the desktop

The delete key works like backspace on the mac.  Command + delete acts like the windows delete key


Make sure in Finder that the c drive is selected if you want finder to work more like windows explorer



Orlando Code Camp 2018

At the 2018 Orlando Code Camp I did a presentation on using Prism with Xamarin Forms.

Here is a link to the sample code and slides

https://github.com/vb2ae/orlandocodecampprism


The slides are in the GitHub repository.  They are in both KeyNote and PowerPoint format

Saturday, January 6, 2018

VSTS build failed

Recently I was moving a TFS XAML build to one the new style builds.   The XAML based build worked fine but when I tried the new format the build failed with this error

Unable to process command '##vso[task.logdetail id=faad7dd0-d179-4a32-ba2a-cecf68fd7d28;parentid=;

I tried turning off logging in the build and it did not help.  This issue was one of the folder names for the projects.  It contained [ ] in the name which the new Build format does not support.  Removing the [ ]  from the folder name allowed the project to build.  I also had to edit the solution file and update the project folder to the new name

Hope this helps

Sunday, November 5, 2017

Using .net standard 2.0 with a UWP app

With the fall 2017 creators update for windows 10 you can now use .Net standard 2.0 .

First step to use .net Standard 2.0 is to create a new UWP and set the min and max version of UWP version 16299 or higher.





Make sure you update the Nuget packages after you create the app






You need version 6.0.1  Microsoft.NETCore.UniversalWindowsPlatform if you need to use a .net standard 2.0 library.


Tuesday, September 5, 2017

Using .net Standard 2.0 with Xamarin Forms

Standard Libraries are replacing Portable Class Libraries.  Here is how to start using them in Xamarin Forms projects today.

To Start with create a new Xamarin forms app that uses a Portable Class Library



Since UWP will nor support .net Standard Libraries for a few months I would press cancel if you get the dialog asking what version of UWP you want to support.

Right click on the solution and select add new project.  Add a class library (.net standard)



Now drag the files App.Xaml and MainPage.Xaml from the PCL to the new class library.  You can also delete the file Class1.cs in the standard library we  created.

Open the properties for the Standard class library and change the default namespace to be the same as the portable class libraries



Time to add a NuGet package to the standard class library.  Microsoft.NetCore.Portability.Compatibility.  This will help prevent some build errors



Remove the portable class library from the solution.  Right click on the solution and select mange NuGet packages for the solution.  Select Updates and find Xamarin Forms.  Select the latest pre release and make sure the standard library is checked.  The latest prerelease supports standard libraries




Right click on the standard class library and select edit csproj.  Remove the following lines from the file.


  <ItemGroup>
    <EmbeddedResource Include="App.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Include="MainPage.xaml">
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
  </ItemGroup>

Save the file and add a reference in the android and ios project to the new standard library

Now you can run it

Here is what it looks like in the android emulator




In the next post in this series we will call a web service and save the json we get back to a file on the device.

Sunday, September 3, 2017

Client NoSql DB

I have used the NoSql database Lex.DB in some of my mobile apps in the past.  The DB was actually very handy and it worked on Xamarin.iOS, Xamarin.Android, Windows phone 8+, UWP,  PCL, Silverlight, and WinRT+.  I am in the process of converting the project to a standard library 2.0 that you can use with net core, Xamarin, UWP when the fall creators update comes out in Oct 2017, and .net 4.6.1

My hope is to get the Database so it can be used in windows, linux, mac, xamarin, or UWP apps.  This database is meant to be used with an app not as a server based NoSql database.

The code is currently in GitHub please report any bugs or feature requests there. Send me an email if you want to contribute to the project

https://github.com/vb2ae/ClientNoSqlDB