You will definitely have app.config or web.config as it is required to specify the end point connections
First Create a ConfigHelper to Edit the address
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
public class ConfigHelper
{
private static string NodePath = "//system.serviceModel//client//endpoint";
private ConfigHelper() { }
public static string GetEndpointAddress()
{
return ConfigHelper.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
}
public static void SaveEndpointAddress(string endpointAddress)
{
XmlDocument doc = loadConfigDocument();
XmlNode node = doc.SelectSingleNode(NodePath);
if (node == null)
throw new InvalidOperationException("Error. Could not find endpoint node in config file.");
try
{
node.Attributes["address"].Value = endpointAddress;
doc.Save(getConfigFilePath());
}
catch (Exception e)
{
throw e;
}
}
public static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}
private static string getConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
}
Then create a "Helper Class" that inherits the client class
class WCFHelper : WCFClient
{
public WCFClientHelper()
{
var endpointAddress = Endpoint.Address;
EndpointAddressBuilder newEndpointAddress = new EndpointAddressBuilder(endpointAddress);
newEndpointAddress.Uri = new Uri( ConfigHelper.GetEndpointAddress());
this.Endpoint.Address = newEndpointAddress.ToEndpointAddress();
}
}
Use this helper class whenever you want to use the Service instead of using the client class
WCFHelper client = new WCFHelper();
lstVenue.ItemsSource = client.GetSomething();
client.Close();