Monday, March 11, 2013

wpf client for wcf service

In one of the earlier posts I had listed out the steps to create a wcf service and a client to consume the service.The client for consuming the service was a windows console client.In this post I will show the steps to create a wpf client for the consuming the same wcf service.The post is located here http://virtuoso217.blogspot.com/2012/08/windows-communication-foundation.html

Open Visual Studio and select Windows Presentation Project from the list of projects



















In the MainWindow.xaml drag and drop the following controls
TextBlock1,TextBox1 for the seed wcf service is expecting
TextBlock2,TextBox2 for the dange wcf service is expecting
TextBlock3 to display the random number received from the service
Button to invoke the wcf service















In the next step, import the service proxy generated during the creation of the service i.e. proxy.cs and the the app.config with configuration settings (See the link posted at the beginning of this post for service proxy generation)
App.config settings below


<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IRandomNumberGenerator" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:12345/ServiceExample/Service/RandomNumberGenerator"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IRandomNumberGenerator"
                contract="IRandomNumberGenerator" name="WSHttpBinding_IRandomNumberGenerator">
                <identity>
                    <userPrincipalName value="myname@office.myoffice.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>



In the button click event instantiate the RandomNumberGeneratorClient class of the proxy and call the GetRandomNumber method.


private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RandomNumberGeneratorClient oClient = new RandomNumberGeneratorClient();

                int iSeed = int.Parse(textBox1.Text);

                int iRange = int.Parse(textBox2.Text);

                textBox3.Text = "Random Number returned from the service: " + oClient.GetRandomNumber(iSeed, iRange);

            }
            catch 
            {
                textBox3.Text = "The service is not responding.Please make sure the service is online.";
            }
        }

Run the service and client.


 Client
Service
Client

Service

No comments: