Tuesday, June 11, 2019

Host asp.net core web api2 to heroku


Deploy asp.net core 2.0 apps on Heroku

Hello,
In this tutorial, we’ll be taking a look at how to deploy web apps built with asp.net core 2.0 to heroku.com, a cloud-hosting platform.








Heroku!

#1. We’ll need a few things:

#2. We’ll need a web app running on asp.net core 2.0.

For this tutorial, I have created a simple web api application on GitHub called sample-web-api which is now hosted on heroku.
All it does is returns an array of strings at the ~/api/values endpoint.
If you need help with creating an asp.net core 2.0 app, the official documentation by Microsoft is good way to start.

#3. Publish your App

dotnet publish -c Release

#4. Add a Dockerfile to the root of your project

In the root of your project, add a file called Dockerfile (no extensions) and place the following in it:
FROM microsoft/aspnetcore
WORKDIR /app
COPY . .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <AssemblyName>.dll
Be sure to replace <AssemblyName> with the name of your project assembly e.g. SampleWebApi

#5. Copy the Dockerfile to your publish directory

Your publish directory should be:
./bin/release/netcoreapp2.0/publish

#6. Build the Docker Image

docker build -t <image-name> ./bin/release/netcoreapp2.0/publish
Be sure to replace <image-name> with the name you intend to give the image e.g. sample-web-api

#7. Create the app on Heroku using the dashboard

  • Make sure you have a heroku.com account
  • Login to the dashboard and create the app
  • I called mine sample-web-api, so you can’t use that again

#8. Be sure you are logged in to heroku and its container registry

heroku login
heroku container:login

#9. Tag the heroku target image

docker tag <image-name> registry.heroku.com/<heroku-app-name>/web

#10. Push the docker image to heroku

docker push registry.heroku.com/<heroku-app-name>/web
Be sure to replace <heroku-app-name> with the name of your heroku app e.g. sample-web-api


eyJhbGciOiJSUzI1NiIsImtpZCI6IjAwOTZhZDZmZjdjMTIwMzc5MzFiMGM0Yzk4YWE4M2U2ZmFkOTNlMGEifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vYXBwYXJ0bWVudGFwcC03NTdlNCIsImF1ZCI6ImFwcGFydG1lbnRhcHAtNzU3ZTQiLCJhdXRoX3RpbWUiOjE1MzA1OTYxMzgsInVzZXJfaWQiOiJzNUJkbWUyT2VaYUl4eE5NNUIxRDhHZElOYmgxIiwic3ViIjoiczVCZG1lMk9lWmFJeHhOTTVCMUQ4R2RJTmJoMSIsImlhdCI6MTUzMDU5NjEzOCwiZXhwIjoxNTMwNTk5NzM4LCJlbWFpbCI6Im15dXNlckBzZXJ2aWNlLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJteXVzZXJAc2VydmljZS5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.Av7EJHbGz2_pNxlqybvInRqrqagcVF2VcOTTl0E6s0pvAGfA8FkHLbIzzsR5t2UdBPC1oFHvKgbxePTcIMNesp_aSOxLluyu5I-EnSNi-loUeh-ZjoCC-qblTq1EQATESdqkn1AXlrrnOfAwHLqRVJmloUqgJJ7zMl7r6e7hIt3BVeSFxFVlsca1TUgOwNCkoao5HJmIQIwCXts9HQpiGowzzun_McumAzj_gxtc9PYqXx8t8Cfwx1l5GNogu0bTji7BYHBrs6Y_IjMfayHKAQ-6rVrEH2dTWpydJ9p3eGCwy2-AhV7mNoZvuTOcL7ps2YONDPDm_iWAX5tThM6DpQ

Updated: heroku link for publishing

heroku container:release web --app <webappName/imagename>

#Bonus. Check that your app works

docker build -t appwebapi2 D:\BitbucketRepo\AppartmentApp\AppartmentApp.WebApi\AppartmentApp.WebApi\bin\Release\PublishOutput
heroku login
heroku container:login
docker tag appwebapi2 registry.heroku.com/appwebapi2/web
docker push registry.heroku.com/appwebapi2/web
heroku container:release web --app appwebapi2

heroku logs
  • Load https://<heroku-app-name>.herokuapp.com in your browser.
  • If all went well, you should see your app up and running.
  • If if did, Yaaaay!!!
  • If not, feel free to give feedback in the comments below.
Clap 

Thursday, March 31, 2016

CROS Calls using javascript jQuery Ajax


This content is form answer of the question how-does-access-control-allow-origin-header-work
from stackoverflow. I am just keeping one copy of that answer at my blog for future reference :

Access-Control-Allow-Origin is a CORS (Cross-Origin Resource Sharing) header.
When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Originresponse header to tell the browser that the content of this page is accessible to certain origins. (Anorigin is a domain, plus a scheme and port number.) By default, Site B's pages are not accessible to any other origin; using the Access-Control-Allow-Origin header opens a door for cross-origin access by specific requesting origins.
For each resource/page that Site B wants to make accessible to Site A, Site B should serve its pages with the response header:
Access-Control-Allow-Origin: http://siteA.com
Modern browsers will not block cross-domain requests outright. If Site A requests a page from Site B, the browser will actually fetch the requested page on the network level and check if the response headers list Site A as a permitted requester domain. If Site B has not indicated that Site A is allowed to access this page, the browser will trigger the XMLHttpRequest's error event and deny the response data to the requesting JavaScript code.

Non-simple requests

What happens on the network level can be slightly more complex than explained above. If the request is a "non-simple" request, the browser first sends a data-less "preflight" OPTIONS request, to verify that the server will accept the request. A request is non-simple when either (or both):
  • using an HTTP verb other than GET or POST (e.g. PUT, DELETE)
  • using non-simple request headers; the only simple requests headers are:
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type (this is only simple when its value is application/x-www-form-urlencoded, multipart/form-data, or text/plain)
If the server responds to the OPTIONS preflight with appropriate response headers (Access-Control-Allow-Headers for non-simple headers, Access-Control-Allow-Methods for non-simple verbs) that match the non-simple verb and/or non-simple headers, then the browser sends the actual request.
Supposing that Site A wants to send a PUT request for /somePage, with a non-simple Content-Typevalue of application/json, the browser would first send a preflight request:
OPTIONS /somePage HTTP/1.1
Origin: http://siteA.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: Content-Type
Note that Access-Control-Request-Method and Access-Control-Request-Headers are added by the browser automatically; you do not need to add them. This OPTIONS preflight gets the successful response headers:
Access-Control-Allow-Origin: http://siteA.com
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: Content-Type
When sending the actual request (after preflight is done), the behavior is identical to how a simple request is handled. In other words, a non-simple request whose preflight is successful is treated the same as a simple request (i.e., the server must still send Access-Control-Allow-Origin again for the actual response).
The browsers sends the actual request:
PUT /somePage HTTP/1.1
Origin: http://siteA.com
Content-Type: application/json

{ "myRequestContent": "JSON is so great" }
And the server sends back an Access-Control-Allow-Origin, just as it would for a simple request:
Access-Control-Allow-Origin: http://siteA.com
See Understanding XMLHttpRequest over CORS for a little more information about non-simple requests.
Happy Living...
Happy Coding...
 

Thursday, August 27, 2015

Search Text in schema of Stored Procedure

With a big database this is one of the basic issue if you want to search whether a particular string/value available in anywhere in schema of stored procedure.
So we cant do it manually (yeah we have option of looking into dependencies but this is one step ahead) like It will help us to search in even comments too.
 

SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%This part is commented%'
GO


Happy Living...
Happy Coding...
Happy Concepts ...

Thursday, June 11, 2015

Reset or Normalize default User Code Stylesheet of Browser

Here i am going to talk about "User Code Style sheet". It is default style sheet of browser get applied on Dom elements if nothing is specified about how to handle elements appearance on page.

Problem:

But it might get applied if the tags are not closed properly and will lead to different UI display.

Solution :


To resolve this issue we have 2 either to reset it or normalize stylesheet.

Reset CSS: (Get Reset Css)
Resets browsers defauts

Normalize CSS: 
Preserve useful browser defaults rather than erasing them. 
Normalize styles for a wide range of HTML elements.
Correct bugs and common browser inconsistencies. 
Improve usability with subtle improvements. 
Explain the code using comments and detailed documentation.

Note: Try to put it after all your style sheet to implement inherit property. 

Happy Living.....
Happy Concepts.....
Happy Coding.....

Friday, May 22, 2015

Enable Nuget packages in Visual Studio 2010

The NuGet is easier way to include dlls or other projects/utilities in your projects. If we aren't using it , in one way we are selecting some tough way to achieve something that can be implemented in straight forward way.

Open Visual studio in menu bar click on Tools-> Extension Manager


fig 1

It will open a new popup as shown below, In this popup you can see there isn't any Extension Manager installed. Once we install Nuget it will appear in this screen.

fig 2
Now we will do following steps:
1. Click on Online Gallery under Installed Extensions.
2. Type Nuget in search text box at right top corner
3. Now it will appear in middle area of window now click to Download.


fig 3
Now you will get another popup for installation read term and conditions and click on install.



fig 4

On completion of installation there will be a message box, close it.

fig 5

Once it get completed, Restart Visual Studio  you will able to see it under Installed Extensions.


fig 6
 

And its done. In next blog we will check how to use NuGet Package Manager.

Happy Living....
Happy Coding....
Happy Concepts....
 

Wednesday, November 5, 2014

Change Doc type to IE8 or IE8 Compatibility mode

Change Doc type to IE8 or IE8 Compatibility mode:

There are some old development that doesn't support in newer version of IE and Chrome. So best option is to make changes in code work for latest IE versions and Chrome. But this is the case not always possible for example previous code was built using third party controls (Infragistics in my case) to make it work with Newer version of IE we need upgraded version on Infragistics and it involves addition cost of licensing.
One other work around if it is possible to support only IE  browser than we can use solution suggested below: (BUT it is not best solution)

<html><head>
 <title> My Page</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">

<meta http-equiv="X-UA-Compatible" content="IE=8">
<head><body>
 ---------------
</body></html>


Note : Make sure to use meta tag just after title tag otherwise it wont work.





Happy Coding...
Happy Concepts...
Happy Living....

Monday, May 5, 2014

Convert local datetime to UTC or Universal data time and UTC to local datetime


--Steps required before performing conversion:

--step:

Declare @TimezoneOffset int=0

-- variable will hold the timezone difference in minutes

set @TimezoneOffset =  DATEDIFF(Minute, GETUTCDATE(), GETDATE())

--Now we have localtimezone difference with UTC.

--Convert Local datetime to UTC:-

Declare @LocalDatetime datetime= GetDate()

Declare @UTCDatetime datetime =null

select  @UTCDatetime= DateAdd(Minute,-@TimezoneOffset,@LocalDatetime)

--Convert UTC to Local datetime :-

 Declare @ConvertedLocalDatetime datetime=null

select @ConvertedLocalDatetime= DateAdd(Minute, (@TimezoneOffset),@UTCDatetime)

select 'LocalTime:-',@LocalDatetime,'  UTC Datetime : ', @UTCDatetime ,'ConvertedLocalDatetime : ', @ConvertedLocalDatetime