1

I have a blazor server application to which I want to add a WCF NetTcp listening port. The regular Blazor Server Program.cs file is as follows:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSizeBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddServiceModelServices();

var app builder.Build()

This works as far as it starts the default web page in the browser. Now I want to add a WCF .Net Core "ServiceHost" that listens on address 'net.tcp://localhost:port'. I followed the advice in these two documents (one & two) and changed the above code to the following:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSizeBlazor();
builder.Services.AddSingleton<WeatherForecastService>();

builder.Services.AddServiceModelServices()
                .AddTransient<IContractExchange, ContractExchange>();

var app builder.Build();

app.UseServiceModel(serviceBuilder =>
{
    serviceBuilder.AddService<ContractExchange>(serviceOptions => serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true);
    serviceBuilder.AddServiceEndpoint<ContractExchange, IContractExchange>(new NetTcpBinding(), "net.tcp://127.0.0.1:21701/Endpoint");
    //serviceBuilder.OpenAsync();
});

and now when I start the app, the browser opens, the web page is served, but the server is not listening on the net.tcp port specified? If I try to open the port (commented out line above), I get a System.InvalidOperationException: "The communication object, CoreWCF.Configuration.ServiceBuilder, cannot be modified while it is in the Opened state" error.

Marcin
  • 131
  • 6

1 Answers1

0

As far as I know, the reason for this error is that the error occurred on the server side but you did not catch it or did not convert it into a soap error.

A better approach is to implement the IErrorHandler interface on your service class, which provides a way to globally catch all exceptions when they are emitted. The following article has more info : Implementing IErrorHandler .

Once that's done, you can recreate the client agent and try again.

Jiayao
  • 510
  • 3
  • 7