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 ));
}