在Windows metro app中调用wcf服务可以通过添加 “服务引用”来实现。一旦项目发布则不可修改。这个和桌面开发不一样。

现在我们通过读取文本的方式来读取wcf地址。

1、添加所需引用的wcf 地址。

2、添加完之后。自动生成的Reference.cs里面我们可以看到 private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)这个方法里面有我们添加的地址。我们知道在这个方法里面去读取文本是不可行的。因为这个方法不支持异步(async)。

3、我们把读取文本中的地址放到 App.xaml.cs 的 OnLaunched 方法中去读取。然后把读到的地址放到全局变量中。

在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 地址可配置的更多相关文章

  1. 第一篇 Windows 8 开发Windows Metro style app环境配置

    半   饱问 题 到 我 这 里 为 止! 第一篇 Windows 8 开发Windows Metro style app环境配置 2012-09-24 08:24 by 半饱, 1289 阅读, 3 ...

  2. 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)

    这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...

  3. C#.NET 操作Windows服务承载WCF

    Windows服务的制作.安装可以参考这篇: C#.NET 操作Windows服务(安装.卸载) - runliuv - 博客园 (cnblogs.com) 本篇会在这个解决方案基础上,继续修改. 一 ...

  4. Windows Phone App Studio 无码开发手机应用

    上周微软发布了一款基于Web的Windows Phone应用开发工具 "Windows Phone App Studio".它与大家熟知Visual Studio的最大不同之处是W ...

  5. [老老实实学WCF] 第二篇 配置WCF

    老老实实学WCF 第二篇 配置WCF 在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Col ...

  6. 老老实实学习WCF[第二篇] 配置wcf

    老老实实学WCF 第二篇 配置WCF 在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Col ...

  7. (转)[老老实实学WCF] 第二篇 配置WCF

    第二篇 配置WCF 在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Collections. ...

  8. (转)[老老实实学WCF] 第二篇 配置WCF

    在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Collections.Generic; u ...

  9. windows下安装MongoDB扩展和配置

    windows下安装MongoDB扩展和配置 1.下载mongoDB扩展,根据当前php版本进行下载 地址如下:http://pecl.php.net/package/mongo 我本地php版本是 ...

随机推荐

  1. 【HDU 5007】Post Robot

    Description DT is a big fan of digital products. He writes posts about technological products almost ...

  2. HTTP各个状态返回值

    转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomcat没有启动起来 504 Gateway Time-out: nginx ...

  3. 【uoj2】 NOI2014—起床困难综合症

    http://uoj.ac/problem/2 (题目链接) 题意 给出n个操作包括And,or,xor,求从0~m中的一个数使得经过这些操作后得到的值最大. Solution 大水题..贪心由高到低 ...

  4. [IOS+PHP Jason格式的发送与解析]

    服务器端PHP文件connect.php: <?php $q = mysql_connect("localhost","root","" ...

  5. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  6. 过滤器在Web开发应用------解决中文乱码

    src/com/Encoding.java package com; import java.io.IOException; import javax.servlet.Filter; import j ...

  7. javaweb学习总结(三十一)——国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  8. Socket入门-获取服务器时间实例

    daytimetcpsrv.c #include <stdio.h> #include <string.h> #include <stdlib.h> #includ ...

  9. Swift语言简介+快速上手

    相关: Xcode 6 beta:https://developer.apple.com/xcode/downloads/ swift语言学习文档英文版:http://pan.baidu.com/s/ ...

  10. Windows下Git Bash中文乱码

    文章转自:http://ideabean.iteye.com/blog/2007367 打开Git Bash 进入目录:$ cd /etc 1. 编辑 gitconfig 文件:$ vi gitcon ...