0

I´m new to asp.net core, and I´m having trouble with injections on a web service.

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

    }
}

This is the ConfigureService method:

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

    }

And the configure method:

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

Now the problem, when I try to consume the webservice I get the error:

"System.ServiceModel.FaultException. Non-static method requires a target"

And if I remove the injection the webservice works fine.

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

    }
}

And the Configure service method:

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

    }

But I need to use the webService to save data to the SQL Server.

I´m almost sure something wrong is not right, but I can´t figure it out.

2 Answers2

0

Your problem seems difference between

public class InstallService : IService
{
    private SQLContext _context;

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

And

public class InstallService : IService
{
}

You got this exception.

"System.ServiceModel.FaultException. Non-static method requires a target"

Probably you didn't get SQLContext instance.

Please check out Configuration.GetConnectionString("SQLConnection")) in your code.

How to read connection string in .NET Core?

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

Correct me if I'm wrong. :p

Leo Vun
  • 36
  • 5
0

For using SoapCore, try to configure service like

            services.AddScoped<IService, InstallService>();

And then use service like below:

            app.UseSoapEndpoint<IService>("/ServicePath.asmx", new BasicHttpBinding());
Edward
  • 28,296
  • 11
  • 76
  • 121