{"id":2410,"date":"2021-10-05T02:29:58","date_gmt":"2021-10-04T18:29:58","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2410"},"modified":"2022-02-16T17:30:14","modified_gmt":"2022-02-16T09:30:14","slug":"aop-autofac-dynamicproxy","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/10\/05\/aop-autofac-dynamicproxy\/","title":{"rendered":"D-10 AoP ? autofac ? DynamicProxy"},"content":{"rendered":"<h1>Service\u600e\u9ebc\u505a\u5230\u50cfMiddleWare\u4e00\u6a23\u7684\u6771\u897f<\/h1>\n<p>\u5728\u7db2\u9801\u7a0b\u5f0f\u4e2d\u53ef\u4ee5\u900f\u904eMiddleWare\u4f86\u505a\u5230\u95dc\u6ce8\u9ede\u5206\u96e2\uff0c\u4f46\u662f\u90a3\u662f\u5728\u8acb\u6c42\u4e2d\u53ef\u4ee5\u53bb\u6514\u622a\u8ddf\u8655\u7406\u8acb\u6c42\uff0c\u5982\u679c\u4eca\u5929\u8173\u8272\u63db\u6210Service\u4e4b\u985e\u7684\u7a0b\u5f0f\u8a72\u5982\u4f55\u8655\u7406\u5462\uff0c\u9019\u5c31\u662f\u6211\u5011\u4eca\u5929\u8981\u4ecb\u7d39\u7684\u5167\u5bb9AoP\u3002<\/p>\n<p><!--more--><\/p>\n<h2>AoP<\/h2>\n<p>\u300c\u5929\u554a~~~\u6211\u8981\u600e\u9ebc\u8655\u7406\u6bcf\u500b\u65b9\u6cd5\u7684\u8f38\u5165\u8f38\u51fa\u7684log\u963f\u3002\u300d<br \/>\n\u4e00\u9032\u8fa6\u516c\u5ba4\u5c0f\u5149\u5c31\u807d\u5230\u5927\u982d\u7684\u6158\u53eb\u8072\uff0c\u56e0\u70ba\u5927\u982d\u6628\u5929\u5728\u8ffd\u4e00\u500bbug\u8ffd\u5230\u6c92\u6709\u65b9\u5411\uff0c\u6240\u4ee5\u4eca\u5929\u4e00\u5927\u65e9\u5927\u982d\u6c7a\u5b9a\u8981\u5148\u628a\u6bcf\u500b\u65b9\u6cd5\u52a0\u4e0a\u8f38\u5165\u8ddf\u8f38\u51fa\u7684log\u3002<br \/>\n\u300c\u4f60\u77e5\u9053\u751a\u9ebc\u662fAoP\u55ce\u300d<br \/>\n\u807d\u5230\u5927\u982d\u7684\u6158\u53eb\u5f8c\u8001K\u9019\u9ebc\u554f\u984c\uff0c\u9019\u6642\u5927\u982d\u50cf\u627e\u5230\u6551\u661f\u4e00\u822c\u81c9\u4e0a\u9732\u51fa\u4e86\u66d9\u5149\u3002<br \/>\n\u300c\u524d\u8f29\uff0c\u6551\u6551\u6211\u5427\u3002\u300d<br \/>\n\u6240\u4ee5\u4eca\u5929\u5c31\u8ddf\u8457\u5c0f\u5149\u9084\u6709\u5927\u982d\u4e86\u89e3\u4e00\u4e0b\u751a\u9ebc\u662fAoP\u4ee5\u53ca\u5728dotnetcore\u505a\u5230AoP\u3002<\/p>\n<h3>\u751a\u9ebc\u662fAoP<\/h3>\n<p>AoP\u5c31\u662f<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E9%9D%A2%E5%90%91%E5%88%87%E9%9D%A2%E7%9A%84%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1\" title=\"\u5256\u9762\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\" rel=\"nofollow noopener\" target=\"_blank\">\u5256\u9762\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08<\/a>\uff0c\u767d\u8a71\u6587\u5c31\u662f\u5982\u540c\u95dc\u6ce8\u9ede\u5206\u96e2\uff0c\u5c0d\u65bc\u958b\u767c\u7684\u7a0b\u5f0f\u95dc\u6ce8\u65bc\u672c\u8eab\u7684\u908f\u8f2f\u958b\u767c\uff0c\u800c\u5176\u4ed6\u8207\u672c\u8eab\u908f\u8f2f\u7121\u95dc\u7684\u529f\u80fd\u5c31\u5f9e\u7a0b\u5f0f\u4e2d\u62bd\u96e2\uff0c\u7136\u5f8c\u5728\u50cf\u5207\u86cb\u7cd5\u4e00\u6a23\u5207\u958b\u4f86\u628a\u984d\u5916\u7684\u529f\u80fd\u52a0\u5230\u7a0b\u5f0f\u5167\uff0c\u5c31\u5982\u540c\u4e00\u958b\u59cb\u7684\u5927\u982d\u5728\u672c\u8eab\u7684\u7a0b\u5f0f\u5167\u6c92\u6709\u65e5\u8a8c\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u73fe\u5728\u6211\u5011\u5728\u6514\u622a\u539f\u4f86\u7684\u7a0b\u5f0f\u4e26\u5728\u9019\u88e1\u52a0\u5165\u65e5\u8a8c\u7684\u529f\u80fd\u3002\u4e0d\u904e\u8aaa\u90a3\u9ebc\u591a\u5728dotnetcore\u5167\u8a72\u5982\u4f55\u8655\u7406\u5462\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u4ecb\u7d39DynamicProxy\u9019\u500b\u5957\u4ef6\u3002<\/p>\n<h3>DynamicProxy<\/h3>\n<p>\u6240\u4ee5\u6211\u5011\u5728dotnetcore\u4e2d\u5be6\u4f5cAoP\u5c31\u662f\u900f\u904e<a href=\"http:\/\/www.castleproject.org\/projects\/dynamicproxy\/\" title=\"dynamicproxy\" rel=\"nofollow noopener\" target=\"_blank\">dynamicproxy<\/a>\u9019\u500b\u5957\u4ef6\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u6211\u5011\u8aaa\u660e\u5982\u4f55\u900f\u904eautofac\u53cadynamicproxy\u505a\u5230AoP\u3002<\/p>\n<h4>\u74b0\u5883\u8655\u7406<\/h4>\n<p>\u5728\u4ecb\u7d39\u5982\u4f55\u64b0\u5beb\u7a0b\u5f0f\u505a\u5230AoP\u4e4b\u524d\u5148\u8aaa\u660e\u6211\u5011\u9700\u8981\u4f7f\u7528\u5230\u54ea\u4e9b\u5957\u4ef6\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u5148\u5b89\u88dd\u4ee5\u4e0b\u5169\u500b\u5957\u4ef6\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-bash\">dotnet add package Autofac.Extras.DynamicProxy\ndotnet add package Autofac.Extensions.DependencyInjection<\/code><\/pre>\n<h4>\u5be6\u4f5c\u6514\u622a\u8005<\/h4>\n<p>\u5728\u5b89\u88dd\u5b8c\u5957\u4ef6\u4e4b\u5f8c\u6211\u5011\u958b\u59cb\u4f86\u958b\u767cAoP\uff0c\u9996\u5148\u5047\u8a2d\u6211\u5011\u7684\u9700\u6c42\u662f\u958b\u767c\u4e00\u500b\u5bc4\u9001email\u7684\u670d\u52d9\uff0c\u4f46\u662f\u6211\u5011\u9700\u8981\u8f38\u5165\u53ca\u8f38\u51fa\u7684\u53c3\u6578\uff0c\u6240\u4ee5\u6211\u5011\u5148\u5b9a\u7fa9\u4ecb\u9762\u4ee5\u53ca\u5be6\u4f5c\u3002<\/p>\n<pre><code class=\"language-cs\">public interface IEmailService\n{\n     string Send(string message);\n}\n\n[Intercept(typeof(CallLogger))]\npublic class EmailService : IEmailService\n{\n    public string Send(string message)\n    {\n        return message;\n    }\n}<\/code><\/pre>\n<p>\u5728\u5be6\u4f5c\u6642\u6211\u5011\u900f\u904e<code>Intercept<\/code>\u9019\u500b\u5c6c\u6027\u4f86\u6ce8\u5165\u6514\u622a\u8005\uff0c\u63a5\u4e0b\u4f86\u8aaa\u660e\u6514\u622a\u8005\u600e\u9ebc\u5be6\u4f5c\u3002<\/p>\n<pre><code class=\"language-cs\">public class CallLogger : IInterceptor\n{\n    public void Intercept(IInvocation invocation)\n    {\n        Console.WriteLine($&quot;You are invoke {invocation.Method.Name}, args:{JsonSerializer.Serialize(invocation.Arguments)}&quot;);\n        invocation.Proceed();\n        Console.WriteLine($&quot;You are finished {invocation.Method.Name}, result:{JsonSerializer.Serialize(invocation.ReturnValue)}&quot;);\n    }\n}<\/code><\/pre>\n<p>\u4e3b\u8981\u6514\u622a\u8005\u5c31\u662f\u7e7c\u627f<code>IInterceptor<\/code>\u4e26\u5be6\u4f5c<code>public void Intercept(IInvocation invocation)<\/code>\u9019\u500b\u65b9\u6cd5\uff0c\u9019\u908a\u6ce8\u610f<code>invocation.Proceed();<\/code>\u5c31\u662f\u57f7\u884c\u88ab\u6514\u622a\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u5011\u628a\u9700\u8981\u505a\u7684\u9700\u6c42\u5beb\u5728\u90a3\u65b9\u6cd5\u4e4b\u524d\u8ddf\u4e4b\u5f8c\u5373\u53ef\u3002\u6700\u5f8c\u4f86\u8aaa\u660e\u5982\u4f55\u8a3b\u518a\u6514\u622a\u8005\u8ddf\u672c\u4f86\u7684\u7a0b\u5f0f\u3002<\/p>\n<pre><code class=\"language-cs\">builder.Register(c =&gt; new CallLogger());\nbuilder.RegisterType&lt;EmailService&gt;().As&lt;IEmailService&gt;().InstancePerDependency().EnableInterfaceInterceptors();<\/code><\/pre>\n<p>\u63d2\u5165\u6514\u622a\u5668\u7684\u65b9\u6cd5\u6709\u4e0b\u5217\u5169\u7a2e\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>EnableClassInterceptors<\/td>\n<td>\u7522\u751f\u4e00\u500b\u985e\u5225\u7e7c\u627f\u8981\u6514\u622a\u7684\u985e\u5225\uff0c\u6240\u4ee5\u65b9\u6cd5\u8981\u52a0virtual\u624d\u80fd\u6514\u622a<\/td>\n<\/tr>\n<tr>\n<td>EnableInterfaceInterceptors<\/td>\n<td>\u52d5\u614b\u7522\u751f\u4e00\u500b\u4ee3\u7406\u63a5\u53e3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9664\u4e86\u900f\u904eAttribute\u4e4b\u5916\uff0c\u9084\u53ef\u4ee5\u5728\u6ce8\u5165\u6642\u52a0\u4e0a\uff0c\u6240\u4ee5\u6ce8\u5165\u6514\u622a\u5668\u7684\u65b9\u6cd5\u6709\u4e0b\u5217\u5169\u7a2e\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u52a0Attribute<\/td>\n<td>[Intercept(typeof(CallLogger))]<\/td>\n<\/tr>\n<tr>\n<td>\u6ce8\u5165\u6514\u622a\u5668<\/td>\n<td>builder.RegisterType<EmailService>().As<IEmailService>().InterceptedBy(typeof(CallLogger)).InstancePerDependency().EnableInterfaceInterceptors();<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u900f\u904eAoP\u53ef\u4ee5\u628a\u4e00\u822c\u7269\u4ef6\u4e2d\u505a\u5230\u95dc\u6ce8\u9ede\u5206\u96e2\uff0c\u5982\u6b64\u4e0d\u53ea\u5728\u8655\u7406\u8acb\u6c42\u4e2d\uff0c\u751a\u81f3\u65bc\u5728\u8655\u7406\u4e00\u822c\u7684\u51fd\u5f0f\u90fd\u53ef\u4ee5\u5c08\u6ce8\u5728\u8a72\u51fd\u5f0f\u4e3b\u8981\u8655\u7406\u7684\u908f\u8f2f\u4e0a\uff0c\u800c\u5176\u5b83\u975e\u9700\u8981\u95dc\u6ce8\u7684\u908f\u8f2f\u5c31\u62bd\u5230\u6514\u622a\u8005\u4f86\u8655\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Service\u600e\u9ebc\u505a\u5230\u50cfMiddleWare\u4e00\u6a23\u7684\u6771\u897f \u5728\u7db2\u9801\u7a0b\u5f0f\u4e2d\u53ef\u4ee5\u900f\u904eMiddleWare\u4f86\u505a\u5230\u95dc\u6ce8\u9ede\u5206 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/10\/05\/aop-autofac-dynamicproxy\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-10 AoP ? autofac ? DynamicProxy\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,136,73,92],"class_list":["post-2410","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-aop","tag-autofac","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2410"}],"version-history":[{"count":15,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2410\/revisions"}],"predecessor-version":[{"id":2933,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2410\/revisions\/2933"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}