none
Improve basic service RRS feed

  • Question

  • How can I improve my service and client ? Give basic examples,please.

    using System;
    using System.ServiceModel;
    namespace Client
    {
        [ServiceContract]
        public interface IStockService
        {
            [OperationContract]
            double GetPrice(string ticker);
        }
     
        class Client
        {
            static void Main()
            {
                ChannelFactory<IStockService> myChannelFactory=
                    new ChannelFactory<IStockService>(
                        new BasicHttpBinding(),
                        new EndpointAddress
                            ("http://localhost:8000/EssentialWCF")
                        );
                IStockService wcfClient = myChannelFactory.CreateChannel();
     
                double p = wcfClient.GetPrice("msft");
                Console.WriteLine("Price: {0}",p);
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.ServiceModel;
     
    namespace EssentialWCF
    {
        [ServiceContract]
        public interface IStockService
        {
            [OperationContract]
            double GetPrice(string ticker);
        }
     
        public class StockService : IStockService
        {
            public double GetPrice(string ticker)
            {
                return 94.85;
            }
        }
     
        public class Service
        {
            public static void Main()
            {
                ServiceHost serviceHost= new 
                                    ServiceHost(typeof(StockService),
                                    new Uri("http://localhost:8000/EssentialWCF"));
                serviceHost.AddServiceEndpoint(
                        typeof(IStockService),
                        new BasicHttpBinding(),
                        ""
                    );
                serviceHost.Open();
     
                Console.WriteLine("Press <ENTER>.\n\n");
                Console.ReadLine();
     
                serviceHost.Close();
            }
        }
    }

    Any good advice welcome.


    Saturday, April 12, 2014 1:21 PM

Answers

  • Add exception handlers (Try/Catch) to the code. How does the service handle a timeout condition if the server doesn't response?  Will this cause an exception in the service or do you have to add a timer to your code?  What does the service return under error conditions like the stock name doesn't exist? 


    jdweng

    Saturday, April 12, 2014 1:49 PM
  • HI

    Client Side :

    At the client while adding the proxy (service reference) click on Advanced for service reference settings, under the client option select "Generate Asyncronous Operation" radio button 

    Server Side : enclose the code in using block

    using((ServiceHost serviceHost= new ServiceHost(typeof(StockService)))

    {

    //Code block

    }


    which will take care of disposing of the objective, The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object

    Also if your WCF service is required for client in intranet try using net.tcp binding for WCF service which is faster as compared other binding

    Regards

    Rajesh Sajjanar


    Sunday, April 13, 2014 5:42 PM
  • Hi,

    For your question, maybe you can try to modify your service and client code as following:
    Service.cs:

    public static void Main()
            {
               ServiceHost serviceHost= new ServiceHost(typeof(StockService),  new Uri(http://localhost:8000/EssentialWCF)) 
                try
                 { 
                     serviceHost.AddServiceEndpoint(
                     typeof(IStockService),
                     new BasicHttpBinding(),
                     ""
                    );
                serviceHost.Open();
                Console.WriteLine("Press <ENTER>.\n\n");
                Console.ReadLine();
                serviceHost.Close();
               } 
             catch(CommunicationException ce)
               {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                serviceHost.Abort();
               }
            }
         }

    Client.cs:

    static void Main()
            {
                ChannelFactory<IStockService> myChannelFactory=
                    new ChannelFactory<IStockService>(
                        new BasicHttpBinding(),
                        new EndpointAddress
                       ("http://localhost:8000/EssentialWCF")
                        );
                try
                {  
                   myChannelFactory.Open();
                   IStockService wcfClient = myChannelFactory.CreateChannel();
                   wcfClient.Open();
                   double p = wcfClient.GetPrice("msft");
                   Console.WriteLine("Price: {0}",p);
                   Console.ReadKey();
    
                 }
                 catch (Exception e)
                  {
                     // Abort the channel if we have one
                     if(wcfClient != null)
                     wcfClient.Abort();
                   }
                   finally
                   {
                      if (myChannelFactory.State == CommunicationState.Opened)
                      {
                        myChannelFactory.Close(); // It throws an exception that the channel is in a faulted state.
                       }
                    }
    
     
                }
    
    

    Best Regards,
    Amy Peng




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 16, 2014 6:54 AM
    Moderator

All replies

  • Add exception handlers (Try/Catch) to the code. How does the service handle a timeout condition if the server doesn't response?  Will this cause an exception in the service or do you have to add a timer to your code?  What does the service return under error conditions like the stock name doesn't exist? 


    jdweng

    Saturday, April 12, 2014 1:49 PM
  • Which concrete exceptions can produce my code ?
    The answer to this question will help me to understand the course of your thinking. I dont readed about timers in code.

    Sunday, April 13, 2014 5:22 PM
  • HI

    Client Side :

    At the client while adding the proxy (service reference) click on Advanced for service reference settings, under the client option select "Generate Asyncronous Operation" radio button 

    Server Side : enclose the code in using block

    using((ServiceHost serviceHost= new ServiceHost(typeof(StockService)))

    {

    //Code block

    }


    which will take care of disposing of the objective, The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object

    Also if your WCF service is required for client in intranet try using net.tcp binding for WCF service which is faster as compared other binding

    Regards

    Rajesh Sajjanar


    Sunday, April 13, 2014 5:42 PM
  • There are 1000 different exception that can occur in the WCF service especially when credentials are required.


    jdweng

    Sunday, April 13, 2014 8:22 PM
  • Hi,

    For your question, maybe you can try to modify your service and client code as following:
    Service.cs:

    public static void Main()
            {
               ServiceHost serviceHost= new ServiceHost(typeof(StockService),  new Uri(http://localhost:8000/EssentialWCF)) 
                try
                 { 
                     serviceHost.AddServiceEndpoint(
                     typeof(IStockService),
                     new BasicHttpBinding(),
                     ""
                    );
                serviceHost.Open();
                Console.WriteLine("Press <ENTER>.\n\n");
                Console.ReadLine();
                serviceHost.Close();
               } 
             catch(CommunicationException ce)
               {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                serviceHost.Abort();
               }
            }
         }

    Client.cs:

    static void Main()
            {
                ChannelFactory<IStockService> myChannelFactory=
                    new ChannelFactory<IStockService>(
                        new BasicHttpBinding(),
                        new EndpointAddress
                       ("http://localhost:8000/EssentialWCF")
                        );
                try
                {  
                   myChannelFactory.Open();
                   IStockService wcfClient = myChannelFactory.CreateChannel();
                   wcfClient.Open();
                   double p = wcfClient.GetPrice("msft");
                   Console.WriteLine("Price: {0}",p);
                   Console.ReadKey();
    
                 }
                 catch (Exception e)
                  {
                     // Abort the channel if we have one
                     if(wcfClient != null)
                     wcfClient.Abort();
                   }
                   finally
                   {
                      if (myChannelFactory.State == CommunicationState.Opened)
                      {
                        myChannelFactory.Close(); // It throws an exception that the channel is in a faulted state.
                       }
                    }
    
     
                }
    
    

    Best Regards,
    Amy Peng




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 16, 2014 6:54 AM
    Moderator