Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Posts Tagged ‘WCF’

AsyncCalls To WCF

Posted by ken zheng on September 11, 2008

This example show you how a client can access a service operation asynchronously in a easy way.
First, I created a WCF Service Library in VS 2008


using System;
using System.ServiceModel;
using System.Diagnostics;

[ServiceContract]
interface ICalculator
{
   [OperationContract]
   int Add(int number1,int number2);

   [OperationContract]
   int Subtract(int number1,int number2);

   [OperationContract]
   int Multiply(int number1,int number2);

   [OperationContract]
   int Divide(int number1,int number2);
}

class Calculator : ICalculator
{
   public int Add(int number1,int number2)
   {
      return number1 + number2;
   }
   public int Subtract(int number1,int number2)
   {
      return number1 - number2;
   }
   public int Multiply(int number1,int number2)
   {
      return number1 * number2;
   }
   public int Divide(int number1,int number2)
   {
      return number1 / number2;
   }
}

Then run the Service Test, if you add a Service Reference to the WCF Service will not generate a event-driven asynchronous calling. You need run the ServiceModel Metadata Utility Tool (Svcutil.exe) tool with both the /async and the /tcv:Version35 command options together as shown in the following command.

svcutil http://localhost:8731/Design_Time_Addresses/SimpleCalculatorService/Calculator/mex /a /tcv:Version35

This generates, in addition to the synchronous and standard delegate-based asynchronous operations, a WCF client class that contains:

  • Two Async operations for use with the event-based asynchronous calling approach
  • Operation completed events of the form Completed for use with the event-based asynchronous calling approach.
  • System.EventArgs types for each operation (of the form CompletedEventArgs) for use with the event-based asynchronous calling approach
  • Then in your client, you just need to create a method to handle the CompletedEvent.

            private void button1_Click(object sender, EventArgs e)
            {
                // AddAsync
                CalculatorClient client = new CalculatorClient();
                int value1 = 100;
                int value2 = 15;
                client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
                client.AddAsync(value1, value2);
                Console.WriteLine("Add({0},{1})", value1, value2);
            }
    
            private void AddCallback(object sender, AddCompletedEventArgs args)
            {
                Text = "Sum = " + args.Result;
            }
    

    Enjoy coding! You can download the code from Here
    More details from http://msdn.microsoft.com/en-us/library/ms730059.aspx

    Posted in .Net, VS2008 | Tagged: | Leave a Comment »

    WCF .Net 3.5 Tutorials

    Posted by ken zheng on September 10, 2008

    Posted in .Net, VS2008 | Tagged: , , | Leave a Comment »

    MOQ WCF

    Posted by ken zheng on September 9, 2008

    Here is the example you can mock a WCF service for Unit Testing

    Create a simple WCF Service in VS 2008, learn more WCF from http://msdn.microsoft.com/en-au/magazine/cc163289.aspx

        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
        }
    
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    

    Then create a client class which reference the service

        public class HelloMOQ
        {
    
            public string getMessage(MyService.IService1 _service)
            {
                string s = "";
                s = _service.GetData(12);
    
                return s;
            }
        }
    

    Now it is the time to MOCK, same way to create a Unit Test Project

            [TestMethod()]
            public void getMessageTest()
            {
                var WCFClient = new Mock<IService1>();
                WCFClient.Expect(client => client.GetData(1)).Returns("Hello 1");
    
                HelloMOQ sc = new HelloMOQ();
                sc.getMessage(WCFClient.Object);
            }
    

    It is easy, doesn’t it.

    You can download the source code from Here

    Posted in .Net, VS2008 | Tagged: , | Leave a Comment »

    WCF Security Guide

    Posted by ken zheng on August 13, 2008

    The official 1.0 release of the WCF security guide. Handy reference to have.
    http://www.codeplex.com/WCFSecurityGuide/Release/ProjectReleases.aspx?ReleaseId=15892

    Posted in .Net | Tagged: | Leave a Comment »