Posts Tagged Asp.Net Core

Bundling and Minification in AspNetCore

Introduction:

In this article we will look into an easy option to do bundling and minification in Asp.Net Core. For those who are new to Bundling and Minification let’s try to understand these concepts.

Bundling

Bundling is the process of combining multiple javascript files to a single file. By this way we will reduce the number of requests needed for loading the files. i.e. if we had javascript code in multiple files then system will generate multiple requests to load all files however since we bundled all files there will be only one requests to load file. This will improve the load time.

Minification

Minification is the process of reducing the size of files (css , javascript). Usually size reduction happens by removing unwanted spaces in file.

For ex, below Javascript code

will change like below after minification.

Bundling and Minification using Gulp

We have variety of options available for Third party tools like Gulp and Grunt can be used to do bundling and minification process. However the problem with this approach is it involves additional steps which are slightly complex.

Using Bundler and Minification Extension

Mads Kristensen has created an extension namely “Bundler and Minifier”, using this we can easily do both bundling and minification in Asp.netCore. You can download the installer for extension from here.

Once you installed the extension, to do bundling and minification all you do is to Right click on the file and then select Bundler & Minifier option.

2017-08-22 14_38_43-Clipboard

If you have a single javascript file then you will see the only Minify File option.

MinfyOption

Once you select the option extension will generate the minified file for us. We can also follow the same process to minify css files as well.

Advertisements

, , , ,

Leave a comment

How to read config settings in Asp.Net Core

Introduction

In this article I will explain the details of how to read config settings in Asp.Net Core.

Description

In Asp.Net webforms we used web.config file to store the config values like a connection string, appsettings etc.However, in Asp.Net core, the config file doesn’t exist anymore.  Instead, we have a slightly different approach where the configuration API provides a way of configuring an app based on a list of name-value pairs that can be read at runtime from multiple sources. You can read the config values from multiple sources like JSON, XML etc.

In this article we will examine how to implement config files based on JSON formats in Asp.Net Core application.

Add the Required references

To read the values from appsettings.json, we need to add the below references to project

Add appsettings.json file to store config values

We will store the config items as Name:Value pair in this json file.  You can also add hierarchical list in json file where each items will be separated by comma.

Sample value

Create ConfigurationBuilder in Startup.cs class

We use AddJsonFile” method in JsonConfigurationExtensions class to provide the Json File path. To reload the configuration file if the file changes we can set the reloadOnChange property.  You can find details of AddJsonFile method here.

Read the value from appsetttings.json file

Using the GetSection method we can access the values from appsettings.json file

Complete Code

You can find the complete source code for this article here

, , ,

Leave a comment

How to access WCF Service in Asp.Net Core Application

Introduction

How to access WCF Service in Asp.Net Core Application

Description:

In this article I will explain how to consume WCF Service in Asp.Net Core Web Application. In previous .Net Framework we have multiple options to consume a WCF service.  We have the options like Add Service Reference, SvcUtil,  ChannelFactory etc. However in Asp.Net Core there is a slight difference in Consuming WCF Service. In Asp.Net core you will notice that, we don’t have the option “Add Service Reference like in the older .Net Framework.  The solution for this is WCFConectedService.

WCFConnectedService:

Asp.net Core team has come up with an extension called WCFConnectedService, This is a Visual Studio extension for generating SOAP service references in the Asp.Net Core application. This extension can be used with any project types in asp.net core.

We use ConnectedService to access a WCF Service in Asp.Net Core Web Application.

Installing WCF Connected Service Extension:

Below are the steps to install WCF Connected Service Extension in Asp.Net Core WebApplication

  • Right Click on the Connected Services Option

ConnectedService7

If the Extension is not installed then you will see the below window. User need to click on Find more Services link

ConnectedServices1

Extension and Updates window will be displayed and from this window you can find the Visual Studio WCF Connected Service extension option

ConnectedServices2

Click on Download button to install the extension. You may need to close the VisualStudio instances to complete the VSIX installation.

Simple WCF Service

For this example, we have created a WCF Service. In our service we have a sample method called “GetData(int id)”.  This method will take integer input values and return a string “You entered:1”

ISimpleService Interface

SimpleService  Method Implementation

Above service is for demo purpose, you can use connected service approach with any service as per your design.

Accessing WCF Service in Asp.Net Core Web Application

In your Asp.Net Core web application, you will see a Connected Service Option like below

ConnectedService7

Follow the below steps to Add Service Reference

  • Right Click on the Connected Services Option
  • Select Add Connected Service and Add Connected Services wizard will be displayed and Select the WCF -Service Preview option

ConnectedService4

  • Click on the Configure button. This will bring up Configure WCF Service Reference dialog box. Enter your Service url and Click on Go or Discover, it will discover the ServiceConnectedServices5
  • Click on Next and select appropriate options

ConnectedServices6

  • Click on Finish to generate proxy for WCF Service.

Once after this you will be able to see Service Reference Folder added in our project.

ConnectedService3

Now all you need to is to Create the Client and access the service like below

Result

You can download sample application for this article from here

, , , , ,

Leave a comment