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