Silverlight dynamically sets the WCF Service Endpoint

Source: Internet
Author: User


In the Silverlight project, the client interacts with the server data through the WCF Service by adding a Service Reference on the Silverlight client. In the client project, Silverlight generates a Service Reference named ServiceReference. clientConfig configuration file. The configuration file contains the Endpoint URL of the WCF Service, which directs the Silverlight client service request to the corresponding server service interface. By default, the Endpoint generated by the client is localhost. When an application is published, the address changes with the configuration of the Web server. As the default setting, each Web server configuration switch requires developers to manually modify the WCF Service configuration, and the Silverlight application cannot fully rely on the ServiceReference. ClientConfig configuration. This not only increases the maintenance cost, but also increases the difficulty of code maintenance. First, add the WCF Service to the project. By default, the ServiceReference. ClientConfig file is generated by Silverlight. It can be seen from the configuration file that the Endpoint is directed to the local device. <Configuration> <system. serviceModel> <bindings> <basicHttpBinding> <binding name = "BasicHttpBinding_ProxyService" maxBufferSize = "2147483647" maxcompute edmessagesize = "2147483647"> <security mode = "None"/> </binding> <binding name = "BasicHttpBinding_WidgetService" maxBufferSize = "2147483647" maxcompute edmessagesize = "2147483647"> <security mode = "None"/> </binding> </basicHttpBinding> </binings d> <client> <endpoint address =" http://localhost/WCFTest/ Proxy. svc "binding =" basicHttpBinding "bindingConfiguration =" BasicHttpBinding_ProxyService "contract =" WCFTestProxy. ProxyService "name =" BasicHttpBinding_ProxyService "/> <endpoint address =" http://localhost/WCFTest/ Widget. svc "binding =" basicHttpBinding "bindingConfiguration =" BasicHttpBinding_WidgetService "contract =" WCFTestWidgetService. widgetService "name =" BasicHttpBinding_WidgetService "/> </client> </system. serviceModel> </configuration> to dynamically set the Endpoint, a helper class DynamicEndpointHelper and public class DynamicEndpointHelper {// BaseUrl is the Web server address for service deployment. private const string BaseUrl =" http://localhost/WCFTest/ "; Public static string ResolveEndpointUrl (string endpointUrl, string xapPath) {string baseUrl = xapPath. substring (0, xapPath. indexOf ("ClientBin"); string relativeEndpointUrl = endpointUrl. substring (BaseUrl. length); string dynamicEndpointUrl = baseUrl + relativeEndpointUrl; return dynamicEndpointUrl;} on the Silverlight client, you can use the background code to dynamically set the Endpoint and private WCFTestProxy. proxyServiceClient GetProxyServ Ice () {WCFTestProxy. proxyServiceClient service = new WCFTestProxy. proxyServiceClient (); service. endpoint. address = new EndpointAddress (DynamicEndpointHelper. resolveEndpointUrl (service. endpoint. address. uri. toString (), App. current. host. source. toString (); return service;} after the proxy client service is created, the Endpoint address points to the currently running Web address. It is worth noting that this method can only support applications and services in the same domain. If the services are in different domains, you must configure cross-domain settings to complete dynamic settings.

Related Article

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.