Home > database >  asp.net wcf lifetime error with context inject
asp.net wcf lifetime error with context inject

Time:10-29

i'm a Python and JS developer and some weeks ago i started to develop in ASP.NET .

I need to implement webservice SOAP on .Net Core , but when i try to use Entity and inject the context from another project on the same solution i've a strange error.

This is my Startup.cs

using System.ServiceModel;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Models;
using SoapCore;
using SoapWebServices.Services;

namespace SoapWebServices
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSoapCore();
            services.TryAddSingleton<IMyTestService, MyTestService>();
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSoapEndpoint<IMyTestService>("/Test.svc", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
        }
    }
}

This is my Service Contract Interface

using OtherProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;

namespace SoapWebServices.Services
{
    [ServiceContract]
    public interface IMyTestService
    {
        [OperationContract]
        string TestMethod(string s);
    }

}

This is my class

using OtherProject.Data;
using OtherProject.Models;
using SoapWebServices.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class MyTestService: IMyTestService
{

    public string TestMethod(string id)
    {
        return "If i receive string build is ok! "   id;
    }
}

Until now is all ok, but if i try to change the OperationContract with

using OtherProject.Data;
using OtherProject.Models;
using SoapWebServices.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class MyTestService: IMyTestService
{
    private MyContext _context;

    public MyTestService(MyContext context)
    {
        _context = context;
    }

    public Info GetInfoById(int id)
    {
      return _context.Info.FirstOrDefault(c => c.Id == id);
    }
}

I receive a strange error on Program.cs during the building

System.AggregateException

Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: SoapWebServices.Services.MyTestService Lifetime: Singleton ImplementationType: MyTestService': Unable to resolve service for type 'OtherProject.Data.MyContext' while attempting to activate 'MyTestService'.)

Regards

CodePudding user response:

Try

public void ConfigureServices(IServiceCollection services)
{
    services.AddSoapCore();
    services.TryAddScoped<IMyTestService, MyTestService>();
    services.AddMvc();
}

CodePudding user response:

Little step forward (i hope).

From here https://stackify.com/soap-net-core/ if i change

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSoapCore();
        services.AddSingleton(new MyTestService());
        services.AddMvc();
    }

i build successfully but, i must define a constructor empty (without Entity, probably i have to reference that )

public MyTestService()
{
}

but service not working and if i try to call via Postman i receive error

No service for type 'SoapWebServices.Services.IMyTestService' has been registered.

Instead from here if i implement SOAP service as middleware

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSoapCore();
        services.TryAddSingleton<IMyTestService>();
        services.AddMvc();
    }

I receive build error Cannot instantiate implementation type 'SoapWebServices.Services.IMyTestService' for service type 'SoapWebServices.Services.IMyTestService'.'

I'm sorry i know that are stupid questions, i'm studying many concept about .Net (Entity, Identity ecc) and now DI too (however Python / Django is more clear and simple than .Net).

Regards

Davide

  • Related