簡體   English   中英

ASP.NET Core Web服務

[英]ASP.NET Core WebService

我是asp.net核心的新手,但是在Web服務上進行注入時遇到了麻煩。

namespace Web.WebServices
{
    public class InstallService : IService
    {
        private SQLContext _context;

        public InstallService(SQLContext context) 
        {
            _context = context;
        }  

        public string Fcn(string msg)
        {
            //Just for testing, the rest of the code is not altering the results
            return string.Join(string.Empty, msg.Reverse());
        }        
    }

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Fcn(string msg);

    }
}

這是ConfigureService方法:

    public void ConfigureServices(IServiceCollection services)
    {            
        services.AddMvc();
        services.AddSingleton(Configuration);
        services.AddDbContext<SQLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLConnection")));
        services.AddSoapExceptionTransformer((ex) => ex.Message);            
        services.AddScoped<IService, InstallService>();

    }

以及配置方法:

app.UseSoapEndpoint<InstallService>("/ServicePath.asmx", new BasicHttpBinding());

現在的問題是,當我嘗試使用Web服務時,出現錯誤:

“ System.ServiceModel.FaultException。非靜態方法需要目標”

而且,如果我刪除注入,則Web服務可以正常工作。

namespace Web.WebServices
{
    public class InstallService : IService
    {
        public string Fcn(string msg)
        {
            return string.Join(string.Empty, msg.Reverse());
        }        
    }

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Fcn(string msg);

    }
}

以及配置服務方法:

   public void ConfigureServices(IServiceCollection services)
    {            
        services.AddMvc();
        services.AddSingleton(Configuration);
        services.AddDbContext<SQLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLConnection")));
        services.AddSoapExceptionTransformer((ex) => ex.Message);            
        services.AddScoped(new InstallService());

    }

但是我需要使用webService將數據保存到SQL Server。

我幾乎可以肯定,錯誤是不對的,但我無法解決。

您的問題似乎之間

public class InstallService : IService
{
    private SQLContext _context;

    public InstallService(SQLContext context) 
    {
        _context = context;
    }  
}

public class InstallService : IService
{
}

你有這個例外。

“ System.ServiceModel.FaultException。非靜態方法需要目標”

可能您沒有獲得SQLContext實例。

請在您的代碼中檢出Configuration.GetConnectionString("SQLConnection"))

如何在.NET Core中讀取連接字符串?

https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

如果我錯了糾正我。 :p

對於使用SoapCore ,請嘗試配置以下服務

            services.AddScoped<IService, InstallService>();

然后使用如下服務:

            app.UseSoapEndpoint<IService>("/ServicePath.asmx", new BasicHttpBinding());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM