﻿
{"id":26603,"date":"2023-10-03T23:11:52","date_gmt":"2023-10-03T23:11:52","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=26603"},"modified":"2023-10-03T23:11:52","modified_gmt":"2023-10-03T23:11:52","slug":"net-8-method-cagrilarini-interceptorlarla-degistirme","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/net-8-method-cagrilarini-interceptorlarla-degistirme\/","title":{"rendered":".NET 8 &#8211; Method \u00c7a\u011fr\u0131lar\u0131n\u0131 Interceptor&#8217;larla De\u011fi\u015ftirme"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde .NET 8&#8217;de C# 12 ile birlikte gelen Interceptor \u00f6zelli\u011fini inceliyor olaca\u011f\u0131z.<\/p>\n<h3>Interceptor Nedir? Neden Bu Yap\u0131lanmaya \u0130htiyac\u0131m\u0131z Vard\u0131r?<\/h3>\n<p>Interceptor, uygulamadaki herhangi bir metoda yap\u0131lan \u00e7a\u011fr\u0131y\u0131 alternatif ba\u015fka bir metotla de\u011fi\u015ftirmemize ya da ilgili metodun i\u015flevini engellememize yarayan, yaz\u0131l\u0131msal yakla\u015f\u0131mlar a\u00e7\u0131s\u0131ndan a\u015fina oldu\u011fumuz lakin programlama dili a\u00e7\u0131s\u0131ndan ise C# 12 ile hayat\u0131m\u0131za giren ilgin\u00e7 ve yeni bir deneysel \u00f6zelliktir.<\/p>\n<p>Interceptor&#8217;lar, \u00f6nceki <a href=\"https:\/\/www.gencayyildiz.com\/blog\/net-8-minimal-api-ahead-of-timeaot-compilation-template\/\" rel=\"noopener\" target=\"_blank\">.NET 8 \u2013 Minimal API Ahead of Time(AOT) Compilation Template<\/a> ba\u015fl\u0131kl\u0131 makalemizde inceledi\u011fimiz AOT i\u00e7in olmasa da esas\u0131nda AOT merkezli d\u00fc\u015f\u00fcnce neticesinde tasarlanm\u0131\u015f yap\u0131lard\u0131r diyebiliriz. \u00c7\u00fcnk\u00fc interceptor&#8217;lar\u0131 kullanarak, daha \u00f6nce AOT dostu olmayan kodu alabilir ve onu source generator taraf\u0131ndan olu\u015fturulan bir s\u00fcr\u00fcmle de\u011fi\u015ftirebilme \u015fans\u0131n\u0131 elde edebiliriz.<\/p>\n<p>\u015eimdi interceptor&#8217;lar\u0131 daha iyi bir \u015fekilde anlayabilmek i\u00e7in basit bir \u00f6rnek \u00fczerinden yap\u0131y\u0131 g\u00f6zlemleyelim.<\/p>\n<h3>Basit Bir \u00d6rnek \u00dczerinden Interceptor Kullan\u0131m\u0131<\/h3>\n<p>Interceptor&#8217;lar her ne kadar source generator&#8217;lar i\u00e7in tasarlanm\u0131\u015f olsa da burada basitlik ad\u0131na ham bir interceptor olu\u015fturmaya odaklanacak ve olay\u0131 \u00f6zetlemi\u015f olaca\u011f\u0131z.<\/p>\n<p>Her \u015feyden \u00f6nce .NET 8 Preview framework&#8217;\u00fcn de herhangi bir proje olu\u015fturman\u0131z gerekmektedir. \u015eahsen ben deniz s\u0131radan bir Console Application \u00fczerinden \u00f6rneklendirme yap\u0131yor olaca\u011f\u0131m.<\/p>\n<p>\u0130lk olarak olu\u015fturdu\u011funuz projede interceptor&#8217;lar\u0131 kullan\u0131labilir hale getirmek i\u00e7in uygulaman\u0131n .csproj dosyas\u0131nda <code>&lt;Features&gt;InterceptorsPreview&lt;\/Features&gt;<\/code> \u00f6\u011fesinin ayarlanmas\u0131 gerekmektedir.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;\r\n\r\n\t&lt;PropertyGroup&gt;\r\n\t\t&lt;OutputType&gt;Exe&lt;\/OutputType&gt;\r\n\t\t&lt;TargetFramework&gt;net8.0&lt;\/TargetFramework&gt;\r\n\t\t&lt;ImplicitUsings&gt;enable&lt;\/ImplicitUsings&gt;\r\n\t\t&lt;Nullable&gt;enable&lt;\/Nullable&gt;\r\n\t\t&lt;Features&gt;InterceptorsPreview&lt;\/Features&gt;\r\n\t&lt;\/PropertyGroup&gt;\r\n\r\n&lt;\/Project&gt;\r\n<\/pre>\n<\/div>\n<p>Ard\u0131ndan uygulamada <code style=\"color:green;\">System.Runtime.CompilerServices<\/code> namespace&#8217;i i\u00e7erisinde a\u015fa\u011f\u0131daki attribute&#8217;un tan\u0131mlanmas\u0131 gerekmektedir. Bu attribute, interceptor&#8217;\u0131n davran\u0131\u015f\u0131n\u0131 farkl\u0131 bir metoda y\u00f6nlendirmemizi sa\u011flayacak olan yap\u0131lanman\u0131n ta kendisidir. Ancak hen\u00fcz hi\u00e7bir yerde herhangi bir base class library&#8217;sine(BCL) dahil edilmedi\u011finden dolay\u0131 bu attribute&#8217;u kendinizin tan\u0131mlamas\u0131 gerekmektedir.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nnamespace System.Runtime.CompilerServices\r\n{\r\n    &#x5B;AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]\r\n    sealed class InterceptsLocationAttribute(string filePath, int line, int column) : Attribute\r\n    {\r\n\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Attribute&#8217;u incelerseniz e\u011fer interceptor&#8217;\u0131n hangi dosyadaki(<code>filePath<\/code>), ka\u00e7\u0131nc\u0131 sat\u0131r(<code>line<\/code>) ve kolondaki(<code>column<\/code>) komutlar i\u00e7in devreye girece\u011fini ifade edecek alanlar tan\u0131mlanm\u0131\u015f vaziyettedir.<\/p>\n<p>Bu tan\u0131mlamadan sonra art\u0131k interceptor&#8217;\u0131 olu\u015fturabilirsiniz. Bunun i\u00e7in misal olarak a\u015fa\u011f\u0131daki i\u00e7eri\u011fe sahip olan &#8216;Program.cs&#8217; dosyas\u0131n\u0131 referans al\u0131rsak e\u011fer;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nX();\r\n\r\nvoid X()\r\n{\r\n    Console.WriteLine(&quot;Laylaylom galiba sana g\u00f6re sevmeler...&quot;);\r\n}\r\n<\/pre>\n<\/div>\n<p>X metoduna bir interceptor \u00f6rneklendirmesi ger\u00e7ekle\u015ftirebiliriz. Bunun i\u00e7in yap\u0131lmas\u0131 gereken \u00e7al\u0131\u015fma a\u015fa\u011f\u0131daki gibidir;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    static public class CodeGenerator\r\n    {\r\n        &#x5B;InterceptsLocation(@&quot;C:\\Users\\Gencay\\Desktop\\Interceptor.Example\\Interceptor.Example\\Program.cs&quot;, line: 1, column: 1)]\r\n        static public void XInterceptor()\r\n        {\r\n            Console.WriteLine(&quot;Doldur be meyhaneci, bo\u015f kalmas\u0131n kadehim...&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<\/div>\n<p>Yukar\u0131daki kod blo\u011funa g\u00f6z atarsan\u0131z e\u011fer static bir s\u0131n\u0131f olu\u015fturulmakta ve i\u00e7erisinde interceptor&#8217;la y\u00f6nlendirmede bulunacak hedef metotla ayn\u0131 imzaya sahip bir metot tan\u0131m\u0131 ger\u00e7ekle\u015ftirilmektedir. \u0130\u015fte bu metot, uygulamada X metodu tetiklendi\u011fi taktirde, bu tetiklenmeyi kesecek ve araya girecek olan metottur.<\/p>\n<p>Tabi burada dikkat edilmesi gereken bir husus vard\u0131r ki, o da; <code>InterceptsLocation<\/code> attribute&#8217;unu kullan\u0131rken <code>line<\/code> ve <code>column<\/code> \u00f6zelliklerine verilen de\u011ferlerin kesinlikle bir metot \u00e7a\u011fr\u0131s\u0131na kar\u015f\u0131l\u0131k gelmesi gereklili\u011fidir. Aksi taktirde verilen de\u011ferler a\u015fa\u011f\u0131daki g\u00f6rseldeki gibi alakas\u0131z bir noktaya\/yap\u0131ya denk geliyorsa ilgili attribute hata verecektir.<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2023\/10\/NET-8-Method-Cagrilarini-Interceptorlarla-Degistirme.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2023\/10\/NET-8-Method-Cagrilarini-Interceptorlarla-Degistirme.png\" alt=\".NET 8 - Method \u00c7a\u011fr\u0131lar\u0131n\u0131 Interceptor&#039;larla De\u011fi\u015ftirme\" width=\"862\" height=\"317\" class=\"aligncenter size-full wp-image-26613\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2023\/10\/NET-8-Method-Cagrilarini-Interceptorlarla-Degistirme.png 862w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2023\/10\/NET-8-Method-Cagrilarini-Interceptorlarla-Degistirme-300x110.png 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2023\/10\/NET-8-Method-Cagrilarini-Interceptorlarla-Degistirme-768x282.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/a>G\u00f6rselde, &#8216;Program.cs&#8217; dosyas\u0131 i\u00e7erisinde belirtilen 3. sat\u0131r 1. kolonun nereye denk geldi\u011fi g\u00f6sterilmektedir. Haliyle bu durumda a\u015fa\u011f\u0131dakine benzer bir hata verilecektir.<\/p>\n<blockquote><p><em style=\"color:red;\">The provided line and character number does not refer to an interceptable method name, but rather to token &#8216;void&#8217;.<\/em><\/p><\/blockquote>\n<p>Ayr\u0131ca a\u015fa\u011f\u0131daki gibi birden fazla metodu ayn\u0131 interceptor&#8217;a y\u00f6nlendirebilirsiniz.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nX();\r\nY();\r\n\r\nvoid X()\r\n{\r\n    Console.WriteLine(&quot;Laylaylom galiba sana g\u00f6re sevmeler...&quot;);\r\n}\r\n\r\nvoid Y()\r\n{\r\n    Console.WriteLine(&quot;Sende ba\u015f\u0131n\u0131 al\u0131p gitme ne olur, ne olur tut ellerimi...&quot;);\r\n}\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    static public class CodeGenerator\r\n    {\r\n        &#x5B;InterceptsLocation(@&quot;C:\\Users\\Gencay\\Desktop\\Interceptor.Example\\Interceptor.Example\\Program.cs&quot;, line: 1, column: 1)]\r\n        &#x5B;InterceptsLocation(@&quot;C:\\Users\\Gencay\\Desktop\\Interceptor.Example\\Interceptor.Example\\Program.cs&quot;, line: 2, column: 1)]\r\n        static public void XInterceptor()\r\n        {\r\n            Console.WriteLine(&quot;Doldur be meyhaneci, bo\u015f kalmas\u0131n kadehim...&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<\/div>\n<blockquote><p><em>Yakalanan parametreli metodun, parametre de\u011ferleri interceptor metoduna da aktar\u0131lacakt\u0131r. Buna dair \u00f6rne\u011fi yaz\u0131n\u0131n sonunda payla\u015ft\u0131\u011f\u0131m github reposundaki \u00f6rnek \u00e7al\u0131\u015fmada g\u00f6zlemleyebilirsiniz.<\/em><\/p><\/blockquote>\n<p>Evet, g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere interceptor&#8217;lar bu \u015fekilde kullan\u0131lmaktad\u0131rlar. Burada konuya dair bilinmesi gereken s\u0131n\u0131rl\u0131l\u0131klardan biri, interceptor&#8217;lar\u0131n yaln\u0131zca metotlar\u0131n m\u00fcdahalesine odaklanmas\u0131yken bir di\u011feri ise interceptor&#8217;\u0131n metot imzas\u0131yla tam olarak e\u015fle\u015fmesi gereklili\u011fidir. Ba\u015fka bir s\u0131n\u0131rl\u0131l\u0131k ise interceptor&#8217;lar derleme s\u00fcrecinin bir par\u00e7as\u0131 olarak \u00e7al\u0131\u015ft\u0131klar\u0131 i\u00e7in NuGet \u00fczerinden y\u00fcklenmi\u015f olan harici k\u00fct\u00fcphanelerden gelen metotlara kar\u015f\u0131n yap\u0131lan \u00e7a\u011fr\u0131lar\u0131 y\u00f6nlendirememektedirler. \u00c7\u00fcnk\u00fc haz\u0131r k\u00fct\u00fcphanelerde gelen yap\u0131lar zaten derlendi\u011fi i\u00e7in bu i\u015flem ge\u00e7erli olmayacakt\u0131r. Ve sonuncu olarak da dosya yollar\u0131n\u0131n tam bir path olarak belirtilme ihtiyac\u0131 Linux, macOS veya Windows ortamlar\u0131nda istemsiz olsa da kesinlikle sorunlara neden olabilecektir.<\/p>\n<blockquote><p><em style=\"color:green;\">Interceptor&#8217;lar tam bir AOP \u00e7\u00f6z\u00fcm\u00fc de\u011fildirler.<\/em><\/p><\/blockquote>\n<p>T\u00fcm bunlar\u0131n d\u0131\u015f\u0131nda <code>InterceptsLocation<\/code> attribute&#8217;u ile bildirilen metot \u00e7a\u011fr\u0131lar\u0131n\u0131n sat\u0131r ve kolon bilgilerinin de\u011fi\u015fmesi ihtimali kodun olas\u0131 hatalara her daim gebe oldu\u011funu g\u00f6stermektedir.<\/p>\n<p>Nihai olarak,<br \/>\nInterceptor&#8217;lar sayesinde beklenen o ki, \u00e7o\u011fu programc\u0131 a\u00e7\u0131s\u0131ndan interceptor&#8217;lar do\u011frudan kullan\u0131lmayacak ancak uygulamalar\u0131n daha h\u0131zl\u0131 \u00e7al\u0131\u015fmas\u0131 ve kolayl\u0131kla konu\u015fland\u0131r\u0131lmas\u0131 a\u00e7\u0131s\u0131ndan \u00f6nemli rol oynayacakt\u0131r kanaatindeyiz.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<p>Not : \u00d6rnek \u00e7al\u0131\u015fmay\u0131 a\u015fa\u011f\u0131daki github adresinden indirebilirsiniz.<br \/>\n<a href=\"https:\/\/github.com\/gncyyldz\/CSharp12.Interceptor.Example\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/gncyyldz\/CSharp12.Interceptor.Example<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde .NET 8&#8217;de C# 12 ile birlikte gelen Interceptor \u00f6zelli\u011fini inceliyor olaca\u011f\u0131z. Interceptor Nedir? Neden Bu Yap\u0131lanmaya \u0130htiyac\u0131m\u0131z Vard\u0131r? Interceptor, uygulamadaki herhangi bir metoda yap\u0131lan \u00e7a\u011fr\u0131y\u0131 alternatif ba\u015fka bir metotla de\u011fi\u015ftirmemize ya&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":26539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4912,4939],"tags":[4915,4937,4938,4933,4936,4935,4934],"class_list":["post-26603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-8","category-c-12","tag-net-8","tag-net-8-interceptor","tag-aot-interceptors","tag-c-12","tag-c-12-interceptors","tag-c-interceptors","tag-interceptors"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/26603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=26603"}],"version-history":[{"count":8,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/26603\/revisions"}],"predecessor-version":[{"id":26616,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/26603\/revisions\/26616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/26539"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=26603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=26603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=26603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}