Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, August 20, 2017

Getting Started with Microsoft Azure Functions using Visual Studio 2017

Introduction:
Azure Functions is a solution for easily running small pieces of code in the cloud. We can create, execute, and test our custom logic function without creating a VM or web applications and also without needing to install any software or infrastructure to run the function.
In this article, you will learn about create, test, debug and deploy azure functions using Visual Studio 2017. Azure Functions tools are available as part of the Azure development workload in Visual Studio 2017 version 15.3, or a later version.
Prerequisites:
  1. Download and Install Visual 2017 version 15.3 .
  2. If you already Installed Visual Studio 2017, Update Visual Studio from Extensions and updates
Open Visual Studio 2017 > Tools > Extensions and Updates > Select product Updates under the Products category and select Visual studio update 15.3 preview 7.0 version and click on update
Step 1: Create Azure Functions Project in Visual Studio 2017
Open Visual Studio 2017 Preview 15.3 > Select File >click on New Project (Ctrl +Shift +N) > Select on Visual C# >Click on Cloud > select Azure Functions, type a Name for your project, and click on OK

Step 2: Create New Function
Azure Functions Solution will generate like below with host.json and locl.settings.json file .
In Solution Explorer, right-click on your Azure function project and select Add > New Item. Select Azure Function and click Add.










Select HttpTrigger as Azure Function type, select Functions for Access Rights, and click Create. The function created is accessed by an HTTP request from any client.
After click on Ok, will generates a new class with a Static Run method, that is attributed with [FunctionName] attribute. The [FunctionName] attribute indicates that the method is the entry for an Azure Function

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace DevEnv_AzureFunctionsDemo
{
   public static class DemoFunctions
   {
       [FunctionName("DemoFunctions")]
       public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
       {
           log.Info("C# HTTP trigger function processed a request.");

           // parse query parameter
           string name = req.GetQueryNameValuePairs()
               .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
               .Value;

           // Get request body
           dynamic data = await req.Content.ReadAsAsync<object>();

           // Set name to query string or body data
           name = name ?? data?.name;

           return name == null
               ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
               : req.CreateResponse(HttpStatusCode.OK, "Hello " + name + "Welcome C# Corner ");
       }
   }
}

Step 3: Azure Functions Test:
You can Press F5 for run, test and debug your azure function, if prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. You may also need to enable a firewall exception so that the tools can handle HTTP requests.
Copy the Azure function local deploy url on runtime output window
Append the name parameter to the query string. Use an actual name for the <Enter a name here> placeholder.
https://<Local Host url> /api/<Your Function Name>? name=<Enter a name here>
Paste the URL into your browser, and you should get a response similar to the following.
If you are Function locally working as expected means, you can initiate to publish the project to Azure.
Step 4: Publish Project to Azure:

  • If you don't have an Azure subscription, create a free account before start deploy azure function.
  • In Solution Explorer, right-click the project and select Publish. Choose Create New and then click on Publish.
  • If you haven't already connected Visual Studio to your Azure account, click Add an Account and Provide the login details and connect
  • In the Create App Service dialog, provide Hosting settings as specified below
Click Create to create a function app in Azure with these settings. After the provisioning is complete, make a note of the Site URL value, which is the address of your function app in Azure.
Copy the base URL of the function app from the Publish profile page. Replace the localhost:port of the URL you used when testing the function locally with the new base URL. As before, make sure to append the query string with &name=<yourname> to this URL and execute the request.
Download Source Code :

Related Article:
Summary:
In this article, you learned about Azure Functions and how to create, test, publish Azure Functions using visual studio 2017.
If you have any questions/ feedback/ issues, please write in the comment box.

Friday, July 28, 2017

Getting Started with the Custom Project Template using Visual Studio 2017

Introduction:
Visual Studio provide the many inbuilt templates for create a project, In this article we can create a new project template as per your client or product requirement and use the your project template on multiple developer. Visual Studio provides wizards to help you generate templates from projects that are open in the integrated development environment (IDE).
Project template will allow a developer to create new projects based on your exported project. The developer will be able to utilize your template for the new project dialog box
Create New Project:
Let start with creating a new Xamarin or any Project using visual studio 2017.
Open Run > Type Devenev.Exe and press Enter > New Project (Ctrl+Shift+N) -> Select any project Template and customize the project as per your requirement.
Click on Ok and edit the project until it is ready to be exported as a template.
Export Project Template:
In this article, we are export only one project. If you have more than one project in your current solution, I will explain in my next article.
Edit the project as per your requirement > Select the project menu from Visual Studio > Select on Export Template.
Choose template Type:
Choose the project template and select your project and click on Next
Export Template Wizard:
Fill out the template options as you want  and click on Finish
  • Provide Template Name is Project template display name  .
  • Provide Template Description is a detailed description about your template
  • Provide Icon Image and icon image will display near to the project Template name
  • Provide preview image and preview image will display near to describe.
  • Check the checkbox for auto import the template into visual studio or after export, will generate zip file and you can move the zip file to following location C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates

Edit Template:
The file explorer should pop up with My Exported Templates where your newly exported project is packed into a .zip file.
You can extract the template and if you want to edit the template, edit form MyTemplate.vstemplate file .

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
 <TemplateData>
   <Name>csharpcornerProjectTemplate</Name>
   <Description>csharpcornerProjectTemplate</Description>
   <ProjectType>CSharp</ProjectType>
   <ProjectSubType>
   </ProjectSubType>
   <SortOrder>1000</SortOrder>
   <CreateNewFolder>true</CreateNewFolder>
   <DefaultName>csharpcornerProjectTemplate</DefaultName>
   <ProvideDefaultName>true</ProvideDefaultName>
   <LocationField>Enabled</LocationField>
   <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
   <Icon>__TemplateIcon.png</Icon>
   <PreviewImage>__PreviewImage.png</PreviewImage>
 </TemplateData>
 <TemplateContent>
   <Project TargetFileName="csharpcornerProjectTemplate.csproj" File="csharpcornerProjectTemplate.csproj" ReplaceParameters="true">
     <ProjectItem ReplaceParameters="true" TargetFileName="App.xaml">App.xaml</ProjectItem>
     <ProjectItem ReplaceParameters="true" TargetFileName="App.xaml.cs">App.xaml.cs</ProjectItem>
     <Folder Name="Assets" TargetFolderName="Assets">
       <ProjectItem ReplaceParameters="false" TargetFileName="LockScreenLogo.scale-200.png">LockScreenLogo.scale-200.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="SplashScreen.scale-200.png">SplashScreen.scale-200.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="Square150x150Logo.scale-200.png">Square150x150Logo.scale-200.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="Square44x44Logo.scale-200.png">Square44x44Logo.scale-200.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="Square44x44Logo.targetsize-24_altform-unplated.png">Square44x44Logo.targetsize-24_altform-unplated.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="StoreLogo.png">StoreLogo.png</ProjectItem>
       <ProjectItem ReplaceParameters="false" TargetFileName="Wide310x150Logo.scale-200.png">Wide310x150Logo.scale-200.png</ProjectItem>
     </Folder>
     <ProjectItem ReplaceParameters="false" TargetFileName="csharpcornerProjectTemplate_TemporaryKey.pfx">csharpcornerProjectTemplate_TemporaryKey.pfx</ProjectItem>
     <ProjectItem ReplaceParameters="true" TargetFileName="MainPage.xaml">MainPage.xaml</ProjectItem>
     <ProjectItem ReplaceParameters="true" TargetFileName="MainPage.xaml.cs">MainPage.xaml.cs</ProjectItem>
     <ProjectItem ReplaceParameters="false" TargetFileName="Package.appxmanifest">Package.appxmanifest</ProjectItem>
     <Folder Name="Properties" TargetFolderName="Properties">
       <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
       <ProjectItem ReplaceParameters="true" TargetFileName="Default.rd.xml">Default.rd.xml</ProjectItem>
     </Folder>
   </Project>
 </TemplateContent>
</VSTemplate>

Custom Visual Studio template:
You can now restart Visual studio and go to New Project in Visual Studio and find the Project template like below
After click on Ok , project will automatically create new project with your default functionality .


Summary:
In this article, you learned how to create a project template using visual studio
If you have any questions/ feedback/ issues, please write in the comment box.