ステップ1.アプリケーションを作成する
簡単にするために、WCFサービスはコンソールアプリケーションをホストします。 したがって、新しいコンソールプロジェクトを追加するだけです。
![](https://habrastorage.org/getpro/habr/post_images/ded/de1/67c/dedde167cdd12e84088eb47e70612240.jpg)
ステップ2. Neliburをインストールする
Neliburをインストールする最も簡単な方法は、 NuGetパッケージマネージャーを使用することです。
![](https://habrastorage.org/getpro/habr/post_images/5f8/cc0/ac2/5f8cc0ac2e24483475be7497e6f07b46.jpg)
パッケージマネージャーコンソールでも同じことができます
![](https://habrastorage.org/getpro/habr/post_images/c5a/fb3/b08/c5afb3b0810a03d138f8b67f18e517aa.png)
これで、RESTful WCFメッセージベースのサービスを作成する準備が整いました。
ステップ3. WCFサービスを作成する
たとえば、WCFサービスには次の要件があります。
- InstanceContextModeは
PerCall
なければなりません -
ResponseFormat
はJson
なければなりません
Neliburには、必要な実装JsonServicePerCallが既に含まれています。
そのため、サービスの起動コードを追加します。
internal class Program { private static WebServiceHost _service; private static void Main() { _service = new WebServiceHost(typeof(JsonServicePerCall)); _service.Open(); Console.WriteLine("ShipTrackingService is running"); Console.WriteLine("Press any key to exit\n"); Console.ReadKey(); _service.Close(); } }
構成を規定します
<system.serviceModel> <services> <service name="Nelibur.ServiceModel.Services.Default.JsonServicePerCall"> <host> <baseAddresses> <add baseAddress="http://localhost:9095/ShipTrackingService" /> </baseAddresses> </host> <endpoint binding="webHttpBinding" contract="Nelibur.ServiceModel.Contracts.IJsonService" /> </service> </services> </system.serviceModel>
ビジネスロジックを実装します
私たちのサービスは次のことができるはずです。
- 船を追加
-
ShipId
出荷をShipId
AddShipCommand
作成する
public sealed class AddShipCommand { public string ShipName { get; set; } }
コマンドの結果は
ShipInfo
オブジェクトである必要があります
public sealed class ShipInfo { public Guid Id { get; set; } public string Name { get; set; } }
ShipLocationQuery
クエリを追加します。結果は
ShipLocation
オブジェクトになります
public sealed class ShipLocationQuery { public Guid ShipId { get; set; } } public sealed class ShipLocation { public string Location { get; set; } public Guid ShipId { get; set; } }
ここで、コマンドとリクエストハンドラを作成する必要があります
public sealed class ShipProcessor : IPost<AddShipCommand>, IGet<ShipLocationQuery> { private static readonly Dictionary<Guid, Ship> _ships = new Dictionary<Guid, Ship>(); public object Get(ShipLocationQuery request) { if (_ships.ContainsKey(request.ShipId)) { return new ShipLocation { Location = "Sheldonopolis", ShipId = request.ShipId }; } throw new WebFaultException(HttpStatusCode.BadRequest); } public object Post(AddShipCommand request) { var ship = new Ship(request.ShipName, Guid.NewGuid()); _ships[ship.Id] = ship; return new ShipInfo { Id = ship.Id, Name = ship.Name }; } }
コマンドをバインドし、ハンドラーに要求します
internal class Program { private static WebServiceHost _service; private static void ConfigureService() { NeliburRestService.Configure(x => { x.Bind<AddShipCommand, ShipProcessor>(); x.Bind<ShipLocationQuery, ShipProcessor>(); }); } private static void Main() { ConfigureService(); _service = new WebServiceHost(typeof(JsonServicePerCall)); _service.Open(); Console.WriteLine("ShipTrackingService is running"); Console.WriteLine("Press any key to exit\n"); Console.ReadKey(); _service.Close(); } }
すべて、サービスは終了しました。 お気づきのように、WCFサービスを変更せずに任意の操作を追加できます...
WCFサービスを使用しているクライアント
クライアントとして、次を使用できます。
- フィドラー
- レストコンソール -Google Chrome拡張機能
- JsonServiceClient - Neliburに組み込まれたクライアント
フィドラー
新しい船の追加(POSTリクエストを使用):
![](https://habrastorage.org/getpro/habr/post_images/198/c56/e58/198c56e587dd03fe8f009945bf446358.jpg)
ShipIdによる船舶の受け取り(GETリクエストを使用):
![](https://habrastorage.org/getpro/habr/post_images/640/e1f/700/640e1f700112a505c62c5e6fb92dda1b.jpg)
JsonServiceClient
クライアントを作成するには、別のコンソールアプリケーションを追加します。
クライアントコードは次のとおりです。
internal class Program { private static void Main() { var client = new JsonServiceClient(Settings.Default.ServiceAddress); var shipInfo = client.Post<ShipInfo>(new AddShipCommand { ShipName = "Star" }); Console.WriteLine("The ship has added: {0}", shipInfo); var shipLocation = client.Get<ShipLocation>(new ShipLocationQuery { ShipId = shipInfo.Id }); Console.WriteLine("The ship {0}", shipLocation); Console.ReadKey(); } }
クライアントを起動し、実行の結果を確認します。
サービス側で
![](https://habrastorage.org/getpro/habr/post_images/4b1/f9d/dfd/4b1f9ddfdb268e7561708876a3af03dc.jpg)
クライアント側
![](https://habrastorage.org/getpro/habr/post_images/b2f/10d/eed/b2f10deed743abb758589cca98720e04.jpg)
さらに読む
それだけです
楽しんでいただけましたでしょうか。 記事を読んでくれてありがとう(翻訳)。
ソースはオリジナルからダウンロードできます。
翻訳者から
記事を読むときは、一連のリクエストとハンドラーのコードに注意することをお勧めします。
private static void ConfigureService() { NeliburRestService.Configure(x => { x.Bind<AddShipCommand, ShipProcessor>(); x.Bind<ShipLocationQuery, ShipProcessor>(); }); }
サービスへの新しい機能の追加は、リクエスト/コマンドクラス、ハンドラクラス、およびそれらのバンドルの作成に限定されます。 キュートなハートCQRSを簡単かつ簡単に整理して使用します。 Neliburを使用すると、最小限の労力でこの奇跡を整理できます。 そして、あなたが少し精通しているなら、たくさんのリクエスト\コマンドとハンドラーが自動的に行われ、それから魔法が完了します:)