Windows metro app wcf address configurable

Source: Internet
Author: User

You can call the wcf Service in the Windows metro app by adding "service reference. Once a project is released, it cannot be modified. This is different from desktop development.

Now we can read the wcf address by reading the text.

1. Add the wcf address to be referenced.

2. After adding the file. In the automatically generated Reference. cs, we can see that the private static System. ServiceModel. EndpointAddress GetEndpointAddress (EndpointConfiguration endpointConfiguration) method contains the address we added. We know that reading text in this method is not feasible. This method does not support async ).

3. Place the address in the read text to the OnLaunched method of App. xaml. cs to read the address. Then place the read address in the global variable.

Read the global variable in Reference. cs.

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>
/// Read the configuration file
/// </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;
}

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 ("cocould not find endpoint with name \ '{0} \'.", endpointConfiguration ));
}

 

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.