Silverlight dynamically set WCF service endpoint

Source: Internet
Author: User
Tags silverlight

2013-02-02 05:57 by Jv9, 1763 reading, 3 Reviews, favorites, compilation

Last December, I received a message from a friend who consulted Silverlight using a WCF service, an issue that has been inaccessible after the deployment of the application, and through several exchanges, it was discovered that in his project, all static URLs were used as endpoint addresses for WCF services, and later changed to dynamic addresses. Problem solving. This article briefly describes how to create a dynamic WCF Endpoint.

The client interacts with server-side data through a WCF service in a Silverlight project by adding service Reference to the Silverlight client. Silverlight generates a configuration file named Servicereference.clientconfig in the client project. The configuration file contains the endpoint URL address for the WCF service, which directs the Silverlight Client service request to the corresponding server-side service interface. By default, the client-generated endpoint address is localhost, and when the app is published, the address changes as the Web server is configured differently.

As a default setting, each time the Web server is configured to switch, the developer has to manually modify the WCF service configuration, and the Silverlight app cannot rely entirely on the servicereference.clientconfig configuration. This not only increases maintenance costs, but also adds difficulty to code maintenance.

By first adding a WCF service to the project, Silverlight generates the Servicereference.clientconfig file by default, as can be seen from the configuration file, where endpoint are pointing locally.

<configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name= "Basichttpbinding_proxyservice" maxbuffersize= "2147483647" Maxreceivedmessagesiz E= "2147483647" > <security mode= "None"/> </binding> <bi nding name= "Basichttpbinding_widgetservice" maxbuffersize= "2147483647" maxreceivedmessagesize= "21474836 "> <security mode=" None "/> </binding> </basichttpbinding                > </bindings> <client> <endpoint address= "Http://localhost/WCFTest/Proxy.svc" binding= "BasicHttpBinding" bindingconfiguration= "Basichttpbinding_proxyservice" contract= "WCF Testproxy.proxyservice "name=" Basichttpbinding_proxyservice "/> <endpoint address=" http://localhost/WCFTe          St/widget.svc "      binding= "BasicHttpBinding" bindingconfiguration= "Basichttpbinding_widgetservice" contract= "WCFTestWid Getservice.widgetservice "name=" Basichttpbinding_widgetservice "/> </client> &LT;/SYSTEM.SERVICEMODEL&G T;</configuration>

In order to implement dynamic settings endpoint, a helper class Dynamicendpointhelper is created below,

public class dynamicendpointhelper{    //BASEURL is the Web server address of the deployment service    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;}    }

In the Silverlight client, the endpoint can be dynamically set through the background code,

Private Wcftestproxy.proxyserviceclient Getproxyservice () {    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 agent client service is created, the endpoint address points to the currently running Web address. It is important to note that this method only supports applications and services in the same domain, and if the service is in a different domain, cross-domain settings must be made to complete the dynamic setting.

Transferred from: http://www.cnblogs.com/jv9/archive/2013/02/02/2889738.html

Silverlight dynamically set WCF service endpoint

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.