【问题标题】:asp.net wcf lifetime error with context inject带有上下文注入的asp.net wcf生命周期错误
【发布时间】:2021-12-13 14:54:03
【问题描述】:

我是一名 Python 和 JS 开发人员,几周前我开始使用 ASP.NET 进行开发。

我需要在 .Net Core 上实现 webservice SOAP,但是当我尝试使用 Entity 并在同一解决方案上从另一个项目注入上下文时,我遇到了一个奇怪的错误。

这是我的 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);
        }
    }
}

这是我的服务合同接口

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);
    }

}

这是我的课

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;
    }
}

到目前为止一切正常,但如果我尝试更改 OperationContract

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);
    }
}

在构建过程中,我在 Program.cs 上收到一个奇怪的错误

System.AggregateException

消息=无法构建某些服务(验证服务描述符“ServiceType:SoapWebServices.Services.MyTestService Lifetime:Singleton ImplementationType:MyTestService”时出错:无法解析“OtherProject.Data”类型的服务。尝试激活“MyTestService”时的 MyContext'。)

问候

【问题讨论】:

  • 该错误告诉您不能将作用域对象注入单例,因为该作用域对象将永远存在。你应该考虑注入一个上下文工厂,这样当你在单例服务中需要一个新的上下文时,你可以得到一个新的上下文。

标签: c# asp.net-core wcf service


【解决方案1】:

试试

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

【讨论】:

  • 这真的只是隐藏问题
  • 不工作,同样的错误
【解决方案2】:

向前迈出一小步(我希望)。

如果我改变,从这里https://stackify.com/soap-net-core/

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

我构建成功但是,我必须定义一个空的构造函数(没有实体,可能我必须引用它)

public MyTestService()
{
}

但服务无法正常工作,如果我尝试通过 Postman 拨打电话,则会收到错误消息

尚未注册“SoapWebServices.Services.IMyTestService”类型的服务。

如果我将 SOAP 服务实现为中间件,则从 here 代替

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

我收到构建错误 无法为服务类型“SoapWebServices.Services.IMyTestService”实例化实现类型“SoapWebServices.Services.IMyTestService”。

很抱歉,我知道这是愚蠢的问题,我正在研究很多关于 .Net(实体、身份 ecc)和现在 DI 的概念(但是 Python / Django 比 .Net 更清晰和简单)。

问候

大卫

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
  • 好的,我解决了!原因是对 .NET Core 的了解不足。在我的解决方案中,我有 2 个项目,并且在我在第二个子项目中引用的帖子中引用了第一个项目的服务。我认为 DI 可以导入 DbContext 对服务器来说太必要了,但我知道如果导入服务我必须在开始时也导入 DbContext。感谢您的耐心等待
猜你喜欢
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多