Export (0) Print
Expand All

Accessing WCF Services with a Windows Store Client App

.NET Framework 4.5

Windows 8 introduces a new type of application called Windows Store applications. These applications are designed around a touch screen interface. .NET Framework 4.5 enables Windows Store applications to call WCF services.

A subset of WCF functionality is available from within a Windows Store application, see the following sections for more details.

Important note Important

Use the WinRT syndication APIs instead of those exposed by WCF. For more information see, WinRT Syndication API

Caution note Caution

Using Add Service Reference to add a web service reference to a Windows Runtime Component isn’t supported.

Hh556233.collapse_all(en-us,VS.110).gifSupported Bindings

The following WCF bindings are supported in Windows Store Applications:

  1. BasicHttpBinding

  2. NetTcpBinding

  3. NetHttpBinding

  4. CustomBinding

The following binding elements are supported in Windows Store Applications

  1. BinaryMessageEncodingBindingElement

  2. TextMessageEncodingBindingElement

  3. ConnectionOrientedTransportBindingElement

  4. SslStreamSecurityBindingElement

  5. WindowsStreamSecurityBindingElement

  6. TcpTransportBindingElement

  7. HttpTransportBindingElement

  8. HttpsTransportBindingElement

  9. TransportSecurityBindingElement

Both Text and Binary encodings are supported. All WCF transfer modes are supported. For more information see, Streaming Message Transfer.

Hh556233.collapse_all(en-us,VS.110).gifAdd Service Reference

To call a WCF service from a Windows Store application, use the Add Service Reference feature of Visual Studio 2012. You will notice a few changes in the functionality of Add Service Reference when done within a Windows Store application. First no configuration file is generated. Windows Store applications do not use configuration files, so they must be configured in code. This configuration code can be found in the References.cs file generated by Add Service Reference. To see this file, make sure to select “Show All Files” in the solution explorer. The file will be located under the Service References and then Reference.svcmap nodes within the project. All operations generated for WCF services within a Windows Store application will be asynchronous using the Task-based asynchronous pattern. For more information, see Task-based Asynchronous Pattern.

Because configuration is now generated in code, any changes made in the Reference.cs file would be overwritten every time the service reference is updated. To remedy this situation the configuration code is generated within a partial method, which you can implement in your client proxy class. The partial method is declared as follows:

static partial void Configure(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint,
            System.ServiceModel.Description.ClientCredentials clientCredentials);

You can then implement this partial method and change the binding or endpoint in your client proxy class as follows:

public partial class Service1Client : System.ServiceModel.ClientBase<MetroWcfClient.ServiceRefMultiEndpt.IService1>, MetroWcfClient.ServiceRefMultiEndpt.IService1
    { 
        static partial void Configure(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, 
            System.ServiceModel.Description.ClientCredentials clientCredentials)
        {
            if (serviceEndpoint.Name == 
                    ServiceRefMultiEndpt.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1.ToString())
            {
                serviceEndpoint.Binding.SendTimeout = new System.TimeSpan(0, 1, 0);
            }
            else if (serviceEndpoint.Name == 
                    ServiceRefMultiEndpt.Service1Client.EndpointConfiguration.BasicHttpBinding_IService11.ToString())
            {
                serviceEndpoint.Binding.SendTimeout = new System.TimeSpan(0, 1, 0);
                clientCredentials.UserName.UserName = "username1";
                clientCredentials.UserName.Password = "password";
            }
            else if (serviceEndpoint.Name == 
                    ServiceRefMultiEndpt.Service1Client.EndpointConfiguration.NetTcpBinding_IService1.ToString())
            {
                serviceEndpoint.Binding.Name = "MyTcpBinding";
                serviceEndpoint.Address = new System.ServiceModel.EndpointAddress("net.tcp://localhost/tcp");
            }
        }
    }

Hh556233.collapse_all(en-us,VS.110).gifSerialization

The following serializers are supported in Windows Store applications:

  1. DataContractSerializer

  2. DataContractJsonSerializer

  3. XmlSerializer

Caution note Caution

XmlDictionaryWriter.Write(DateTime) now writes the DateTime object as a string.

Hh556233.collapse_all(en-us,VS.110).gifSecurity

The following security modes are supported in Windows Store applications

  1. None

  2. Transport

  3. TransportWithMessageCredentials

  4. TransportCredentialOnly

The following client credential types are supported in Windows Store applications

  1. None

  2. Basic

  3. Digest

  4. Negotiate

  5. NTLM

  6. Windows

  7. Username (Message Security)

  8. Windows (Transport Security)

In order for Windows Store applications to access and send default Windows credentials, you must enable this functionality within the Package.appmanifest file. Open this file and select the Capabilities tab and select “Default Windows Credentials”. This allows the application to connect to intranet resources that require domain credentials.

Important note Important

In order for Windows Store applications to make cross machine calls you must enable another capability called “Home/Work Networking”. This setting is also in the Package.appmanifest file under the Capabilities tab. Select the Home/Work Networking checkbox. This gives your application inbound and outbound access to the networks of the user’s trusted places like home and work. Inbound critical ports are always blocked. For accessing services on the internet you must also enable Internet (Client) capability.

Hh556233.collapse_all(en-us,VS.110).gifMisc

The use of the following classes is supported for Windows Store Applications:

  1. ChannelFactory

  2. DuplexChannelFactory

  3. CallbackBehaviorAttribute

Hh556233.collapse_all(en-us,VS.110).gifDefining Service Contracts

We recommend only defining asynchronous service operations using the task-based async pattern. This ensures Windows Store applications remain responsive while calling a service operation.

Caution note Caution

While no exception will be thrown if you define a synchronous operation, it is strongly recommended to only define asynchronous operations.

Hh556233.collapse_all(en-us,VS.110).gifCalling WCF Services from Windows Store Applications

As mentioned before all configuration must be done in code in the GetBindingForEndpoint method in the generated proxy class. Calling a service operation is done the same as calling any task-based asynchronous method as shown in the following code snippet.

void async SomeMethod()
{
    ServiceClient proxy = new ServiceClient();
    Task<T> results = await proxy.CallAsync(param1, param2);
    T result = results.Result;
    if (result.Success)
    {
       // Do something with result
    }
}

Notice the use of the async keyword on the method making the asynchronous call and the await keyword when calling the asynchronous method.

Show:
© 2014 Microsoft