Service implementation object was not Intialization in CoreWcf

1.6k Views Asked by At

I receive the following error message

The service implementation object was not initialized or is not available. at CoreWCF.Dispatcher.TaskMethodInvoker.InvokeAsync(Object instance, Object[] inputs) at CoreWCF.Dispatcher.DispatchOperationRuntime.<>c__DisplayClass71_0.<<InvokeAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at CoreWCF.Dispatcher.DispatchOperationRuntime.InvokeAsync(MessageRpc rpc)

Please find below code for your reference.

Startup.cs

public class Startup
{

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddServiceModelServices().AddServiceModelConfigurationManagerFile("Web.config");
        services.AddServiceModelMetadata();

        services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();

        services.AddLogging(loggingBuilder =>
        {
            loggingBuilder.SetMinimumLevel(LogLevel.Information);
            loggingBuilder.AddApplicationInsights("xxxxxxxxxxx930939xxxx");
        });

        services.AddSingleton<ILoggingContractResolver, LoggingContractResolver>();


        IMapper mapper = SappoApiAutoMapperConfiguration.Configure().CreateMapper();

        services.AddSingleton<IMessageStoreFactory, AzureBlobMessageStoreFactory>();

        services.AddSingleton(mapper);
        services.AddSingleton(typeof(IOutboundSappoLogger<>), typeof(OutboundSappoLogger<>));
        services.AddSingleton<IBlobStorageSettingsHelper, BlobStorageSettingsHelper>();
        services.AddSingleton<IBlobClientFactory, BlobClientFactory>();
        services.AddSingleton<IServiceBusFactory>(ctx =>
        {
            return new ServiceBusFactory();
        });
        services.AddTransient<IShipperAsnService, ShipperAsnService>();
        services.AddHttpContextAccessor();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }


        app.UseServiceModel(serviceBuilder =>
        {
            serviceBuilder.AddService<ShipperAsnService>(serviceOptions =>
            {
                serviceOptions.DebugBehavior.IncludeExceptionDetailInFaults = true;

            }).AddServiceEndpoint<ShipperAsnService, IShipperAsnService>(new BasicHttpBinding(), "/ShipperAsnService.svc");
            serviceBuilder.ConfigureServiceHostBase<ShipperAsnService>(serviceHost =>
            {
                var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
                if (behavior == null)
                {
                    behavior = new ServiceBehaviorAttribute();
                    serviceHost.Description.Behaviors.Add(behavior);
                }
                behavior.InstanceContextMode = InstanceContextMode.Single;
                behavior.ConcurrencyMode = ConcurrencyMode.Multiple;
                behavior.IncludeExceptionDetailInFaults = true;
            });

        });
    }

}

IShipperAsnService.cs

[ServiceContract]
    public interface IShipperAsnService
    {
       [OperationContract]
        [WebInvoke]
        Task<ServiceResponse> ActionAsync(ShipperAsnRequest shipperBrnAsnRequest);
    }

ShipperAsnService.cs

public class ShipperAsnService : BaseShipperService<ShipperAsnRequest, OutboundShipperAsnServiceDto>, IShipperAsnService
{
    public ShipperAsnService(
        IMessageStoreFactory messageStoreFactory,
        IServiceBusFactory serviceBusFactory,
        IMapper mapper,
        IOutboundSappoLogger<ShipperAsnService> log)
        : base(messageStoreFactory, serviceBusFactory, mapper, DestinationApi.SAPPOAsn, log)
    {
    }

    public override async Task<ServiceResponse> ActionAsync(ShipperAsnRequest shipperBrnAsnRequest)
    {
        return await base.ActionAsync(shipperBrnAsnRequest);
    }
}

enter image description here

I am struggling to fix it. I want to resolve this issue.

2

There are 2 best solutions below

0
On

I think you are using CoreWCF.

CoreWCF will try get the WCF service instance first in DI, but Core WCF just try get it by Service Type, It doesn't care the interface of service in this step. So if you add your service as .NET way, in the DI container, you just can use the interface to get service instance, but Core WCF will get null.

Once Core WCF can't get the service instance by service type, it will back to WCF way to use the constructor with no parameters.

See: https://github.com/CoreWCF/CoreWCF/issues/533

1
On

I just had the same issue and you could try to add

services.AddSingleton<ShipperAsnService>();

in the ConfigureServices method. The credit goes to Carlos Pons and this blog entry

It even seems unnecessary to register the interface implementation for the service with this line

services.AddTransient<IShipperAsnService, ShipperAsnService>();

At least in my own case.

If you don't need a Singleton service you can either use AddTransient or leave the registration out alltogether according to this comment.