Dynamic access to Web service addresses in Silverlight-practical tips

Source: Internet
Author: User
Tags silverlight
When using WCF or WS for database operations, you need to have a clear service address, if WCF is the Svc file address. Beginners usually add a reference directly using the Add Service reference of VS. This is indeed a very convenient means, but what kind of trouble will it bring?

First, once the directory structure of the WS file has changed in the development phase, the WS address in SL project must be changed, and this change is not simply the update Service reference, because the address has changed, You have to delete the old reference to add the new reference, which is one.

Second, the big problem comes when project development is done to deploy the server. If you add a reference using the VS Add Service reference at development time, the system automatically generates a Servicereferences.clientconfig configuration file with information such as the address and cache of WS and the length of the connection. It's killing me. This configuration file is packaged into the XAP file with SL project, which means that once the project is deployed to the server, it cannot be changed to the address of WS. This has a very negative impact on the migration of the project and the change of the server IP address.

Here is a way to dynamically obtain a WS address, which does not depend on the Servicereferences.clientconfig file. Although not dependent on configuration files, developers still need to use the VS Add Service reference to add a reference to generate the SL-side operational code. Okay, let's get down to business. The following is the class used to generate WSClient

Copy Code code as follows:

public class Serviceutil
{
public static string Svcpath {set; get;}

<summary>
Get Data Service Path
</summary>
<returns>path</returns>
public static workflowserviceclient Getdynamicclient ()
{
if (Svcpath = null)
throw new Exception ("Wrong SVC path!");

BasicHttpBinding binding = new BasicHttpBinding (Application.Current.Host.Source.Scheme.Equals ("https", stringcomparison.invariantcultureignorecase)? BasicHttpSecurityMode.Transport:BasicHttpSecurityMode.None);
Binding. maxreceivedmessagesize = Int. MaxValue;
Binding. MaxBufferSize = Int. MaxValue;

return new Workflowserviceclient (binding, New EndpointAddress (New Uri (Application.Current.Host.Source, Svcpath));
}
}


The above Serviceutil class has a static string variable to hold the WS file path, and the following Getdynamicclient method combines the host site URI of the SL application with the WS file path to form the complete WS reference URL. This static variable svcpath is usually assigned before using the WS operation. and the specific value can be passed through the site Web.config file appsetting in the field, as to how the SL project to get the parameters of the ASPX page, I wrote in the last essay, we can refer to.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.