Windows Metro app WCF address configurable

Source: Internet
Author: User

Invoking a WCF service in the Windows Metro app can be done by adding a "service reference". Once the project is published, it cannot be modified. This is not the same as desktop development.

Now we read the WCF address by reading the text.

1. Add the WCF address that you want to refer to.

2, after the addition. Automatically generated Reference.cs inside we can see the private static System.ServiceModel.EndpointAddress getendpointaddress ( Endpointconfiguration endpointconfiguration) This method has the address we added. We know that it is not feasible to read the text in this method. Because this method does not support asynchronous (async).

3. We put the address in the read text into the App.xaml.cs OnLaunched method to read. Then put the read address into the global variable.

The global variable is read in the Reference.cs.

The code under App.xaml.cs:

protected override async void OnLaunched (Launchactivatedeventargs args)
{
var key = await readwcfservice ();
var localsettings = ApplicationData.Current.LocalSettings;
if (LocalSettings.Values.ContainsKey ("Wcfapi"))
localsettings.values["WCFAPI"] = key;
Else
LOCALSETTINGS.VALUES.ADD ("Wcfapi", key);

}

<summary>
Reading configuration Files
</summary>
<returns></returns>

Public async task<string> Readwcfservice ()
{
Try
{
var storagefolder = knownfolders.documentslibrary;
var file = await Storagefolder.getfileasync ("WcfServiceUrl.txt");
var str = await fileio.readtextasync (file);
Return str. Trim ();
}
catch (Exception e)
{
}
return string. Empty;
}

The code under Reference.cs:

private static System.ServiceModel.EndpointAddress getendpointaddress (Endpointconfiguration endpointconfiguration) {
if ((endpointconfiguration = = Endpointconfiguration.basichttpbinding_iservice1)) {

var str = string. Empty;
var localsettings = ApplicationData.Current.LocalSettings;
if (LocalSettings.Values.ContainsKey ("Wcfapi"))
{
str = localsettings.values["Wcfapi"]. ToString ();
}

return new System.ServiceModel.EndpointAddress (str);
}
throw new System.InvalidOperationException (string. Format ("Could not find endpoint with name \ ' {0}\ '.", endpointconfiguration));
}

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.