PerCall
ResponseFormat
must be Json
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
AddShipCommand
public sealed class AddShipCommand { public string ShipName { get; set; } }
ShipInfo
object ShipInfo
public sealed class ShipInfo { public Guid Id { get; set; } public string Name { get; set; } }
ShipLocationQuery
query, the result of which should be the ShipLocation
object 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(); } }
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(); } }
private static void ConfigureService() { NeliburRestService.Configure(x => { x.Bind<AddShipCommand, ShipProcessor>(); x.Bind<ShipLocationQuery, ShipProcessor>(); }); }
Source: https://habr.com/ru/post/225117/
All Articles