{"id":959,"date":"2025-01-17T03:01:19","date_gmt":"2025-01-17T11:01:19","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/?p=959"},"modified":"2025-01-17T03:01:19","modified_gmt":"2025-01-17T11:01:19","slug":"net-aspire%e5%b0%8f%e8%b4%b4%e7%a4%ba-%e7%ae%a1%e7%90%86%e5%ae%b9%e5%99%a8%e5%92%8c%e6%95%b0%e6%8d%ae%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/net-aspire%e5%b0%8f%e8%b4%b4%e7%a4%ba-%e7%ae%a1%e7%90%86%e5%ae%b9%e5%99%a8%e5%92%8c%e6%95%b0%e6%8d%ae%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/","title":{"rendered":".NET Aspire\u5c0f\u8d34\u793a &#8211; \u7ba1\u7406\u5bb9\u5668\u548c\u6570\u636e\u751f\u547d\u5468\u671f"},"content":{"rendered":"<p style=\"text-align: center;\"><em><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u672c\u6587\u7ffb\u8bd1\u81ea <a class=\"read-more\" href=\"https:\/\/devblogs.microsoft.com\/dotnet\/author\/jamesmontemagno\" data-bi-area=\"body_author_top\" data-bi-id=\"post_page_body_author_top_name\" data-bi-name=\"James Montemagno\">James Montemagno<\/a>\u00a0\u7684 <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/dotnet-aspire-container-lifetime\/\">.NET Aspire Quick Tip \u2013 Managing Container &amp; Data Lifetime<\/a><\/span><\/em><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">.NET Aspire \u51ed\u501f\u5176\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u5408\u7f16\u6392\u529f\u80fd\u589e\u5f3a\u4e86\u672c\u5730\u5f00\u53d1\u6d41\u7a0b\u3002\u5728 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u96c6\u4e2d\u4f4d\u7f6e\u4e3a\u5e94\u7528\u7a0b\u5e8f\u6307\u5b9a\u6240\u6709\u9879\u76ee\u3001\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4e91\u8d44\u6e90\u548c\u5bb9\u5668\u3002\u5f53\u60a8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u9879\u76ee\u65f6\uff0c.NET Aspire \u5c06\u81ea\u52a8\u8fd0\u884c\u60a8\u7684\u9879\u76ee\u548c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6839\u636e\u9700\u8981\u914d\u7f6e\u4e91\u8d44\u6e90\uff0c\u5e76\u4e0b\u8f7d\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u7684\u5bb9\u5668\u3002<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/whats-new\/dotnet-aspire-9\">.NET Aspire 9<\/a>\u00a0\u6dfb\u52a0\u4e86\u65b0\u529f\u80fd\uff0c\u8ba9\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u7ba1\u7406\u5bb9\u5668\u751f\u547d\u5468\u671f\u7684\u65b9\u5f0f\uff0c\u4ece\u800c\u5728\u4f7f\u7528\u5bb9\u5668\u65f6\u52a0\u5feb\u5f00\u53d1\u901f\u5ea6\u3002<\/span><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>.NET Aspire <\/strong><strong>\u5bb9\u5668<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u672c\u5730 Redis \u5bb9\u5668\u8d44\u6e90\uff0c\u7b49\u5f85\u5176\u53ef\u7528\u540e\uff0c\u4e3a Web \u9879\u76ee\u914d\u7f6e\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">\/\/ Create a distributed application builder given the command line arguments.\r\nvar builder = DistributedApplication.CreateBuilder(args);\r\n\r\n\/\/ Add a Redis server to the application.\r\nvar cache = builder.AddRedis(\"cache\");\r\n\r\n\/\/ Add the frontend project to the application and configure it to use the \r\n\/\/ Redis server, defined as a referenced dependency.\r\nbuilder.AddProject&lt;Projects.MyFrontend&gt;(\"frontend\")\r\n       .WithReference(cache)\r\n       .WaitFor(cache);<\/code><\/pre>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5f53 App Host \u542f\u52a8\u540e\uff0c\u5bf9 <code>AddRedis<\/code> \u7684\u8c03\u7528\u5c06\u4e0b\u8f7d\u76f8\u5e94\u7684 Redis \u955c\u50cf\u3002<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image.png\"><img decoding=\"async\" class=\"alignnone wp-image-965\" src=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image-300x100.png\" alt=\"Image redis image\" width=\"819\" height=\"273\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image-300x100.png 300w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image-1024x340.png 1024w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image-768x255.png 768w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-image.png 1361w\" sizes=\"(max-width: 819px) 100vw, 819px\" \/><\/a><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5b83\u8fd8\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Redis \u5bb9\u5668\u5e76\u81ea\u52a8\u8fd0\u884c\u5b83\u3002<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container.png\"><img decoding=\"async\" class=\"alignnone wp-image-966\" src=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container-300x193.png\" alt=\"Image redis container\" width=\"826\" height=\"531\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container-300x193.png 300w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container-1024x658.png 1024w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container-768x494.png 768w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/redis-container.png 1487w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/a><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5f53\u6211\u4eec\u505c\u6b62\u8c03\u8bd5 App Host \u65f6\uff0c.NET Aspire \u4f1a\u81ea\u52a8\u505c\u6b62\u6240\u6709\u9879\u76ee\uff0c\u540c\u65f6\u505c\u6b62 Redis \u5bb9\u5668\u5e76\u5220\u9664\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u7684\u5173\u8054\u5377\u3002<\/span><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>\u5bb9\u5668\u751f\u547d\u5468\u671f<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u867d\u7136\u4e0a\u8ff0\u65b9\u5f0f\u9002\u7528\u4e8e\u8bb8\u591a\u573a\u666f\uff0c\u4f46\u5982\u679c\u5bb9\u5668\u6ca1\u6709\u4efb\u4f55\u66f4\u6539\uff0c\u60a8\u53ef\u80fd\u4f1a\u5e0c\u671b\u5bb9\u5668\u59cb\u7ec8\u4fdd\u6301\u8fd0\u884c\uff0c\u800c\u4e0d\u53d7 App Host \u72b6\u6001\u7684\u5f71\u54cd\u3002\u8fd9\u65f6\u5c31\u8f6e\u5230\u65b0\u7684 <code>WithLifetime<\/code>\u00a0API \u51fa\u573a\uff0c\u5b83\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u5bb9\u5668\u7684\u751f\u547d\u5468\u671f\u3002\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5c06\u5bb9\u5668\u914d\u7f6e\u4e3a\u542f\u52a8\u5e76\u4fdd\u6301\u8fd0\u884c\u72b6\u6001\uff0c\u4ece\u800c\u52a0\u5feb\u9879\u76ee\u542f\u52a8\u901f\u5ea6\uff0c\u56e0\u4e3a\u5bb9\u5668\u968f\u65f6\u53ef\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u6570\u636e\u5377\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">var builder = DistributedApplication.CreateBuilder(args);\r\n\r\n\/\/ Add a Redis server to the application and set lifetime to persistent\r\nvar cache = builder.AddRedis(\"cache\")\r\n                   .WithLifetime(ContainerLifetime.Persistent);\r\n\r\nbuilder.AddProject&lt;Projects.MyFrontend&gt;(\"frontend\")\r\n       .WithReference(cache)\r\n       .WaitFor(cache);<\/code><\/pre>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u8fd0\u884cApp Host\u65f6\uff0c\u5982\u679c\u672a\u627e\u5230\u5bb9\u5668\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bb9\u5668\u8d44\u6e90\u5e76\u542f\u52a8\u5b83\uff0c\u4f46\u662f\uff0c\u5982\u679c\u627e\u5230\u4e86\u6307\u5b9a\u540d\u79f0\u7684\u5bb9\u5668\u8d44\u6e90\uff0c.NET Aspire \u4f1a\u4f7f\u7528\u8be5\u8d44\u6e90\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8d44\u6e90\u3002\u5f53 App Host \u5173\u95ed\u65f6\uff0c\u5bb9\u5668\u8d44\u6e90\u4e0d\u4f1a\u88ab\u7ec8\u6b62\uff0c\u4ece\u800c\u5141\u8bb8\u60a8\u5728\u591a\u6b21\u8fd0\u884c\u4e2d\u91cd\u590d\u4f7f\u7528\u5b83\uff01\u60a8\u53ef\u4ee5\u5728 .NET Aspire \u4eea\u8868\u677f\u4e0a\u770b\u5230\u4e00\u4e2a\u5c0f\u56fe\u9489\u56fe\u6807\uff0c\u8868\u793a\u8be5\u5bb9\u5668\u5df2\u8bbe\u7f6e\u4e3a<code>\u6301\u4e45\u5316<\/code>\u72b6\u6001\u3002<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-pin.png\"><img decoding=\"async\" class=\"alignnone wp-image-967\" src=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-pin-300x100.png\" alt=\"Image aspire pin\" width=\"375\" height=\"125\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-pin-300x100.png 300w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-pin.png 483w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f53\u8bbe\u7f6e\u4e86 <code>ContainerLifetime.Persistent<\/code> \u65f6\uff0c.NET Aspire \u4f1a\u8003\u8651\u591a\u79cd\u56e0\u7d20\u4ee5\u786e\u5b9a\u662f\u4f7f\u7528\u73b0\u6709\u5bb9\u5668\u8fd8\u662f\u521b\u5efa\u65b0\u5bb9\u5668\u3002.NET Aspire \u9996\u5148\u6839\u636e App Host \u9879\u76ee\u8def\u5f84\u7684\u54c8\u5e0c\u503c\u4e3a\u5bb9\u5668\u751f\u6210\u552f\u4e00\u540d\u79f0\u3002\u8fd9\u610f\u5473\u7740\u6b64\u5bb9\u5668\u53ea\u5bf9\u4e8e\u7279\u5b9a\u7684 App Host \u9879\u76ee\u662f\u6301\u4e45\u7684\uff0c\u5982\u679c\u60a8\u6709\u591a\u4e2a App Host \u9879\u76ee\uff0c\u5e76\u4e0d\u4f1a\u5168\u5c40\u751f\u6548\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>WithContainerName<\/code> \u65b9\u6cd5\u7ed9\u5bb9\u5668\u6307\u5b9a\u4e00\u4e2a\u540d\u79f0\uff0c\u8fd9\u6837\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u552f\u4e00\u7684\u6301\u4e45\u5316\u5bb9\u5668\u3002<\/span><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u9664\u4e86\u5bb9\u5668\u540d\u79f0\u4e4b\u5916\uff0c.NET Aspire \u8fd8\u4f1a\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5bb9\u5668\u955c\u50cf<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u542f\u52a8\u5bb9\u5668\u7684\u547d\u4ee4\u53ca\u5176\u53c2\u6570<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u6570\u636e\u5377\u6302\u8f7d<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u516c\u5f00\u7684\u5bb9\u5668\u7aef\u53e3<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u73af\u5883\u53d8\u91cf<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5bb9\u5668\u91cd\u542f\u7b56\u7565<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">.NET Aspire \u4f1a\u83b7\u53d6\u6240\u6709\u8fd9\u4e9b\u4fe1\u606f\u5e76\u4ece\u4e2d\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684\u54c8\u5e0c\u503c\uff0c\u4ee5\u4fbf\u4e0e\u4efb\u4f55\u73b0\u6709\u7684\u5bb9\u5668\u6570\u636e\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u8fd9\u4e9b\u8bbe\u7f6e\u4e2d\u6709\u4efb\u4f55\u4e0d\u540c\uff0c\u5219\u4e0d\u4f1a\u91cd\u590d\u4f7f\u7528\u73b0\u6709\u5bb9\u5668\uff0c\u800c\u662f\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u5bb9\u5668\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u597d\u5947\u4e3a\u4ec0\u4e48\u4f1a\u521b\u5efa\u65b0\u5bb9\u5668\uff0c\u90a3\u53ef\u80fd\u662f\u56e0\u4e3a\u67d0\u4e9b\u914d\u7f6e\u53d1\u751f\u4e86\u53d8\u5316\u3002\u8fd9\u662f .NET Aspire \u5728\u4e00\u5f00\u59cb\u9488\u5bf9\u6b64\u65b0\u9009\u9879\u91c7\u7528\u7684\u975e\u5e38\u4e25\u683c\u7684\u7b56\u7565\uff0c\u56e2\u961f\u6b63\u5728\u5bfb\u6c42<a href=\"https:\/\/github.com\/dotnet\/aspire\/issues\/6891\">\u6709\u5173\u672a\u6765\u8fed\u4ee3\u7684\u53cd\u9988<\/a>\u3002<\/span><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>\u6301\u4e45\u5316\u6570\u636e<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u73b0\u5728\uff0c\u6211\u4eec\u5728 App Host \u7684\u591a\u6b21\u542f\u52a8\u4e4b\u95f4\u5b9e\u73b0\u4e86\u5bb9\u5668\u7684\u6301\u4e45\u5316\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6211\u4eec\u6b63\u5728\u91cd\u7528\u4e0e\u5176\u5173\u8054\u7684\u6570\u636e\u5377\u3002\u6570\u636e\u5377\u662f\u4fdd\u5b58\u5bb9\u5668\u751f\u6210\u7684\u6570\u636e\u7684\u63a8\u8350\u65b9\u6cd5\uff0c\u5176\u4f18\u70b9\u662f\u53ef\u4ee5\u540c\u65f6\u5b58\u50a8\u6765\u81ea\u591a\u4e2a\u5bb9\u5668\u7684\u6570\u636e\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\uff0c\u5e76\u4e14\u6613\u4e8e\u5907\u4efd\u6216\u8fc1\u79fb\u3002\u56e0\u6b64\uff0c\u5c3d\u7ba1\u6211\u4eec\u786e\u5b9e\u5728\u91cd\u590d\u4f7f\u7528\u6570\u636e\u5377\uff0c\u4f46\u5982\u679c\u8bbe\u7f6e\u53d1\u751f\u4e86\u66f4\u6539\uff0c\u4ecd\u7136\u53ef\u80fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5bb9\u5668\u3002\u901a\u8fc7\u5bf9\u4f7f\u7528\u548c\u91cd\u590d\u4f7f\u7528\u7684\u6570\u636e\u5377\u8fdb\u884c\u66f4\u7cbe\u51c6\u7684\u63a7\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u529f\u80fd\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u671f\u95f4\u7ef4\u62a4 Redis \u5b9e\u4f8b\u4e2d\u7684\u7f13\u5b58\u6570\u636e\u6216\u6d88\u606f\u3002<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5728\u5ef6\u957f\u7684\u5f00\u53d1\u4f1a\u8bdd\u671f\u95f4\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u8fde\u7eed\u6570\u636e\u96c6\u3002<\/span><\/li>\n<li><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5728 Azure Blob \u5b58\u50a8\u6a21\u62df\u5668\u4e2d\u6d4b\u8bd5\u6216\u8c03\u8bd5\u4e0d\u65ad\u53d8\u5316\u7684\u6587\u4ef6\u96c6\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u56e0\u6b64\uff0c\u6211\u4eec\u7528 <code>WithDataVolume<\/code> \u65b9\u6cd5\u544a\u8bc9\u5bb9\u5668\u8d44\u6e90\u8981\u4f7f\u7528\u54ea\u4e2a\u6570\u636e\u5377\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u6839\u636e\u6211\u4eec\u7684\u9879\u76ee\u548c\u8d44\u6e90\u540d\u79f0\u5206\u914d\u4e00\u4e2a\u540d\u79f0\uff1a<code>{appHostProjectName}-{resourceName}-data<\/code>\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u5c06\u8981\u521b\u5efa\u548c\u91cd\u7528\u7684\u540d\u79f0\uff0c\u8fd9\u5728\u6211\u4eec\u6709\u591a\u4e2a App Host \u65f6\u5f88\u6709\u5e2e\u52a9\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">var cache = builder.AddRedis(\"cache\")\r\n        .WithLifetime(ContainerLifetime.Persistent)\r\n        .WithDataVolume(\"myredisdata\");<\/code><\/pre>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u73b0\u5728\uff0c\u5c06\u4e3a\u8be5\u5bb9\u5668\u8d44\u6e90\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e76\u91cd\u590d\u4f7f\u7528\u7684\u6570\u636e\u5377\uff0c\u5e76\u4e14\u5982\u679c\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u521b\u5efa\u4e86\u65b0\u5bb9\u5668\uff0c\u5b83\u4ecd\u5c06\u4f7f\u7528 <code>myredisdata<\/code> \u5377\u3002<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume.png\"><img decoding=\"async\" class=\"alignnone wp-image-968\" src=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume-300x88.png\" alt=\"Image aspire datavolume\" width=\"696\" height=\"204\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume-300x88.png 300w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume-1024x299.png 1024w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume-768x224.png 768w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-datavolume.png 1387w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/a><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u4f7f\u7528\u6570\u636e\u5377\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u7406\u60f3\u7684\u6027\u80fd\u3001\u53ef\u79fb\u690d\u6027\u548c\u5b89\u5168\u6027\u3002\u7136\u800c\uff0c\u6709\u65f6\u60a8\u53ef\u80fd\u9700\u8981\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u8ba1\u7b97\u673a\u4e0a\u7684\u6587\u4ef6\u3002\u5f53\u60a8\u9700\u8981\u5b9e\u65f6\u66f4\u6539\u65f6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u7ed1\u5b9a\u6302\u8f7d\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">var cache = builder.AddRedis(\"cache\")\r\n        .WithLifetime(ContainerLifetime.Persistent)\r\n        .WithDataBindMount(@\"C:\\Redis\\Data\");<\/code><\/pre>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u6570\u636e\u7ed1\u5b9a\u6302\u8f7d\u4f9d\u8d56\u4e8e\u6587\u4ef6\u7cfb\u7edf\u4ee5\u786e\u4fdd\u5bb9\u5668\u91cd\u542f\u540e\u4fdd\u7559 Redis \u6570\u636e\u3002\u5728\u8fd9\u91cc\uff0c\u6570\u636e\u7ed1\u5b9a\u6302\u8f7d\u4f1a\u5728 Windows \u4e0a\u6302\u8f7d\u5230 Redis \u5bb9\u5668\u4e2d\u7684 <code>C:\\Redis\\Data<\/code> \u8def\u5f84\u3002<\/span><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5bf9\u4e8e Redis\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u63a7\u5236\u6570\u636e\u6301\u4e45\u5316\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\u5728\u7279\u5b9a\u65f6\u95f4\u95f4\u9694\u548c\u9608\u503c\u65f6\u5bf9\u6570\u636e\u8fdb\u884c\u5feb\u7167\u4fdd\u5b58\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">var cache = builder.AddRedis(\"cache\")\r\n        .WithLifetime(ContainerLifetime.Persistent)\r\n        .WithDataVolume(\"myredisdata\")\r\n        .WithPersistence(interval: TimeSpan.FromMinutes(5), keysChangedThreshold: 100);<\/code><\/pre>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5728\u8fd9\u91cc\uff0c<code>interval<\/code> \u8868\u793a\u5feb\u7167\u5bfc\u51fa\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\uff0c<code>keysChangedThreshold<\/code> \u8868\u793a\u89e6\u53d1\u5feb\u7167\u6240\u9700\u7684\u952e\u66f4\u6539\u64cd\u4f5c\u6b21\u6570\u3002<\/span><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u4e0d\u540c\u7684\u96c6\u6210\u5bf9 <code>WithDataVolume<\/code> \u548c <code>WithBindMount<\/code> \u6709\u5404\u81ea\u7684\u89c4\u8303\uff0c\u56e0\u6b64\u8bf7\u52a1\u5fc5\u67e5\u9605\u60a8\u6240\u4f7f\u7528\u7684<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/fundamentals\/integrations-overview\">\u96c6\u6210\u7684\u6587\u6863<\/a>\u3002<\/span><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>\u66f4\u597d\u5730\u63a7\u5236\u8d44\u6e90<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u5728 App Host \u4e2d\u5b8c\u6210\u4e86\u6240\u6709\u8bbe\u7f6e\u3001\u6301\u4e45\u5316\u5e76\u51c6\u5907\u5c31\u7eea\u3002\u989d\u5916\u7684\u597d\u6d88\u606f\u662f\uff0c.NET Aspire 9 \u4e2d\u8fd8\u65b0\u589e\u4e86\u76f4\u63a5\u4ece\u4eea\u8868\u677f\u542f\u52a8\u3001\u505c\u6b62\u548c\u91cd\u542f\u8d44\u6e90\uff08\u5305\u62ec\u5bb9\u5668\uff09\u7684\u529f\u80fd\uff01<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-actions.png\"><img decoding=\"async\" class=\"alignnone wp-image-969\" src=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-actions-243x300.png\" alt=\"Image aspire actions\" width=\"340\" height=\"420\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-actions-243x300.png 243w, https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-content\/uploads\/sites\/75\/2025\/01\/aspire-actions.png 407w\" sizes=\"(max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u8fd9\u9879\u529f\u80fd\u975e\u5e38\u68d2\uff0c\u53ef\u4ee5\u8ba9\u60a8\u5728\u4e0d\u79bb\u5f00\u4eea\u8868\u677f\u7684\u60c5\u51b5\u4e0b\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\u7684\u5f39\u6027\u3002<\/span><\/p>\n<h2><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 18pt;\"><strong>\u5347\u7ea7\u5230 .NET Aspire 9<\/strong><\/span><\/h2>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u5728 .NET Aspire 9 \u4e2d\u8fd8\u6709\u66f4\u591a\u529f\u80fd\uff0c\u56e0\u6b64\u8bf7\u52a1\u5fc5\u9605\u8bfb<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/whats-new\/dotnet-aspire-9\">.NET Aspire 9.0 \u4e2d\u7684\u65b0\u589e\u529f\u80fd\u6587\u6863<\/a>\uff0c\u5e76\u901a\u8fc7\u5b8c\u6574\u7684<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/get-started\/upgrade-to-aspire-9\">\u5347\u7ea7\u6307\u5357<\/a>\u5728\u51e0\u5206\u949f\u5185\u8f7b\u677e\u5b8c\u6210\u5347\u7ea7\u3002<\/span><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u6b64\u5916\uff0c\u8fd8\u66f4\u65b0\u4e86\u6709\u5173<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/fundamentals\/app-host-overview?tabs=docker#container-resource-lifecycle\">\u5bb9\u5668\u8d44\u6e90\u751f\u547d\u5468\u671f<\/a>\u3001<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/fundamentals\/persist-data-volumes\">\u4f7f\u7528\u6570\u636e\u5377\u6301\u4e45\u5316\u6570\u636e<\/a>\u548c\u65b0<a href=\"https:\/\/learn.microsoft.com\/dotnet\/aspire\/fundamentals\/dashboard\/explore#resource-actions\">\u4eea\u8868\u677f\u529f\u80fd<\/a>\u7684\u6587\u6863\u3002<\/span><\/p>\n<p><span style=\"font-family: tahoma, arial, helvetica, sans-serif; font-size: 12pt;\">\u6b22\u8fce\u5728\u4e0b\u65b9\u8bc4\u8bba\u4e2d\u544a\u8bc9\u6211\u4eec\u60a8\u5bf9 .NET Aspire 9 \u4e2d\u8fd9\u9879\u65b0\u529f\u80fd\u4ee5\u53ca\u5176\u4ed6\u6240\u6709\u51fa\u8272\u529f\u80fd\u7684\u770b\u6cd5\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7ffb\u8bd1\u81ea James Montemagno\u00a0\u7684 .NET Aspire Quick Tip \u2013 Managing Container &amp; Data Lifetime .NET Aspire \u51ed\u501f\u5176\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u5408\u7f16\u6392\u529f\u80fd\u589e\u5f3a\u4e86\u672c\u5730\u5f00\u53d1\u6d41\u7a0b\u3002\u5728 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u96c6\u4e2d\u4f4d\u7f6e\u4e3a\u5e94\u7528\u7a0b\u5e8f\u6307\u5b9a\u6240\u6709\u9879\u76ee\u3001\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4e91\u8d44\u6e90\u548c\u5bb9\u5668\u3002\u5f53\u60a8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u9879\u76ee\u65f6\uff0c.NET Aspire \u5c06\u81ea\u52a8\u8fd0\u884c\u60a8\u7684\u9879\u76ee\u548c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6839\u636e\u9700\u8981\u914d\u7f6e\u4e91\u8d44\u6e90\uff0c\u5e76\u4e0b\u8f7d\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u7684\u5bb9\u5668\u3002.NET Aspire 9\u00a0\u6dfb\u52a0\u4e86\u65b0\u529f\u80fd\uff0c\u8ba9\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u7ba1\u7406\u5bb9\u5668\u751f\u547d\u5468\u671f\u7684\u65b9\u5f0f\uff0c\u4ece\u800c\u5728\u4f7f\u7528\u5bb9\u5668\u65f6\u52a0\u5feb\u5f00\u53d1\u901f\u5ea6\u3002 .NET Aspire \u5bb9\u5668 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u672c\u5730 Redis \u5bb9\u5668\u8d44\u6e90\uff0c\u7b49\u5f85\u5176\u53ef\u7528\u540e\uff0c\u4e3a Web \u9879\u76ee\u914d\u7f6e\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a \/\/ Create a distributed application builder given the command line arguments. var builder = DistributedApplication.CreateBuilder(args); \/\/ Add a Redis server to [&hellip;]<\/p>\n","protected":false},"author":177361,"featured_media":972,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[59,60,1,74],"tags":[52],"class_list":["post-959","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-net-aspire","category-dotnet-ch","category-containers","tag-net-9"],"acf":[],"blog_post_summary":"<p>\u672c\u6587\u7ffb\u8bd1\u81ea James Montemagno\u00a0\u7684 .NET Aspire Quick Tip \u2013 Managing Container &amp; Data Lifetime .NET Aspire \u51ed\u501f\u5176\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u5408\u7f16\u6392\u529f\u80fd\u589e\u5f3a\u4e86\u672c\u5730\u5f00\u53d1\u6d41\u7a0b\u3002\u5728 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u4e2a\u96c6\u4e2d\u4f4d\u7f6e\u4e3a\u5e94\u7528\u7a0b\u5e8f\u6307\u5b9a\u6240\u6709\u9879\u76ee\u3001\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4e91\u8d44\u6e90\u548c\u5bb9\u5668\u3002\u5f53\u60a8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u9879\u76ee\u65f6\uff0c.NET Aspire \u5c06\u81ea\u52a8\u8fd0\u884c\u60a8\u7684\u9879\u76ee\u548c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u6839\u636e\u9700\u8981\u914d\u7f6e\u4e91\u8d44\u6e90\uff0c\u5e76\u4e0b\u8f7d\u548c\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u7684\u5bb9\u5668\u3002.NET Aspire 9\u00a0\u6dfb\u52a0\u4e86\u65b0\u529f\u80fd\uff0c\u8ba9\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u7ba1\u7406\u5bb9\u5668\u751f\u547d\u5468\u671f\u7684\u65b9\u5f0f\uff0c\u4ece\u800c\u5728\u4f7f\u7528\u5bb9\u5668\u65f6\u52a0\u5feb\u5f00\u53d1\u901f\u5ea6\u3002 .NET Aspire \u5bb9\u5668 \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u7b80\u5355\u7684 .NET Aspire \u5e94\u7528\u7a0b\u5e8f\u4e3b\u673a\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a\u672c\u5730 Redis \u5bb9\u5668\u8d44\u6e90\uff0c\u7b49\u5f85\u5176\u53ef\u7528\u540e\uff0c\u4e3a Web \u9879\u76ee\u914d\u7f6e\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a \/\/ Create a distributed application builder given the command line arguments. var builder = DistributedApplication.CreateBuilder(args); \/\/ Add a Redis server to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/users\/177361"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/comments?post=959"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media\/972"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}