嗯,听起来这里有两个问题:
- 如何在运行时更新 app.config(这是可能的,但不是自动的);并且,
- 如何在运行时根据 app.config 设置动态设置绑定和端点。
要在运行时更新您的 app.config,请致电 RefreshSection。因此,您可能希望将该调用连接到客户端按钮单击事件。
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["foo"]);
对于在运行时动态设置绑定和端点信息,这绝对是可能的。我根据MSDN Calculator Service 示例将一个简单的示例放在一起。 (为了简单起见,我使用 BasicHttpBinding 而不是 WSHttpBinding。)更新这是我对示例服务所做的唯一更改:
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new BasicHttpBinding(),
"CalculatorService");
我采取的步骤是:
- 从 MSDN 代码示例创建服务并运行它(以便服务端点正在监听);
- 创建了一个基本的 C# 控制台应用程序并向我的服务添加了一个服务引用,以便它可以在我的客户端控制台应用程序中创建客户端引用类。 (它还创建了一个 app.config,但我没有使用它创建的任何 app.config 设置。);
- 编写了以下代码,用于设置我的自动生成的客户端代码将用于连接到服务的绑定和端点地址:
*
static void Main(string[] args) {
//refresh the appSettings section
ConfigurationManager.RefreshSection("appSettings");
//this could come from app.configs appSettings (value = "http://localhost:8000/ServiceModelSamples/Service/CalculatorService")
Uri calcService = new Uri(ConfigurationManager.AppSettings["uri"]);
Binding calcBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
EndpointAddress calcEndpoint = new EndpointAddress(calcService.AbsoluteUri);
CalculatorClient calcClient = new CalculatorClient(calcBinding, calcEndpoint);
double sum = calcClient.Add(10, 20);
double difference = calcClient.Subtract(sum, 10);
Console.WriteLine("10 + 20 = {0}", sum.ToString());
Console.WriteLine("{0} - 10 = {1}", sum.ToString(), difference.ToString());
Console.ReadLine();
}
应该这样做。因此,您可以从配置文件(或用户条目)中读取 Binding 和 EndpointAddress 构造函数的参数,并且可以根据需要设置其他 Binding 和 EndpointAddress 属性。
希望这会有所帮助。如果还有其他问题,请告诉我,我会相应地更新我的答案。
更新 2(现在使用 WSHttpBinding!!)
我对此进行了更新,以包括使用 WSHttpBinding(具有消息级安全性)作为第二个示例。使用 WCF 处理安全性有很多不同的方法,MSDN 有一个很好的指南,可以根据您的场景相应地配置您的安全性。这是该页面的link。
所以我更新的例子和上面基本一样,只是客户端创建了一个WSHttpBinding而不是BasicHttpBinding,并指定了消息级别的安全性作为SecurityMode。
static void Main(string[] args)
{
//refresh the appSettings section
ConfigurationManager.RefreshSection("appSettings");
//this could come from app.configs appSettings (value = "http://localhost:8000/ServiceModelSamples/Service/CalculatorService")
Uri calcService = new Uri(ConfigurationManager.AppSettings["uri"]);
//create the WsHttpBinding and set some security settings for the transport...
WSHttpBinding calcBinding = new WSHttpBinding(SecurityMode.Message);
EndpointAddress calcEndpoint = new EndpointAddress(calcService.AbsoluteUri);
CalculatorClient calcClient = new CalculatorClient(calcBinding, calcEndpoint);
double sum = calcClient.Add(10, 20);
double difference = calcClient.Subtract(sum, 10);
Console.WriteLine("10 + 20 = {0}", sum.ToString());
Console.WriteLine("{0} - 10 = {1}", sum.ToString(), difference.ToString());
Console.ReadLine();
}
我在服务器上所做的唯一区别是我在添加服务端点时指定了 WSHttpBinding。同样,我选择了绑定默认值,但上面的 MSDN 链接将描述如何根据您的安全需求配置服务器。
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");
我希望这会有所帮助!请记住,您可以在 WCF 配置中执行的任何操作都可以在代码中执行。配置设置和代码之间存在 1:1 的关系(基本上,配置中的所有内容都会转换为您可以使用的某个 WCF 类)。
祝你好运!如果还有其他问题,请告诉我。