在Windows metro app中调用wcf服务可以通过添加 “服务引用”来实现。一旦项目发布则不可修改。这个和桌面开发不一样。
现在我们通过读取文本的方式来读取wcf地址。
在Reference.cs中读取该全局变量。
app.xaml.cs下的代码:
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
var key = await ReadWCFService();
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey("WCFAPI"))
localSettings.Values["WCFAPI"] = key;
else
localSettings.Values.Add("WCFAPI", key);
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <returns></returns>
public async Task<string> ReadWCFService()
{
try
{
var storageFolder = KnownFolders.DocumentsLibrary;
var file = await storageFolder.GetFileAsync("WcfServiceUrl.txt");
var str = await FileIO.ReadTextAsync(file);
return str.Trim();
}
catch (Exception e)
{
}
return string.Empty;
}
Reference.cs下的代码:
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration) {
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IService1)) {
var str = string.Empty;
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey("WCFAPI"))
{
str = localSettings.Values["WCFAPI"].ToString();
}
return new System.ServiceModel.EndpointAddress(str);
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \‘{0}\‘.", endpointConfiguration));
}
Windows metro app wcf 地址可配置,布布扣,bubuko.com
原文:http://www.cnblogs.com/dingli/p/3805936.html