Uncategorized

How to update the Entity Framework model, when changing stored procedure in Database

Solution:

All you need to do is to open your entity model diagram window, then click on Model Browser then go through each and delete the desired stored procedure artifacts in below folders and SAVE the file.

  1. Complex Types
  2. Function Imports
  3. Stored Procedures / Functions

Advertisements
Development, Entity Framework, Uncategorized

How to force Entity Framework to Always Load Fresh copy of Data from the Database

How to force Entity Framework to Always Load Fresh copy of Data from the Database

Solution:

There are many solution to achieve this result.

The most popular and easiest one is to dispose the existing context object and create new one

entities.Dispose();
entities = new Entities(); // DbContext

but it is not optimal you are disposing whole context which could be very expensive.

Optimal Solution:

Db.Entry(item).Reload(); // Db here means DbContext

this will only load the entity (item) you passed from the database and if you changed the item before reload, after reload the change will be overwritten by the values coming from database.

For more information Please check

Azure App Service, Development

How to serve WOFF fonts from Azure App Service

How to serve WOFF fonts from Azure App Service.

Solution:

1. Locate or create web.config

If you have asp.net application you will find the web.config but if you have hosted any other distrubution such as wordpress you can still create the web.config if this is not presented already.

2. Include/merge below code in web.config


  
    
      <mimeMap fileExtension="woff" mimeType="application/font-woff" />
      <mimeMap fileExtension="woff2" mimeType="application/font-woff" /> 
    
  

How to do it:

Go to Azure portal -> Locate your App Service -> Under Development Tools click on Advanced Tools -> click on Go link

How to serve woff font files from Azure App Service

A new tab will be opened and you will see Kudu logo, select Debug console and then CMD

Kudu click on cmd - How to serve woff font files from Azure App Service .png

Now navigate to the D:homesitewwwroot and find the web.config, if you find it then merge the code, else create it and copy paste the code mentioned in step 2.

In fact you can put any file extension you want to serve Azure App Service to serve as static file.

Merge web.config entreies - How to serve woff font files from Azure App Service.png

For more Details

Azure API Management, Cloud Computing

How to setup your Open API in Azure API management : Part 3

 

In this post, we are discussing, how to Get Started with Azure API Management, how we can automate API subscription and management using Azure API Management.

This post is a part of the Azure Services series, in this series, we will discuss about Microsoft Cloud Application Architecture and common cloud computing patterns to make application resilient and scalable. We will be using Asp.net MVC as our framework for applicable examples and Azure as cloud services provider.

You can find all the Microsoft Azure Services; we have covered already at Azure series.

How to setup your Open API in Azure API management : Part 3

In previous post we discussed, how to create your own Open API in Dot.net Core using swagger and published on Azure App Service.

In this post we are going to configure our Open API in Azure API management.

We had published our Open API on Azure App service, we are going to use it to configure it with Azure API management. In order to proceed we need to have Full URL of the json file generated by the swagger, in our case it is as below:

http://azureapimanagementv1.azurewebsites.net/swagger/v1/swagger.json

you can click on the URL marked below and it will take you to the swagger.json file.

Let go to the Azure Portal | Azure API Management instance | under API management click APIs | Click on OpenAPI Specification | and copy the URL of the swagger.json file.

Now go to Azure API Over view | click on publisher portal | APIs | Import API | select From URL | choose Swagger | choose New API radio button | mention the suffix URL, this could be name of your service | and choose the scheme, we are going for https | click save.

Now if you go to Azure API Management Instance | APIs , you could see that our service is available there.

Let us assign it to ultimate product, in Products text box type unlimited | choose unlimited | save | verify the API is associated with product by going to unlimited product, as you can see our API is listed there.

Now to use this API, all we need to do is subscribe, which in turn will generate the subscription id that can be used to call the service methods. Since in this series we already subscribed to the unlimited product so we can use our primary key to call the methods of our service.

Go to Overview in Azure API Management | click on developer portal | sign in | Click APIs tab | click on Azure API management API | click try it | fill the field | send

By default, Delete method is select so beware of this when you test the API on production.

You should receive the successful response as below.

Let us consume this service, create a new Asp.net Core Web application with the name client.

We will use NSwagStudio to generate client side classes for the C#, download the tool from below Address:

https://github.com/RSuter/NSwag/wiki/NSwagStudio
First get the swagger URL for the API. Go to developer portal | API tab | Click on AzureAPIManagementV1 | click on API definition | choose Open API | then copy the URL

Once NSwagStudio is installed, open the tool, mention the URL and check CSharp Client and either you can click on Generate output button and get the CSharp client code which you can copy paste in visual studio or you can generate the file for you. I copied the path to my client project including the cs file name which will be generated. If you are getting error while generating, try to run the tool as administrator.

If you go to your visual studio the File is already added for you, if the file is not visible use the add existing item option to include it in the project.

Now let us use this to call the API. Open, Index.cshtml.cs in Get method type below code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Client.Pages

{

public
class
IndexModel : PageModel

{

public
async
void OnGet()

{

System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();

httpClient.DefaultRequestHeaders.Add(“Ocp-Apim-Subscription-Key”, “ba210977b3374feb89caafefe28ba78d”);

MyNamespace.Client client = new MyNamespace.Client(httpClient);

System.Collections.ObjectModel.ObservableCollection<string> x = await client.ApiValuesGetAsync();

}

}

}

As you can see we need to include the Ocp-Apim-Subscription-Key
in default header.

If you debug you will get the proper response.

Azure API Management, Azure App Service

Auto documented API using Swagger in dot.net Core to integrate with Azure API Management : Part 2

In this post, we are discussing, how to Get Started with Azure API Management, How we can automate API subscription and management using Azure API Management.

This post is a part of the Azure Services series, in this series, we will discuss about Microsoft Cloud Application Architecture and common cloud computing patterns to make application resilient and scalable. We will be using Asp.net MVC as our framework for applicable examples and Azure as cloud services provider.

You can find all the Microsoft Azure Services; we have covered already at Azure series.

Auto documented API using Swagger in dot.net Core to integrate with Azure API Management : Part 2

In previous post we discussed, how the Azure API management tool work, briefly discussed Developer dashboard and subscription the Echo API.

In this post we are going to create our own API and set it up with Azure API management and consume it on client side.

Let us fire up our visual studio 2017, File Menu | New Project | Under Visual C# select .NET Core | Select Asp.net Core Web Application | click okay to create it.

Right click on Project in Visual Studio and choose the Manage NuGet Packages.

Click on browse tab, and search for Swagger, we will go for swachbuckle.aspnetcore, since we are using .net core so we need to choose Swachbukle.AspNetCore (this already include swaggerGen, UI and Swagger, so you don’t need to install each of them separately ) Package and install it.

Now add the following bold lines in the Startup.cs file

When we are using Swagger, we need to explicitly need to make sure our controller has Http verb attribute and for non-route parameters “From” body tag.

Under controller Directory | Open Values Controller | and verify that your code match as below:


 

Now we can run our application and see output. Since we have configured the c.RoutePrefix = “” that means the Swagger will open on the root of the site, navigate to the root site, in this example we will go to localhost:61300



As you can see under the Values, there are actions listed, but we do not have any documentation attached to them. Let us do just that.

Open the Values Controller class and add some details like below:

You can use triple forward slash to add summary tag for you, just press /// before the signature of the action and visual studio will add summary tags for you.


Now we need to enable these comments to show up in the swagger UI, Right click on Project | Properties | Build | check the XML documentation File.


Now we need to add using System.IO; in startup.cs

And add below lines of code as well, you may need to change the xml name if your xml file in build tab is different.

public
void ConfigureServices(IServiceCollection services, IHostingEnvironment env)

{


//services.AddMvc();

services.AddMvc().AddJsonOptions(options =>

{

options.SerializerSettings.Formatting = Formatting.Indented;

});

if (env.IsDevelopment())

{

services.ConfigureSwaggerGen(c =>

{


var xmlCommentsPath = Path.Combine(System.AppContext.BaseDirectory, “AzureAPIManagement.xml”);

c.IncludeXmlComments(xmlCommentsPath);

});

}

services.AddSwaggerGen(c =>

{

c.SwaggerDoc(“v1”, new Info { Title = “AzureAPIManagementV1”, Version = “v1” });

});

}

Now run the solution, you can see that the documentation is coming up.


Now our Basic API is ready, let us host it on Azure App Service, you can publish anywhere you want unless you get the Public URL of your API and then in next part we will configure it with API management and will use it on client side.

Right click on project | Publish | create new App Service | fill in the details | Click Create. It will take a little bit for time to publish for first time as it will create new app service for you.


After create | click on publish if it is not publishing already | you will see that after publish it automatically open the API URL in browser.


Next we will configure our API in Azure API Management, which is coming up next.