Dynamic API Endpoint URL

Hi All,
Sometimes you may need to dynamically change the URL of the API endpoint. In these cases standard way with putting URL in app.config file does now work properly, so we need another way.

Here I want to share with you one of the good options that I used in some API integrations here URL needs to be configured in the sessions screen:

public static Usage()
{
	using (DefaultSoapClient client = new DefaultSoapClient(CreateBinding(), CreateEndpoint()))
	{
		//...
	}
}

public const String URL = "http://sm/Summit/";
public const String ENDPOINT = "Default";
public const String VERSION = "18.200.001";
public static System.ServiceModel.EndpointAddress CreateEndpoint()
{
	string url = URL;
	if (url != null && url[url.Length - 1] != '/') url += "/";
	string endpoint = ENDPOINT;
	if (endpoint != null && endpoint[endpoint.Length - 1] != '/') endpoint += "/";

	Uri uri = new Uri(new Uri(new Uri(new Uri(url), "entity/"), endpoint), VERSION);
	return new System.ServiceModel.EndpointAddress(uri.ToString());
}
public static System.ServiceModel.Channels.Binding CreateBinding()
{
	if (URL.StartsWith("https://"))
	{
		return new System.ServiceModel.BasicHttpsBinding()
		{
			AllowCookies = true,
			MaxReceivedMessageSize = 6553600,
			SendTimeout = TimeSpan.FromMinutes(5),
			ReceiveTimeout = TimeSpan.FromMinutes(5),
			Security = new System.ServiceModel.BasicHttpsSecurity()
			{ 
				Mode = System.ServiceModel.BasicHttpsSecurityMode.Transport },
			};
	}
	else
	{
		return new System.ServiceModel.BasicHttpBinding()
		{
			AllowCookies = true,
			MaxReceivedMessageSize = 6553600,
			SendTimeout = TimeSpan.FromMinutes(5),
			ReceiveTimeout = TimeSpan.FromMinutes(5),
			//Security = new System.ServiceModel.BasicHttpSecurity() 
			//{ 
			//	Mode = System.ServiceModel.BasicHttpSecurityMode.Transport 
			//},
		};
	}
}

This approach also helped me previously to solve issue “Could not find endpoint element with name ‘<Name>’ and Contract ‘<Name>’ in the ServiceModel client configuration section. …”

Could not find endpoint element with name

In general this error can be solved by copying app.config file with dll or embedding configurations, but it is much more flexible solution to make endpoint dynamic.

Hope it help!

3 Replies to “Dynamic API Endpoint URL”

  1. Hi Sergey,

    Your last statement: “In general this error can be solved by copying app.config file with dll or embedding configurations, but it is much more flexible solution to make endpoint dynamic.”

    I’m trying to refactor an old customization project in Acumatica. How do one get the web.config file with the the dll file?

    1. Hi Arrie, not web.config, but app.config.
      If you use web services endpoint, you will have an extra file with the dll – PX.Commerce.Core.dll.config

  2. Hi Sergey,

    Thank you for the post. Your advice worked fine on http from the first shot!

Leave a Reply

Your email address will not be published. Required fields are marked *