Accessing a SOAP service from .NET MVC Controller with a Static IP
Note: This example assumes your xlroutes_URL environment variable is set and contains your unique connection string.
Here we show how to use XLRoutes to access a SOAP service from a .NET MVC web controller. This example should also apply to other WebRequests made from an MVC controller.
A common use case is from your MVC controller you want to access an IP-whitelisted service that uses SOAP but the web service object does not have a “proxy” property that allows you to specify the XLRoutes proxy service.
Solution: Create a proxy object and use it as a property of the WebRequest MVC class, which will send all following requests via the proxy.
The solution is split in to two parts, the configuration in your web.config (which on Azure can be done from your management console) and the MVC Controller itself.
MVC Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bank.Webservices; // fictitious namespace name for your webservice's reference
using System.Diagnostics;
using System.Xml;
using System.Net;
using System.Web.Configuration;
public class PaymentController : Controller
{
[HttpPost]
// this is usually a form result, so attribute of action is HttpPost
public ActionResult Index(string anyTokenYouUseWithYourBank)
{
string xlroutesProxyAddress = WebConfigurationManager.AppSettings["xlroutes_Proxy_URI"];
// to get the Proxy Uri from Web.config
string xlroutesUser = WebConfigurationManager.AppSettings["xlroutes_User"];
// to get the Proxy User from Web.config
string xlroutesPassword = WebConfigurationManager.AppSettings["xlroutes_Password"];
// to get the Proxy Password from Web.config
var proxy = new WebProxy(xlroutesProxyAddress);
// we create a new proxy object
proxy.Credentials = new NetworkCredential(xlroutesUser, xlroutesPassword);
// we give the proxy the XLRoutes credentials
WebRequest.DefaultWebProxy = proxy;
// thanks to WebRequest, from here on every request of this controller will pass through the proxy
var soapClientObject = new WSBankSoapClient(); //fictitious name for the banks's web service
var result = soapClientObject .GrabSomethingOnTheNet(anyTokenYouUseWithYourBank);
// now our WS call goes passes Proxy and in the variable result we have the web service's result
// we use the result here
return Redirect("http://newpage");
}
}
Configuration (web.config)
<!-- keys for XLRoutes-->
<add key="xlroutes_Proxy_URI" value="http://yourxlroutesUser:yourxlroutesPassword@eu-west-1-babbage.getstatica.io:9293"/>
<add key="xlroutes_User" value="yourxlroutesUser"/>
<add key="xlroutes_Password" value="yourxlroutesPassword"/>
<!-- end of keys for XLRoutes-->
...
</appSettings>
This solution was kindly contributed by Riccardo Moschetti, a Microsoft MCSD Web Applications specialist.