Changing the address of a service at run time
My Web config at Client Side like this
<client>
<endpoint address="http://192.168.1.7/zfsapi/api.php" binding="basicHttpBinding"
bindingConfiguration="ZfsSoapBinding"开发者_开发知识库 contract="SourceAPI.ZfsSoapPort"
name="ZfsSoapPort" />
</client>
And I chnages my address at run time like this
EndpointAddress epa1 = new EndpointAddress("http://192.168.1.7/zfsapi/api.php");
DemoChangingAddressApi.SourceAPI.ZfsSoapPortClient oservice = new SourceAPI.ZfsSoapPortClient(binding1, epa1);
DemoChangingAddressApi.SourceAPI.ZfsVolume[] v1 = oservice.getVolumeList();
// or instantiate whatever other binding you're using
BasicHttpBinding binding = new BasicHttpBinding();
// define the endpoint address
EndpointAddress epa = new EndpointAddress("http://192.168.1.8/zfsapi/api.php");
// create your WCF client-side proxy based on those settings
DemoChangingAddressApi.SourceAPI.ZfsSoapPortClient oservice1 = new SourceAPI.ZfsSoapPortClient(binding, epa);
DemoChangingAddressApi.SourceAPI.ZfsVolume[] v2 = oservice1.getVolumeList();
when i do this i get error @ DemoChangingAddressApi.SourceAPI.ZfsVolume[] v2 = oservice1.getVolumeList();
Error :
Error in deserializing body of reply message for operation 'getVolumeList'.
how i can change address at runtime of service
if you access the web.config file as a xml you always could in runtime: Something like this: // in the code below I had the requirment of making copies of a website dynamically
// Set the root path of the Web application that contains the
// Web.config file that you want to access.
string configPath = ConfigurationManager.AppSettings["appRoot"] + virtualDirectoryName + "\\";
XmlDocument doc = new XmlDocument();
bool change = false;
string configFile = Path.Combine(configPath, "web.config");
doc.Load(configFile);
//get root element
System.Xml.XmlElement Root = doc.DocumentElement;
//I have created appSettings dictionary that holds key/value pairs that wanted to update in section.
Dictionary<string, object> appSettings = new Dictionary<string, object>();
appSettings.Add("connString", "server=" + serverAddressAppDb +
"; uid=" + uidAppDb + "; pwd=" + pwdAppDb +
"; database=" + virtualDirectoryName + "; min pool size=1; max pool size=100;");
appSettings.Add("applogin", urlApp + virtualDirectoryName + "/login.aspx");
appSettings.Add("appUrl", urlApp + virtualDirectoryName);
appSettings.Add("mailer_completeImagePath", urlApp + virtualDirectoryName + "/backgrounds/");
appSettings.Add("mailer_pathImagesComplete", urlApp + virtualDirectoryName + "/images/");
// Now the code below will go loop through each key/value pair under section and will compare the values in with appSettings dictionary. If it sees values are different, It will update in section.
XmlNode appNode = Root["appSettings"];
foreach (XmlNode node in appNode.ChildNodes)
{
if (node.Attributes != null)
{
try
{
string key = node.Attributes.GetNamedItem("key").Value;
string value = node.Attributes.GetNamedItem("value").Value;
if (appSettings.ContainsKey(key) && value != appSettings[key].ToString())
{
node.Attributes.GetNamedItem("value").Value = appSettings[key].ToString();
change = true;
}
}
catch (Exception e)
{
}
}
}
精彩评论