0

I have an issue. My boss has setup a test SOAP web service that he wants use to consume in our API. Originally we only had one, so it was straight forward enough. Now we have two and they are on different endpoints, so I created 2 Web References that look like this:

http://ssd-001/tradeportal/webservices.asmx?wsdl

http://ssd-001/testtradeportal/webservices.asmx?wsdl

We are using autofac and I was able to register the service like this:

builder.RegisterType<webServices>().As<webServices>().InstancePerDependency(); 

I was going to create a factory class to switch between the two, but the issue I have is that even though they are both a webServices class, they are on different name spaces and do not have an interface or anything. The only thing I could find was that they both inherit from System.Web.Services.Protocols.SoapHttpClientProtocol, but registering that would lose any of the public methods which I need.

Does anyone know or can think of any suggestions that might help me get around this issue? Basically I want one class to switch between the two depending on if we are live or not, we currently have a flag in a config file for that.

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • 1
    If you need different implementations (because of the namespace issue), create another layer of abstraction. But why do they have different namespaces? In any case, what you can do is create one class that wraps around one service, another class around the other one, both classes implement a common, new, interface, and you use this new interface in your solution instead of the actual web service interfaces. – Lasse V. Karlsen May 09 '18 at 14:06
  • If the different end points refer to services with the same api, you don't need another service reference, you can just add an end point to the config file and select to which end point you want to connect in code. – Zohar Peled May 09 '18 at 14:15

1 Answers1

1

As @Zohar mentioned, the web services are the same; it's just the URL that is different. So I added an entry to my web.config file and then created a factory class like this:

public static class TradePortalFactory
{
    public static webServices Create(CormarConfig config) => new webServices { Url = config.TradePortalUrl };
}

Then I could change my autofac register to this:

builder.Register(m => TradePortalFactory.Create(m.Resolve<CormarConfig>())).As<webServices>().InstancePerDependency();
r3plica
  • 13,017
  • 23
  • 128
  • 290