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.
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.
Is there an easy way to change where the presentation layer is looki开发者_开发问答ng 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.
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.
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).
I follow a method whereby if an address is specified in the ServiceReferences.ClientConfig file then it is used, otherwise i reconstruct the target endpoint based on the address of the host and a known location within the hosting web app, as detailed here.
Another way to do it is to set the target end point urls in your web.config of your web app, and then pass these config settings through to the silverlight control as part of its initParams
. Once you have the target address then you can just construct the end point programmatically as mentioned in the link above.
精彩评论