多服务地址的Configs在WCF的Silverlight应用程序应用程序、地址、WCF、Configs

2023-09-03 15:28:54 作者:被我吃掉了

我的团队正在构建我们的第一个显著的Silverlight应用程序,使用一个3层架构和WCF。我们在中间层开发了约10个独立的WCF服务,到目前为止,这个数字只会增长。

My team is building our first significant Silverlight application, using a 3 layered architecture and WCF. We have developed about 10 separate WCF services in the middle layer so far, and this number is only going to grow.

通常情况下,presentation层(即Silverlight应用程序)所指向的服务,托管我们的开发服务器上。但是,有些时候我希望它从本地主机访问服务时代 - 即。开发商机。

Generally, the presentation layer (ie. the Silverlight app) is pointing to the services as hosted on our dev server. However, there are times when I want it to access the services from localhost - ie. the developers machine.

有一个简单的方法来改变,其中presentation层正在寻找服务?是否有此选项之间轻松切换某种方式?

Is there an easy way to change where the presentation layer is looking for the services? Is there some way of easily switching between options here?

推荐答案

甚至比更新客户端配置文件??容易

Even easier than updating the client side config file??

您可能会考虑什么做的是有一个单独的文件,你的客户端配置,并创建一个为正常的使用,另一种为开发计算机使用。

What you might consider doing is having your client config in a separate file, and create one for "normal" use, and another one for "dev machine" use.

然后在您的WCF的配置,使用外化配置文件:

Then in your WCF config, use externalized config files:

<system.serviceModel>
  <client configSource="client.normal.config" />
</system.serviceModel>

如果你需要切换到开发计算机使用,用

and if you need to switch to "dev machine" usage, use

<system.serviceModel>
  <client configSource="client.localhost.config" />
</system.serviceModel>

这两个外部化的配置文件,然​​后将是这个样子:

Those two externalized config files would then look something like this:

[client.normal.config]

<?xml version="1.0" encoding="utf-8" ?>
<client>
    <endpoint name="...." address="http://YourServer/Service1" ...... />
    <endpoint name="...." address="http://YourServer/Service2" ...... />
    ....
    <endpoint name="...." address="http://YourServer/ServiceX" ...... />
</client>

[client.localhost.config]

<?xml version="1.0" encoding="utf-8" ?>
<client>
    <endpoint name="...." address="http://localhost/Service1" ...... />
    <endpoint name="...." address="http://localhost/Service2" ...... />
    ....
    <endpoint name="...." address="http://localhost/ServiceX" ...... />
</client>

这样一来,您创建配置文件,一旦正常使用,一次是本地主机使用 - 你可以很容易地在两者之间的基础配置切换

That way, you create your config files once for normal use and once for localhost use - and you can easily switch in the base config between the two.

这是不是一个WCF特定功能 - 这是一个.NET配置功能。 任何配置部分(但不配置节组)可以被外部化到一个单独的* config文件。你可以把WCF配置的其他部分与外部配置文件(但不能外部化整&LT; system.serviceModel&GT; ,因为该节点是一个配置节组 - 不是配置部分)。

This is not a WCF specific feature - it's a .NET configuration feature. Any configuration section (but not configuration section groups) can be externalized into a separate *.config file. You can put other parts of the WCF config into external config files (but you cannot externalize the entire <system.serviceModel> node since that is a configuration section group - not a configuration section).