Set Silverlight on the network Dynamic Access WCF Services are mostly left behind Service Reference S. clientconfig File , Completely Code To achieve dynamic access to WCF. However, this approach has complicated implementation and limitations. In actual use, for security and other reasons Definition Bind , Set a lot of content. Use the wizard in vs to generate Silverlight access to WCF, which can automatically identify the content and generate servicereferences. the clientconfig file allows you to access services in a simple way. However, manual code binding is much more difficult than the vs wizard mode, and once the Service binding changes, code Modification on the Silverlight side is also very troublesome.
We know that the WCF Service is set through three elements of ABC, and the vs wizard can automatically generate three elements of ABC. Program Deployed on the server, the only change is a -- address. Therefore, it is obviously the easiest way to dynamically modify a Based on the automatically generated configuration file.
Program in Development Environment and deployment on the server mainly face two situations:
1. Silverlight always accesses the WCF Service with a fixed server address. For example, the address of one service is http: // my Services . Com/myservice. SVC.
2. The server provides two access methods: public network and lan. the WCF Service and the web program carrying Silverlight are on the same site. The two may be in the same web program, it may also be two different programs. For example, in the LAN, http: // 192.168.1.4/myservices/myservice. SVC and http: // 192.168.1.4/ Web UI/AAA. aspx. In the public network, they are http://mytest.com/myservices/myservice.svcand http://mytest.com/webui/aaa.aspx.
Obviously, the two cases require different processing methods. The following method can be used to handle both cases.
1. Web Application
In the first case, because the configuration files of Silverlight are all packaged in xap files, you can pass the service address for them through the web program that hosts Silverlight, the best place to store this address is the web of the web program. config file. For example, add a key-Value Pair <add key = "wcfserviceaddress" value = "http://myServices.com"/> In the appsettings section of Web. config. Call Silverlight in the aspx page file that carries silverlighgt and add the following content:
<Param name = "initparams" value = 'wcfserviceaddress = <% = system. configuration. configurationmanager. deleetask[ "wcfserviceaddress"] %> '/>
In the second case, you do not need to add content in Web. config.
2. Silverlight end
1) Add the following code in the application_startup method of APP. XAML. CS:
// Read the WCF Service address passed by the web program
VaR slservicepath = E. initparams ["wcfserviceaddress"];
// If there is no value, this is the second case. Then, the address information of the web program is read to adapt to the public network and LAN situations.
If (string. isnullorempty (slservicepath ))
{
VaR TMP = htmlpage. Document. documenturi;
Slservicepath = string. Format ("{0}: // {1 }:{ 2}", TMP. scheme, TMP. Host, TMP. Port );
}
If (slservicepath. Trim (). endswith ("/"))
{
Slservicepath = slservicepath. substring (0, slservicepath. Length-1 );
}
// Save the read WCF address to the resource.
Application. Current. Resources. Add ("wcfserviceaddress", slservicepath );
// Save the read WCF address to the resource.
application. current. resources. add ("wcfserviceaddress", slservicepath);
2) create a public utility class and add a method to it. The Code is as follows:
Public static endpointaddress pro cessserviceaddress (endpointaddress originalendpointaddress)
{< br> var newuri = application. current. resources ["wcfserviceaddress"] + originalendpointaddress. uri. absolutepath;
return New endpointaddress (newuri);
}< br> 3) use the WCF Service:
myserviceclient myservice = new myserviceclient ();
myservice. endpoint. address = utility. processserviceaddress (myservice. endpoint. ad Dress);
// code for calling a service method ......
in this way, Silverlight can easily implement dynamic access to WCF. Even if the content such as the binding of the WCF Service changes, you only need to update the service reference through the vs wizard. The Code does not need to be modified.