using System.Text.Json; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;
namespace ServiceC { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddDapr().AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; } ); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
/// <summary> /// Provides extension methods for <see cref="IMvcBuilder" />. /// </summary> public static class DaprMvcBuilderExtensions { /// <summary> /// Adds Dapr integration for MVC to the provided <see cref="IMvcBuilder" />. /// </summary> /// <param name="builder">The <see cref="IMvcBuilder" />.</param> /// <param name="configureClient">The (optional) <see cref="DaprClientBuilder" /> to use for configuring the DaprClient.</param> /// <returns>The <see cref="IMvcBuilder" /> builder.</returns> public static IMvcBuilder AddDapr(this IMvcBuilder builder, Action<DaprClientBuilder> configureClient = null) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); }
// This pattern prevents registering services multiple times in the case AddDapr is called // by non-user-code. if (builder.Services.Any(s => s.ImplementationType == typeof(DaprMvcMarkerService))) { return builder; }
builder.Services.AddDaprClient(configureClient);
builder.Services.AddSingleton<DaprMvcMarkerService>(); builder.Services.AddSingleton<IApplicationModelProvider, StateEntryApplicationModelProvider>(); builder.Services.Configure<MvcOptions>(options => { options.ModelBinderProviders.Insert(0, new StateEntryModelBinderProvider()); });
[ApiController] public class HelloController : ControllerBase {
private readonly DaprClient daprClient;
public HelloController(DaprClient daprClient) { this.daprClient = daprClient; }
[HttpPost("talk")] public async Task<SomeResponseBody> Talk(SomeRequestBody someRequestBody) { var data = new { Time = DateTime.Now.ToLongDateString(), Id = "This is Service C." }; HTTPExtension httpExtension = new HTTPExtension() { Verb = HTTPVerb.Post }; SomeResponseBody responseBody = await daprClient.InvokeMethodAsync<object, SomeResponseBody>("dotnet-server-c", "talk", data, httpExtension);
class Program { static async Task Main(string[] args) { var jsonOptions = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, };
var client = new DaprClientBuilder() .UseJsonSerializationOptions(jsonOptions) .Build();
var data = new { Time = DateTime.Now.ToLongDateString(), Id="This is Client A" }; HTTPExtension httpExtension = new HTTPExtension() { Verb = HTTPVerb.Post }; while (true) { var a = await client.InvokeMethodAsync<object, SomeResponseBody>("dotnet-server-b", "talk", data, httpExtension); Console.WriteLine(a.Msg); await Task.Delay(5 * 1000); } } }