Thursday, April 23, 2020

Google ComboChart Example

1. Write the following  code in index.html page and run in browser.


Example


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <!--<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>-->
    <script src="loader.js"></script>
    <script type="text/javascript">
        google.charts.load('current', { 'packages': ['corechart'] });
        google.charts.setOnLoadCallback(drawVisualization);

        function drawVisualization() {
            // Some raw data (not necessarily accurate)
            var data = google.visualization.arrayToDataTable([
                ['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua New Guinea', 'Rwanda', 'Average', 'Average2', 'Average2'],
                ['2004/05', 165, 938, 522, 998, 450, 614.6, 614.6, 214.6],
                ['2005/06', 135, 1120, 599, 1268, 288, 682, 414.6, 314.6],
                ['2006/07', 157, 1167, 587, 807, 397, 623, 414.6, 414.6],
                ['2007/08', 139, 1110, 615, 968, 215, 609.4, 304.6, 514.6],
                ['2008/09', 136, 691, 629, 1026, 366, 569.6, 604.6, 614.6]
            ]);

            var options = {
                title: 'Monthly Coffee Production by Country',
                vAxis: { title: 'Cups' },
                hAxis: { title: 'Month' },
                seriesType: 'bars',
                series: { 5: { type: 'line' }, 6: { type: 'line' }, 7: { type: 'line' } }
            };

            var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }
    </script>
    <title>Chard Example</title>
</head>
<body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>

Wednesday, April 22, 2020

Self Host Web API in C# ASP.NET Web Api with Swagger

Please download source code form here

1. Add a console application
2. Add the following nuget packages in you application


  •    Microsoft.AspNet.WebApi.Client
  •    Microsoft.AspNet.WebApi.Core
  •   Microsoft.AspNet.WebApi.SelfHost
  •   Microsoft.AspNet.WebApi.WebHost  
  •   Swashbuckle
  •   Swashbuckle.Core

3. Add a windows service class "WebApi.cs"   and write the following code

    partial class WebApi : ServiceBase
    {
        public WebApi()
        {
            InitializeComponent();
        }
        private HttpSelfHostConfiguration config;
        protected override void OnStart(string[] args)
        {
            config = new HttpSelfHostConfiguration("http://localhost:8085");
            config.MapHttpAttributeRoutes();
            //config.Routes.MapHttpRoute("default",
            //                            "api/{controller}/{id}",
            //                            new { id = RouteParameter.Optional });
            config.Routes.MapHttpRoute(
                                      name: "API Default",
                                      routeTemplate: "api/{controller}/{id}",
                                        defaults: new { id = RouteParameter.Optional }
                                    );
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "WebApiSelfHost");
                c.IncludeXmlComments(GetXmlcontetpat());
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                c.DescribeAllEnumsAsStrings();
                //c.IncludeXmlComments(string.Format(@"{0}\bin\WebApiSelfHost.XML",
                //                     System.AppDomain.CurrentDomain.BaseDirectory));
            }).EnableSwaggerUi(x => x.DisableValidator());

            var server = new HttpSelfHostServer(config);
            var task = server.OpenAsync();
            task.Wait();
        }

        private string GetXmlcontetpat()
        {
            return AppDomain.CurrentDomain.BaseDirectory + @"WebApiSelfHost.XML";
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            if (config != null)
            {
                config.Dispose();
            }

        }
        internal void TestStartupAndStop(string[] args)
        {
            this.OnStart(args);
            //Console.ReadLine();
            //this.OnStop();
        }

        [Conditional("DEBUG_SERVICE")]
        private static void DebugMode()
        {
            Debugger.Break();
        }
    }

4. write the following code on your Main function in Program.cs

         static void Main(string[] args)
        {
            if (Environment.UserInteractive)
            {
                WebApi service1 = new WebApi();
                service1.TestStartupAndStop(args);
            }
            else
            {
                ServiceBase[] ServicesToRun;
                WebApi _selfHostService = new WebApi();
                _selfHostService.ServiceName = "WebAPI_Hosted";
                ServicesToRun = new ServiceBase[]
                {
                _selfHostService
                };
                ServiceBase.Run(ServicesToRun);
            }
            Console.WriteLine("Web API Server has started at http://localhost:8085");
            Console.ReadLine();
        }


Please download source code form here

Bind SSL on Port or Self Host WCF Service add SSL on Port

To Create certificate first, Run flowing command on power shell (Administrator mode) Create cert. New-Sel...