ASP.NET Coreã«é¢ããèšäºã®æåã®ã·ãªãŒãº
1. Nano Serveräžã®ASP.NET Core ã
2. ã¢ããªã±ãŒã·ã§ã³ã®WebãµãŒãã¹ããã³ããšã³ããäœæããŸã ã
3. ASP.NET Core MVCããã³Visual Studioã䜿çšããæåã®Web APIã®äœæ ã
4. Visual Studioã䜿çšããŠãWebã¢ããªã±ãŒã·ã§ã³ãAzure Application Serviceã«ãããã€ããŸã ã
5. Visual Studio Codeã䜿çšããæåã®Macã¢ããªã±ãŒã·ã§ã³ ã
æ¢å®ã§ã¯ã Azure Service Fabricã¯WebãµãŒãã¹ã®ãããªãã¯ã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããŸããã ã¢ããªã±ãŒã·ã§ã³ãHTTPã¯ã©ã€ã¢ã³ãã«å¯ŸããŠæ©èœãããã«ã¯ããšã³ããªãã€ã³ããšããŠæ©èœããåã ã®ãµãŒãã¹ãšããŒã¿ã亀æããWebãããžã§ã¯ããäœæããå¿ èŠããããŸãã
ãã®èšäºã§ã¯ãã¹ããŒããã«ã«ãŠã³ã¿ãŒãµãŒãã¹çšã®WebãµãŒãã¹ãVisual Studioã¢ããªã±ãŒã·ã§ã³ã«è¿œå ããŸãã Visual Studioã§ã¢ããªã±ãŒã·ã§ã³ãäœæããåºæ¬çãªããã»ã¹ã«æ £ããŠããªãå Žåã¯ã ãã¡ãã«ã¢ã¯ã»ã¹ããŠãã ããã
ASP.NET CoreãµãŒãã¹ãã¢ããªã±ãŒã·ã§ã³ã«è¿œå ãã
ASP.NET Coreã¯ãææ°ã®WebãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãšWeb APIãäœæã§ããã·ã³ãã«ãªã¯ãã¹ãã©ãããã©ãŒã WebéçºããŒã«ã§ãã ASP.NET Web APIãããžã§ã¯ããæ¢åã®ã¢ããªã±ãŒã·ã§ã³ã«è¿œå ããŸãããã
1.ãœãªã¥ãŒã·ã§ã³ãšã¯ã¹ãããŒã©ãŒã§ã¢ããªã±ãŒã·ã§ã³ãããžã§ã¯ããéããç¶æ ã§ã[ ãµãŒãã¹ ]ãå³ã¯ãªãã¯ãã[ è¿œå ]> [æ°ããService Fabric Service ]ãéžæããŸãã
2. [ ãµãŒãã¹ã®äœæ]ããŒãžã§[ ASP.NET Core]ãéžæãããã®ãµãŒãã¹ã®ååãæå®ããŸãã
3.次ã®ããŒãžã¯ãASP.NET Coreãããžã§ã¯ããã³ãã¬ãŒãã»ããã瀺ããŠããŸãã ãããã¯ãService Fabricã¢ããªã±ãŒã·ã§ã³ã®å€éšã§ASP.NET Coreãããžã§ã¯ããäœæãããšãã«äœ¿çšããããã®ãšåããã¿ãŒã³ã§ããããšã«æ³šæããŠãã ããã ãã®ã¬ã€ãã§ã¯ã Web APIã䜿çšããŸã ã ãã ãããããã®ã¡ãœããã䜿çšããŠæ¬æ ŒçãªWebã¢ããªã±ãŒã·ã§ã³ãäœæã§ããŸãã
泚ïŒããã«äœæ¥ãé²ããã«ã¯ã .NET Core 1.0ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸã ã
Web APIãäœæããåŸãã¢ããªã±ãŒã·ã§ã³ã«ã¯2ã€ã®ãµãŒãã¹ããããŸãã ã¢ããªã±ãŒã·ã§ã³ãæ¹è¯ãããšãã«ãåãæ¹æ³ã§æ°ãããµãŒãã¹ãè¿œå ã§ããŸãã ãããã®åãµãŒãã¹ã¯ãç¹å®ã®ããŒãžã§ã³ãå«ããåå¥ã«æŽæ°ã§ããŸãã
泚ïŒASP.NET CoreãµãŒãã¹ã®äœæã®è©³çŽ°ã«ã€ããŠã¯ãASP.NET Coreã®ããã¥ã¡ã³ããåç §ããŠãã ããã
ã¢ããªã±ãŒã·ã§ã³ã®èµ·å
å®è¡ããæé ãããããç解ããããã«ãæ°ããã¢ããªã±ãŒã·ã§ã³ããããã€ããASP.NET Core Web APIãã³ãã¬ãŒãã®äœ¿çšã«é¢é£ããããã©ã«ãã®åäœãèŠãŠã¿ãŸãããã
1.ã¢ããªã±ãŒã·ã§ã³ããããã°ããã«ã¯ãVisual Studioã§F5ãæŒããŸãã
2.é 眮ãå®äºãããšãVisual Studioã¯ASP.NET Web APIãµãŒãã¹ã®ã«ãŒããã£ã¬ã¯ããªïŒããšãã°ã localhost ïŒ33003ïŒã§ãã©ãŠã¶ãŒãèµ·åããŸãã ããŒãçªå·ã¯ä»»æã«å²ãåœãŠãããç°ãªãå ŽåããããŸãã Web APIãã³ãã¬ãŒãã¯ãã«ãŒããã£ã¬ã¯ããªã®ããã©ã«ãã®åäœãæäŸããªãããããã©ãŠã¶ã«ãšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãã
3.ãã©ãŠã¶ã®ã¢ãã¬ã¹ã«ãã¹
/api/values
ãè¿œå ããŸãã ããã«ãããWeb APIãã³ãã¬ãŒãã®ValuesControllerãªããžã§ã¯ãã®
Get
ã¡ãœãããåŒã³åºãããŸãã ãã³ãã¬ãŒãã«ãã£ãŠæäŸãããããã©ã«ãã®å¿çã2è¡ãå«ãJSONé åãè¿ããŸãã
ãã®ã¬ã€ãã®æé ãå®äºãããšãããã©ã«ãå€ã¯ã¹ããŒã¿ã¹ã«ãŠã³ã¿ãŒãµãŒãã¹ã«ãŠã³ã¿ãŒã®æåŸã®å€ã«çœ®ãæããããŸãã
ãµãŒãã¹æ¥ç¶
Service Fabricã䜿çšãããšãReliable Servicesãšèªç±ã«ããåãã§ããŸãã 1ã€ã®ã¢ããªã±ãŒã·ã§ã³ã«ã¯ãTCPçµç±ã§å©çšå¯èœãªãµãŒãã¹ãšãHTTP REST APIçµç±ã§å©çšå¯èœãªãµãŒãã¹ãããã³Webãœã±ããçµç±ã§å©çšå¯èœãªãµãŒãã¹ããããŸãïŒå¯èœãªãªãã·ã§ã³ãšçæ³çãªæ¯çã®éžæã«ã€ããŠã¯ã èšäºãåç §ããŠãã ãã ïŒã ããã§ã¯ãæãåçŽãªã¢ãããŒãã®1ã€ã䜿çšããSDKã§æäŸããã
ServiceProxy/ServiceRemotingListener
ã¯ã©ã¹ã䜿çšããŸãã
ServiceProxy
ïŒãªã¢ãŒãããã·ãŒãžã£ã³ãŒã«ãŸãã¯RPCã«åºã¥ãïŒã䜿çšããã¢ãããŒãã¯ããããªãã¯ãµãŒãã¹ã³ã³ãã©ã¯ããšããŠæ©èœããã€ã³ã¿ãŒãã§ã€ã¹ãå®çŸ©ããŸãã 次ã«ããã®ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠããµãŒãã¹ãšå¯Ÿè©±ããããã®ãããã·ã¯ã©ã¹ãäœæããŸãã
ã€ã³ã¿ãŒãã§ã€ã¹äœæ
ASP.NET Coreãããžã§ã¯ããå«ããã¹ããŒããã«ãµãŒãã¹ãšãã®ã¯ã©ã€ã¢ã³ãéã®ã³ã³ãã©ã¯ããšããŠæ©èœããã€ã³ã¿ãŒãã§ã€ã¹ãäœæããããšããå§ããŸãããã
1.ãœãªã¥ãŒã·ã§ã³ãšã¯ã¹ãããŒã©ãŒã§ããœãªã¥ãŒã·ã§ã³ãå³ã¯ãªãã¯ãã[ è¿œå ]> [æ°ãããããžã§ã¯ã ]ãéžæããŸãã
2.å·ŠåŽã®ããã²ãŒã·ã§ã³ãã€ã³ã§ã[ Visual CïŒ]ãéžæãã ã¯ã©ã¹ã©ã€ãã©ãªãã³ãã¬ãŒããéžæããŸãã .NET Framework 4.5.2ããŒãžã§ã³ãéžæãããŠããããšã確èªããŸã ã
3.
ServiceProxy
ã䜿çšããã€ã³ã¿ãŒãã§ã€ã¹ã«ã€ããŠã¯ãIServiceã€ã³ã¿ãŒãã§ã€ã¹ãã掟çããå¿ èŠããããŸãã ãã®ã€ã³ã¿ãŒãã§ã€ã¹ã¯ãNuGet Service Fabricããã±ãŒãžã®1ã€ã«å«ãŸããŠããŸãã ããã±ãŒãžãè¿œå ããã«ã¯ãã¯ã©ã¹ã©ã€ãã©ãªãããžã§ã¯ããå³ã¯ãªãã¯ãã [NuGetããã±ãŒãžã®ç®¡ç ]ãéžæããŸãã
4. Microsoft.ServiceFabric.Servicesããã±ãŒãžãèŠã€ããŠã€ã³ã¹ããŒã«ããŸãã
5.ã¯ã©ã¹ã©ã€ãã©ãªã§ãåäžã®
GetCountAsync
ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠã€ã³ã¿ãŒãã§ã€ã¹ãäœæããIServiceããã€ã³ã¿ãŒãã§ã€ã¹ãæ¡åŒµããŸãã
namespace MyStatefulService.Interfaces { using Microsoft.ServiceFabric.Services.Remoting; public interface ICounter: IService { Task<long> GetCountAsync(); } }
ã¹ããŒããã«ãµãŒãã¹ã§ã®ã€ã³ã¿ãŒãã§ã€ã¹ã®å®è£
ã€ã³ã¿ãŒãã§ãŒã¹ãå®çŸ©ãããã®ã§ãã¹ããŒããã«ãµãŒãã¹ã«å®è£ ããå¿ èŠããããŸãã
1.ã¹ããŒããã«ãµãŒãã¹ã§ããã®ã€ã³ã¿ãŒãã§ã€ã¹ãå«ãã¯ã©ã¹ã©ã€ãã©ãªãããžã§ã¯ããžã®ãªã³ã¯ãè¿œå ããŸãã
2.
StatefulService
ããç¶æ¿ããã¯ã©ã¹ïŒ
MyStatefulService
ãªã©ïŒã
MyStatefulService
ããããæ¡åŒµããŠ
ICounter
ã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããŸãã
using MyStatefulService.Interfaces; ... public class MyStatefulService : StatefulService, ICounter { // ... }
3.
ICounter
ã€ã³ã¿ãŒãã§ã€ã¹ã§æå®ãããåäžã¡ãœãã
GetCountAsync
ICounter
ãŸãã
public async Task<long> GetCountAsync() { var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary"); using (var tx = this.StateManager.CreateTransaction()) { var result = await myDictionary.TryGetValueAsync(tx, "Counter"); return result.HasValue ? result.Value : 0; } }
ãµãŒãã¹ãªã¢ãŒãã£ã³ã°ãªã¹ããŒã«ããã¹ããŒããã«ãµãŒãã¹ã®æäŸ
ICounter
ã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããåŸãä»ã®ãµãŒãã¹ããç¶æ ãç£èŠããŠãµãŒãã¹ãåŒã³åºãããšãå¯èœã«ããæåŸã®æé ã¯ãéä¿¡ãã£ãã«ãéãããšã§ãã ã¹ããŒããã«ãµãŒãã¹ã®å ŽåãService Fabricã¯
CreateServiceReplicaListeners
ãšåŒã°ãããªãŒããŒã©ã€ãã¡ãœãããæäŸããŸãã ãã®ã¡ãœããã䜿çšãããšããµãŒãã¹ã§äœ¿çšããéä¿¡ã®ã¿ã€ãã«åºã¥ããŠ1ã€ä»¥äžã®éä¿¡ãªã¹ããŒãæå®ã§ããŸãã
泚ïŒã¹ããŒãã¬ã¹ãµãŒãã¹ã®éä¿¡ãã£ãã«ãéãããã®ã¡ãœããã«çžåœãããã®ã¯
CreateServiceInstanceListeners
ã§ãã
ãã®äŸã§ã¯ãæ¢åã®
CreateServiceReplicaListeners
ã¡ãœããã眮ãæããŠã
ServiceProxy
ã䜿çšããŠã¯ã©ã€ã¢ã³ããåŒã³åºãRPCãšã³ããã€ã³ããäœæãã
ServiceRemotingListener
ã€ã³ã¹ã¿ã³ã¹ã䜿çšããŸãã
using Microsoft.ServiceFabric.Services.Remoting.Runtime; ... protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners() { return new List<ServiceReplicaListener>() { new ServiceReplicaListener( (context) => this.CreateServiceRemotingListener(context)) }; }
ServiceProxyã¯ã©ã¹ã䜿çšããŠãµãŒãã¹ãšå¯Ÿè©±ãã
æºåããç¶æ 远跡ãµãŒãã¹ã¯ãä»ã®ãµãŒãã¹ãããã©ãã£ãã¯ãåä¿¡ããæºåãã§ããŠããŸãã ãããã£ãŠãASP.NET WebãµãŒãã¹ãã察話ããã³ãŒããè¿œå ããã ãã§ãã
1. ASP.NETãããžã§ã¯ãã§ã
ICounter
ã€ã³ã¿ãŒãã§ã€ã¹ãå«ãã¯ã©ã¹ã©ã€ãã©ãªãžã®åç §ãè¿œå ããŸãã
2. [ ãã«ã ]ã¡ãã¥ãŒããã æ§æãããŒãžã£ãŒãéããŸãã 次ã®ãããªãã®ã衚瀺ãããã¯ãã§ãã
MyStatefulService.Interfaceã¯ã©ã¹ã©ã€ãã©ãªãããžã§ã¯ãã§ã¯ãä»»æã®CPUãéžæãããŠããããšã«æ³šæããŠãã ããã Service Fabricã§æ£ããåäœããã«ã¯ã64ããããã©ãããã©ãŒã ã䜿çšããå¿ èŠããããŸãã [ãã©ãããã©ãŒã ]ããããããŠã³ã¡ãã¥ãŒãéãã[ æ°èŠ ]ãéžæããŠ64ããããã©ãããã©ãŒã ãæ§æããŸãã
3.以åã«ã¯ã©ã¹ã©ã€ãã©ãªãããžã§ã¯ãã§è¡ã£ãããã«ãMicrosoft.ServiceFabric.Servicesããã±ãŒãžãASP.NETãããžã§ã¯ãã«è¿œå ããŸãã
ServiceProxy
ã¯ã©ã¹ãè¿œå ãããŸãã
4. Controllersãã©ã«ããŒã§ã
ValuesController
ã¯ã©ã¹ãéããŸãã çŸæç¹ã§ã¯ã
Get
ã¡ãœããã¯ããã©ãŠã¶ãŒã§åã«èŠããã®ã«å¯Ÿå¿ãããvalues1ãšvalue2ã®ã€ã³ã©ã€ã³æååé åãè¿ãããšã«æ³šæããŠãã ããã ãã®å®è£ ã次ã®ã³ãŒãã«çœ®ãæããŸãã
using MyStatefulService.Interfaces; using Microsoft.ServiceFabric.Services.Remoting.Client; ... public async Task<IEnumerable<string>> Get() { ICounter counter = ServiceProxy.Create<ICounter>(new Uri("fabric:/MyApplication/MyStatefulService"), new ServicePartitionKey(0)); long count = await counter.GetCountAsync(); return new string[] { count.ToString() }; }
ã³ãŒãã®æåã®è¡ã¯ããŒã§ãã ã¹ããŒããã«ãµãŒãã¹ã«ICounterãããã·ãè¿œå ããã«ã¯ãã»ã¯ã·ã§ã³èå¥åãšãµãŒãã¹åã®2çš®é¡ã®ããŒã¿ãæäŸããå¿ èŠããããŸãã
ããŒãã£ã·ã§ãã³ã°ã䜿çšããŠãç¶æ 远跡ãµãŒãã¹ãã¹ã±ãŒãªã³ã°ã§ããŸããããšãã°ã顧客IDãéµäŸ¿çªå·ãªã©ãæå®ããããŒã«åºã¥ããŠãç¶æ ãç°ãªãã»ã°ã¡ã³ãã«åå²ã§ããŸãã ãã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã¹ããŒããã«ãµãŒãã¹ã«ã¯ããŒãã£ã·ã§ã³ã1ã€ãããªããããããŒã¯é¢ä¿ãããŸããã æå®ãããããŒã¯ãã¹ãŠåãã»ã¯ã·ã§ã³ã«ãªããŸãã ããŒãã£ã·ã§ã³äœæã®è©³çŽ°ã«ã€ããŠã¯ã èšäºãåç §ããŠãã ãã ã
ãµãŒãã¹åã¯ãfabricïŒ/ <application_name> / <service_name>ãšãã圢åŒã®äžæã®ãªãœãŒã¹ã³ãŒãã§ãã
ãã®æ å ±ã䜿çšããŠãService Fabricã¯èŠæ±ã®éä¿¡å ã®ã³ã³ãã¥ãŒã¿ãŒãæ£ç¢ºã«èå¥ã§ããŸãã ãŸãã
ServiceProxy
ã¯ã©ã¹ã¯ãç¶æ ç£èŠãµãŒãã¹ããŒãã£ã·ã§ã³ããã¹ãããã³ã³ãã¥ãŒã¿ãŒã§é害ãçºçããå Žåãäžè²«ããŠæäœãå¥ã®ã³ã³ãã¥ãŒã¿ãŒã«åãæ¿ããŸãã ãã®æœè±¡åã«ãããä»ã®ãµãŒãã¹ãšé£æºããããã®ã¯ã©ã€ã¢ã³ãã³ãŒããç°¡åã«èšè¿°ã§ããŸãã
ãããã·ãµãŒããŒãäœæããããGetCountAsyncã¡ãœãããåŒã³åºããŠçµæãååŸããŸãã
5. F5ããŒãæŒããŠãå€æŽããã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸãã ååãšåæ§ã«ããã©ãŠã¶ãèªåçã«èµ·åããWebãããžã§ã¯ãã®ã«ãŒããã£ã¬ã¯ããªãããŒããããŸãã ãã¹ãapi / valuesããè¿œå ãããšãã«ãŠã³ã¿ãŒã®çŸåšã®æ»ãå€ã衚瀺ãããŸãã
ãã©ãŠã¶ãæŽæ°ããŠãã«ãŠã³ã¿ãŒèªã¿åãå€ã®å€æŽã远跡ããŸãã
ãã³ãã¬ãŒãã§æå®ãããKestrelãšããŠç¥ãããASP.NET Core WebãµãŒããŒã¯ãçŽæ¥ã€ã³ã¿ãŒããããã©ãã£ãã¯ã®åŠçããµããŒãããŠããªãããšãèŠããŠããããšãéèŠã§ãã éçšã·ããªãªã§ã¯ãASP.NET Coreãšã³ããã€ã³ããAPI ManagementãµãŒãã¹ãŸãã¯ã€ã³ã¿ãŒãããã¢ã¯ã»ã¹ãåããä»ã®ã²ãŒããŠã§ã€ã®èåŸã«é 眮ããããšãæ€èšããŠãã ããã IISã§ã®å±éã§ã¯ãService Fabricã¯ãµããŒããããŠããªãããšã«æ³šæããŠãã ããã
被éšè ãšã®ããŒã¿äº€æ
ããã§ã¯ãã¹ããŒããã«ãµãŒãã¹ãšã®éä¿¡ãæäŸããWebã€ã³ã¿ãŒãã§ã€ã¹ã®è¿œå ã«ã€ããŠèª¬æããŸãã ãã ããåæ§ã®ã¢ãã«ã䜿çšããŠã被éšè ãšããŒã¿ã亀æã§ããŸãã ããã«ç°¡åã§ãã
Visual Studioã§ãµããžã§ã¯ããããžã§ã¯ããäœæãããšãã€ã³ã¿ãŒãã§ã€ã¹ãããžã§ã¯ããèªåçã«äœæãããŸãã ãã®ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšããŠãWebãããžã§ã¯ãã«ãµããžã§ã¯ããšå¯Ÿè©±ããããã®ãµããžã§ã¯ãã®ãããã·ãµãŒããŒãäœæã§ããŸãã éä¿¡ãã£ãã«ã¯èªåçã«æäŸãããŸãã ãããã£ãŠã
ServiceRemotingListener
ãµãŒãã¹ã®å Žåã®ããã«ã
ServiceRemotingListener
ãæ§æããããã®ã¢ã¯ã·ã§ã³ãå®è¡ããå¿ èŠã¯ãããŸããã
ããŒã«ã«ã¯ã©ã¹ã¿ã§ã®WebãµãŒãã¹ã®åäœ
ããŒã«ã«ã¯ã©ã¹ã¿ãŒã«å±éãããService Fabricã¯ãéåžžãè€æ°ã®ã³ã³ãã¥ãŒã¿ãŒãå«ãã¯ã©ã¹ã¿ãŒã«å±éã§ããŸãã ãã®ã¢ããªã¯æ£åžžã«åäœããŸãã ããã¯ãããŒã«ã«ã¯ã©ã¹ã¿ãŒãåãã³ã³ãã¥ãŒã¿ãŒäžã«ãã5ããŒãæ§æã§ããããã§ãã
ãã ãã1ã€ã®éèŠãªæ©èœãWebãµãŒãã¹ã«é¢é£ä»ããããŠããŸãã Azureç°å¢ã§æäŸãããŠããããã«ã¯ã©ã¹ã¿ãŒãããŒããã©ã³ãµãŒã«å«ãŸããŠããªãå Žåã¯ãWebãµãŒãã¹ãåã³ã³ãã¥ãŒã¿ãŒã«å±éãããŠããããšã確èªããå¿ èŠããããŸãã ããã¯ãããŒããã©ã³ãµãŒãã³ã³ãã¥ãŒã¿ãŒéã®ãã©ãã£ãã¯ãåçŽã«ã«ãŒãããããã«å¿ èŠã§ãã ãããè¡ãã«ã¯ããµãŒãã¹ã®
InstanceCount
ãã©ã¡ãŒã¿ãŒã«ç¹å¥ãªå€ã-1ããæå®ããŸãã
ããã©ããããWebãµãŒãã¹ãããŒã«ã«ã§å®è¡ããå ŽåããµãŒãã¹ã®ã€ã³ã¹ã¿ã³ã¹ã1ã€ã ãå®è¡ãããŠããããšã確èªããå¿ èŠããããŸãã ããããªããšãåããã¹ãšããŒãã§ãªãã¹ã³ããŠããè€æ°ã®ããã»ã¹ãšç«¶åããŸãã ãããã£ãŠãããŒã«ã«å±éã§ã¯ãWebãµãŒãã¹ã€ã³ã¹ã¿ã³ã¹ã«ãŠã³ã¿ãŒãã1ãã«èšå®ããå¿ èŠããããŸãã
ããŸããŸãªç°å¢ã«ããŸããŸãªå€ãèšå®ããæ¹æ³ã®è©³çŽ°ã«ã€ããŠã¯ã èšäºãåç §ããŠãã ãã ã