我是一名 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.MyContext”的服務'我的測驗服務'。)
問候
uj5u.com熱心網友回復:
嘗試
public void ConfigureServices(IServiceCollection services)
{
services.AddSoapCore();
services.TryAddScoped<IMyTestService, MyTestService>();
services.AddMvc();
}
uj5u.com熱心網友回復:
向前邁出一小步(我希望)。
從這里https://stackify.com/soap-net-core/如果我改變
public void ConfigureServices(IServiceCollection services)
{
services.AddSoapCore();
services.AddSingleton(new MyTestService());
services.AddMvc();
}
我成功構建但是,我必須定義一個空的建構式(沒有物體,可能我必須參考它)
public MyTestService()
{
}
但服務不作業,如果我嘗試通過郵遞員打電話,我會收到錯誤
沒有注冊“SoapWebServices.Services.IMyTestService”型別的服務。
如果我將 SOAP 服務實作為中間件,則從這里開始
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 更清晰和簡單)。
問候
戴維德
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/343456.html
