Getting error when run CoreWCF application using dotnet.exe

186 Views Asked by At

Platform: ASP.NET ver 7.0 , visual studio 2022

Hi,

I have created new project using CoreWCF framework. I am seeing error when try to run this project .dll using DOTNET.exe. Reported error is enter image description here

I have added endpoint BasicHttpBinding. My confusion is that how to add base address for https to fix error.

It will be great help if anyone can suggest solution of mentioned error.

Thanks in advance, Vijay Singh


This is code of Program.cs

using EDISharedLibCore.AppSetting;
using System.Xml;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using Serilog;
using EDICoreWCFService.Service;
using EDISharedLibCore.AppDbContext;
using EDISharedLibCore.AppDbTable;
using EDICoreWCFService.Interface;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using EDISharedLibCore.Common;
 
var builder = WebApplication.CreateBuilder();
builder.WebHost.ConfigureKestrel((context, options) =>
{
    options.AllowSynchronousIO = true;
});
 
// Add WSDL support
builder.Services.AddServiceModelServices();
//builder.Services.AddServiceModelConfigurationManagerFile("wcf.config");
builder.Services.AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
builder.Services.AddTransient<EDICoreService>(); //for [Injected] HttpResponse httpResponse
 
builder.Services.AddDbContext<AppDbContext>(options =>
{
    var connectionString = builder.Configuration.GetConnectionString("local");
    // options.UseSqlServer(connectionString);
 
    options.UseSqlServer(connectionString,
    sqlServerOptionsAction: sqlOpt =>
    {
        sqlOpt.EnableRetryOnFailure(maxRetryCount: 3,
        maxRetryDelay: TimeSpan.FromSeconds(10),
        errorNumbersToAdd: null);
    });
});
 
// Add Identity
builder.Services
   .AddIdentity<TableUserInfo, IdentityRole<int>>()
   .AddEntityFrameworkStores<AppDbContext>()
   .AddUserStore<UserInfoStore>()
   .AddUserManager<AppUserManager>()
   .AddDefaultTokenProviders();
 
// Asp.NET Core Authentication
builder.Services
        .AddAuthentication(options =>
        {
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
    .AddJwtBearer(options =>
    {
       // options.MapInboundClaims = false;
       // options.IncludeErrorDetails = true;
       // options.SaveToken = true;
       // options.RequireHttpsMetadata = false;
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            //ValidateIssuerSigningKey=false,
            //ValidateSignatureLast=false,
            ValidateLifetime=true,
            RequireSignedTokens = true,
 
            ValidIssuer = builder.Configuration["JWTKey:ValidIssuer"],
            ValidAudience = builder.Configuration["JWTKey:ValidAudience"],
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWTKey:Secret"]??"")),
 
        };
        int bedug = gfunc.ToInt(builder.Configuration["Debug"]??"0");
        if (bedug == 1)
        {
            options.Events = new JwtBearerEvents()
            {
                OnTokenValidated = ctx =>
                {
                    Console.WriteLine();
                    Console.WriteLine("Claims from the access token");
                    if (ctx?.Principal != null)
                    {
                        foreach (var claim in ctx.Principal.Claims)
                        {
                            Console.WriteLine($"{claim.Type} - {claim.Value}");
                        }
                    }
                    Console.WriteLine();
                    return Task.CompletedTask;
                },
                OnMessageReceived = msg =>
                {
                    var token = msg?.Request.Headers.Authorization.ToString();
                    string path = msg?.Request.Path ?? "";
                    if (!string.IsNullOrEmpty(token))
 
                    {
                        Console.WriteLine("Access token");
                        Console.WriteLine($"URL: {path}");
                        Console.WriteLine($"Token: {token}\r\n");
                    }
                    else
                    {
                        Console.WriteLine("Access token");
                        Console.WriteLine("URL: " + path);
                        Console.WriteLine("Token: No access token provided\r\n");
                    }
                    return Task.CompletedTask;
                }
            };
        }
   
    });
 
 
var _logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration).Enrich.FromLogContext()
    .CreateLogger();
builder.Logging.AddSerilog(_logger);
 
AppSettings.connectString = builder.Configuration.GetConnectionString("local")??"";
AppSettings.logFileName = builder.Configuration["LogFileName"]??"";
 
 
var app = builder.Build();
var readerQuoates = new XmlDictionaryReaderQuotas
{
    MaxBytesPerRead = 4096,
    MaxDepth = 32,
    MaxArrayLength = 16384,
    MaxStringContentLength = 16384,
    MaxNameTableCharCount = 16384
};
// Configure an explicit none credential type for WSHttpBinding as it defaults to Windows which requires extra configuration in ASP.NET
//var myWSHttpBinding = new WSHttpBinding(SecurityMode.Transport);
var myWSHttpBinding = new WSHttpBinding
    {
 
        Security = new WSHTTPSecurity
        {
            Mode = SecurityMode.Transport
        },
        MaxReceivedMessageSize = 2147483647,
        MaxBufferPoolSize= 2147483647,
        ReaderQuotas = readerQuoates
    };
 
myWSHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
 
app.UseServiceModel(builder =>
{
    builder.AddService<EDICoreService>((serviceOptions) => { serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true; })
    .AddServiceEndpoint<EDICoreService, IEDICoreService>(new BasicHttpBinding
    {
        Security = new BasicHttpSecurity
        {
            Mode = BasicHttpSecurityMode.Transport
        },
        MaxReceivedMessageSize = 5242880,
        MaxBufferSize = 65536,
        ReaderQuotas = readerQuoates,
        Name = "HttpsBasic"
    }, "")
        .AddServiceEndpoint<EDICoreService, IEDICoreService>(new BasicHttpBinding
        {
            Security = new BasicHttpSecurity
            {
                Mode = BasicHttpSecurityMode.None
            },
            MaxReceivedMessageSize = 5242880,
            MaxBufferSize = 65536,
            ReaderQuotas = readerQuoates,
            Name = "HttpBasic"
        }, ""//)
 
        );
 
});
 
 
 
var serviceMetadataBehavior = app.Services.GetRequiredService<CoreWCF.Description.ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;
serviceMetadataBehavior.HttpsGetEnabled = true;
 
app.Run();
1

There are 1 best solutions below

0
On

I had the same problem and was able to fix it by specifying base address as follows:

builder.AddService<EDICoreService>(
    (serviceOptions) =>
    {
        serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true;
        serviceOptions.BaseAddresses.Add(new Uri("https://mydomainname.com"));
    
});