WCF Soap Web Reference Injection Question

beantownace

Active member
Joined
Feb 15, 2019
Messages
37
Programming Experience
5-10
Hello all,

Quick question not sure what I am doing wrong with this.

I have a NET 8 Web API where I added a ASMX soap reference to the API. I have a worker class where I am going to be calling some of the ASMX reference methods. When I added that it added the interface so for example CustomerSoap and then I have a worked called CustomerWorker I injected it in the constructor as follows:

public class CustomerWorker(CustomerSoap customerSoap) : ICustomerWorker
{
private readonly CustomerSoap _customerSoap = customerSoap;
}

How do I inject this with the program.cs file. I have an AddScoped on this worker in the Program.cs

builder.Services.AddScoped<ICustomerWorker, CustomerWorker>();

It is not liking the CustomerSoap so trying to figure that out. Also I will need to change that ASMX path based on environment dev, prod but just trying to get this running. I can also use credentials as Default Credentials.

Thanks all blanking on this as I don't usually use soap web references.
 
What error are you getting? How are you registering the CustomerSoap with your dependency injection container?
 
error happens in the var app builder.Build()
"some services are not able to be constructed"

That is what I am trying to see how I register this when it is only the interface it seems to add when I add the web reference.

I want to be able to unit test this so mocking that soap service that is why I am trying to inject it but it is the register I am struggling with.

There is also a CustomerSoapClient available as well
 
Last edited:
Wouldn't you just do a similar thing like:
C#:
builder.Services.AddScoped<CustomerSoap>();
OR
C#:
builder.Services.AddScoped<ICustomerSoap, CustomerSoap>();
 
Wouldn't you just do a similar thing like:
C#:
builder.Services.AddScoped<CustomerSoap>();
OR
C#:
builder.Services.AddScoped<ICustomerSoap, CustomerSoap>();
No it does not work where I tried that and it says configuration files are not supported. It must need a specific setup for registering a soap service
 
I've never used WCF so forgive my ignorance. I thought that CustomerSoap was the actual proxy class. Apparently that was the interface based on my re-reading of your original post, as well as, some very quick and light skimming of documentation.

Assuming that in your old code you could do something like this:
C#:
CustomerSoap customer = new CustomerSoapClient();
or
C#:
CustomerSoap customer = new CustomerSoapProxy();

then you should be able to register something like:
C#:
builder.Services.AddScoped<CustomerSoap, CustomerSoapClient>();
or
C#:
builder.Services.AddScoped<CustomerSoap, CustomerSoapProxy>();

What is the exact error that the compiler gives? Or is it a runtime error?
 
I've never used WCF so forgive my ignorance. I thought that CustomerSoap was the actual proxy class. Apparently that was the interface based on my re-reading of your original post, as well as, some very quick and light skimming of documentation.

Assuming that in your old code you could do something like this:
C#:
CustomerSoap customer = new CustomerSoapClient();
or
C#:
CustomerSoap customer = new CustomerSoapProxy();

then you should be able to register something like:
C#:
builder.Services.AddScoped<CustomerSoap, CustomerSoapClient>();
or
C#:
builder.Services.AddScoped<CustomerSoap, CustomerSoapProxy>();

What is the exact error that the compiler gives? Or is it a runtime error?

The error I get if try just using the client is this as I just tried this without scoping anything:

CustomerSoapClient client = new CustomerSoapClient(CustomerSoapClient.EndpointConfiguration.CustomerSoap);

The Http Request is unauthorized with client authentication scheme 'Anonymous' the authentication header from the server was Negotiate, NTLM.

I was then seeing if I could change the type to Windows but not seeing how to do that. For now just trying to get the call to work then how I can set this up in the program.cs to inject to eventually unit test and mock it.
 
As I understand things, you can modify your app.config to change the bindings to use a different authentication scheme.

Or if you control the IIS server hosting your SOAP web service, and you aren't sending anything secure or requires the identity of the caller, you can just add Anonymous to one of the allowed binding for the web app hosting the web service. I don't know if that is sufficient, or if you also need to touch the web.config of the web app hosting the web service to also allow Anonymous connections.
 
As I understand things, you can modify your app.config to change the bindings to use a different authentication scheme.

Or if you control the IIS server hosting your SOAP web service, and you aren't sending anything secure or requires the identity of the caller, you can just add Anonymous to one of the allowed binding for the web app hosting the web service. I don't know if that is sufficient, or if you also need to touch the web.config of the web app hosting the web service to also allow Anonymous connections.

Yeah I looked for this but there is no app config this is a Web API with appsettings.json file etc.
 
Then you'll need to step through that generated proxy code to find out wherr it is getting its binding settings and override it somehow.

I didn't know you could still generate WCF classes in .NET (Core). I thought that WCF had become obsolete and been replaced by gRPC.
 
Back
Top Bottom