Sunday, March 4, 2012

how to read from an WCF web service and how to build your own WCF web service

This post is going to be a lot like my post titled "how to read from an .asmx web service and how to build your own .asmx web service" and I recommend reading that post first as the work we are to do will use an application identical to the one in the other post while merely taking a different approach to a web service. Again, we are using a calculator application with one web form. The web form itself will be the same as it was in my other post. The code behind is a little different however. Notice the namespace CalculatorThatUsesWebService.MyServiceReference as CalculatorWebServiceClient uses this namespace. The code for our code behind is as follows:

using System;

using CalculatorThatUsesWebService.MyServiceReference;

namespace CalculatorThatUsesWebService

{

   public partial class _Default : System.Web.UI.Page

   {

      protected void Page_Load(object sender, EventArgs e)

      {

      }

   

      protected void EqualsButton_Click(object sender, EventArgs e)

      {

         CalculatorWebServiceClient client = new CalculatorWebServiceClient();

         int firstInteger = Convert.ToInt32(FirstTextBox.Text);

         int lastInteger = Convert.ToInt32(LastTextBox.Text);

         int magicNumber = client.AddTwoIntegers(firstInteger, lastInteger);

         LastLabel.Text = magicNumber.ToString();

      }

   }

}

 
 

MyServiceReference is a web service reference. It references the WCF web service at http://www.sweetsoursaltybitter.com/CalculatorWebService.svc, and, in order to use to use this service, I went to http://www.sweetsoursaltybitter.com/CalculatorWebService.svc in a browser and learned that the service exposes a Web Services Description Language (WSDL) enpoint at: http://www.sweetsoursaltybitter.com/CalculatorWebService.svc?wsdl

 
 

I then right-clicked on my project and selected: Add Service Reference...

 
 

I then filled out the dialog box that appeared to add a reference to the WSDL which created the CalculatorThatUsesWebService.MyServiceReference namespace. This illustrates how to use a WCF web service, and thus begs the question "How does one author a WCF web service?" To do so, at a UI project in Visual Studio, right-click and then select: Add > New Item... and finally navigate to pick the WCF Service option.

 
 

A .svc file will be created which you may change up:

namespace CalculatorThatUsesWebService

{

   public class CalculatorWebService : ICalculatorWebService

   {

      public int AddTwoIntegers(int initialInteger, int integerToAppend)

      {

         return initialInteger + integerToAppend;

      }

   }

}

 
 

To my surprise, an interface was also created! I changed it up some too. In both my class and my interface I replaced the default DoWork(); method with a method of my own to handle the calculation needed.

using System.ServiceModel;

namespace CalculatorThatUsesWebService

{

   [ServiceContract]

   public interface ICalculatorWebService

   {

      [OperationContract]

      int AddTwoIntegers(int initialInteger, int integerToAppend);

   }

}

 
 

At the suggestion of "Pro WCF 4" (a book by Nishith Pathak) I added this to my Web.config file inside the system.serviceModel tag like so:

<services>

   <service name="ICalculatorWebService">

      <endpoint contract="ICalculatorWebService" binding="wsHttpBinding" />

   </service>

</services>

 
 

That said, I tried removing this code and there did not seem to be a side effect. I suppose that I do not, as of this writing, understand the nuances of the Web.config configuration around WCF. The whole of the system.serviceModel tag is as follows. All of the code was generated for me save for the stuff I wrote above.

<system.serviceModel>

   <bindings>

      <basicHttpBinding>

         <binding name="BasicHttpBinding_ICalculatorWebService"

               closeTimeout="00:01:00" openTimeout="00:01:00"

               receiveTimeout="00:10:00" sendTimeout="00:01:00"

               allowCookies="false" bypassProxyOnLocal="false"

               hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"

               maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

               messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

               useDefaultWebProxy="true">

            <readerQuotas maxDepth="32" maxStringContentLength="8192"

                  maxArrayLength="16384" maxBytesPerRead="4096"

                  maxNameTableCharCount="16384" />

            <security mode="None">

               <transport clientCredentialType="None" proxyCredentialType="None"

                     realm="" />

               <message clientCredentialType="UserName" algorithmSuite="Default" />

            </security>

         </binding>

      </basicHttpBinding>

   </bindings>

   <client>

      <endpoint address="http://www.sweetsoursaltybitter.com/CalculatorWebService.svc"

               binding="basicHttpBinding"

               bindingConfiguration="BasicHttpBinding_ICalculatorWebService"

               contract="MyServiceReference.ICalculatorWebService"

               name="BasicHttpBinding_ICalculatorWebService" />

   </client>

   <services>

      <service name="ICalculatorWebService">

         <endpoint contract="ICalculatorWebService" binding="wsHttpBinding" />

      </service>

   </services>

   <behaviors>

      <serviceBehaviors>

         <behavior name="">

            <serviceMetadata httpGetEnabled="true" />

            <serviceDebug includeExceptionDetailInFaults="false" />

         </behavior>

      </serviceBehaviors>

   </behaviors>

   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

</system.serviceModel>

No comments:

Post a Comment