18 Mart 2019 Pazartesi

ASP.NET MVC ActionFilterAttribute, IActionFilter Intercept Requests

    public class CustomActionFilter : ActionFilterAttribute, IActionFilter
    {
        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            Stopwatch tmr = new Stopwatch();
            tmr.Start();
            filterContext.HttpContext.Items["tmr"] = tmr;
            Debug.WriteLine(JsonConvert.SerializeObject(filterContext.ActionParameters));
        }

        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            Stopwatch tmr = (Stopwatch)filterContext.HttpContext.Items["tmr"];
            Debug.WriteLine(tmr.ElapsedMilliseconds);
        }
    }

        [CustomActionFilter]
        public ActionResult Index(string p1, string p2)
        {
            return Content(DateTime.Now.ToString());
        }

23 Ocak 2019 Çarşamba

MS SQL Server Date StartOfDay EndOfDay

CREATE FUNCTION [dbo].[fnDate_EndOfDay]
(
@date datetime
)
RETURNS datetime
AS
BEGIN
return CONVERT(DATETIME, CONVERT(varchar(11), @date, 111 ) + ' 23:59:59', 111)
END

CREATE FUNCTION [dbo].[fnDate_StartOfDay]
(
@date datetime
)
RETURNS datetime
AS
BEGIN
return CONVERT(DATETIME, CONVERT(varchar(11), @date, 111 ) + ' 00:00:00', 111)
END


print dbo.fnDate_StartOfDay(getdate())
print dbo.fnDate_EndOfDay(getdate())


Jan 24 2019 12:00AM
Jan 24 2019 11:59PM

14 Ocak 2019 Pazartesi

WebClient UploadString POST Object as JSON

            string apiUrl = "http://localhost:14101/Print/MutfakAdisyonYazdir";

            var req = new MutfakAdisyonYazdir_RequestDTO();
            req.TokenGuid = Guid.NewGuid().ToString();
            req.AdisyonSiparisIdler = new List<long>() { 123, 234, 345, 456 };

            string inputJson = JsonHelper.Serialize(req);
            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            string json = client.UploadString(apiUrl, inputJson);

            var resp = JsonHelper.Deserialize<BaseResponseDTO>(json);

8 Ocak 2019 Salı

SQL Server Reorganize All Indexes in Database

declare @tableName nvarchar(500)
declare @indexName nvarchar(500)
declare @indexType nvarchar(55)
declare @percentFragment decimal(11,2)

declare FragmentedTableList cursor for
 SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName,
   ind.name AS IndexName, indexstats.index_type_desc AS IndexType,
   indexstats.avg_fragmentation_in_percent
 FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats
   INNER JOIN sys.indexes ind ON ind.object_id = indexstats.object_id
        AND ind.index_id = indexstats.index_id
  WHERE
-- indexstats.avg_fragmentation_in_percent , e.g. >30, you can specify any number in percent
   indexstats.avg_fragmentation_in_percent > 5
  AND ind.Name is not null
  ORDER BY indexstats.avg_fragmentation_in_percent DESC

    OPEN FragmentedTableList
    FETCH NEXT FROM FragmentedTableList 
    INTO @tableName, @indexName, @indexType, @percentFragment

    WHILE @@FETCH_STATUS = 0
    BEGIN
      print 'Processing ' + @indexName + ' on table ' + @tableName + ' which is ' + cast(@percentFragment as nvarchar(50)) + ' fragmented'
     
      if(@percentFragment<= 30)
      BEGIN
            EXEC( 'ALTER INDEX [' +  @indexName + '] ON ' + @tableName + ' REBUILD; ')
       print 'Finished reorganizing ' + @indexName + ' on table ' + @tableName
      END
      ELSE
      BEGIN
         EXEC( 'ALTER INDEX [' +  @indexName + '] ON ' + @tableName + ' REORGANIZE;')
        print 'Finished rebuilding ' + @indexName + 'on table ' + @tableName
      END 
      FETCH NEXT FROM FragmentedTableList 
        INTO @tableName, @indexName, @indexType, @percentFragment
    END
    CLOSE FragmentedTableList
    DEALLOCATE FragmentedTableList