Follow artech to learn about the status of WCF (3)

Source: Internet
Author: User

At first, I thought it was a problem with the WCF session. I knocked on one side of the code and found that the session was not used to store data. After my own research, I found that the author again stored the status of the WCF.

The project structure is as follows:

 

The Code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using Contract;namespace Client{    class Program    {        static void Main(string[] args)        {            ChannelFactory<ICalculator> calculatorChannelFactory = new ChannelFactory<ICalculator>("httpEndpoint");            Console.WriteLine("Create a calculator proxy: proxy1");            ICalculator proxy1 = calculatorChannelFactory.CreateChannel();            Console.WriteLine("Invocate  proxy1.Adds(1)");            proxy1.Adds(1);            Console.WriteLine("Invocate  proxy1.Adds(2)");            proxy1.Adds(2);            Console.WriteLine("The result return via proxy1.GetResult() is : {0}", proxy1.GetResult());            try            {                proxy1.Adds(1);            }            catch (Exception ex)            {                Console.WriteLine("It is fail to invocate the Add after terminating session because \"{0}\"", ex.Message);            }            //(proxy1 as ICommunicationObject).Close();            Console.WriteLine("Create a calculator proxy: proxy2");            ICalculator proxy2 = calculatorChannelFactory.CreateChannel();            Console.WriteLine("Invocate  proxy2.Adds(1)");            proxy2.Adds(1);            Console.WriteLine("Invocate  proxy2.Adds(2)");            proxy2.Adds(2);            Console.WriteLine("The result return via proxy2.GetResult() is : {0}", proxy2.GetResult());            //(proxy2 as ICommunicationObject).Close();            Console.Read();        }    }}

======================================

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. servicemodel; namespace contract {[servicecontract (sessionmode = sessionmode. required)] public interface icalculator {// call any method that allows the initialization of the Session Service (by default, all service methods automatically initialize the session, that is, isinitiating = true ). // Call any service method that contains the "isterminating = true" Declaration (by default, all service methods are isterminating = false, and we need to display the Declaration ). // Terminating termination means initiating initiation means [operationcontract (isoneway = true, isinitiating = true, isterminating = false)] void adds (Double X); [operationcontract (isinitiating = false, isterminating = true)] Double getresult ();}}

==========================================================

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using Sevice;using System.ServiceModel;namespace Hosting2{    class Program    {        static void Main(string[] args)        {            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))            {                host.Opened += delegate                {                    Console.WriteLine("The Calculator service has begun to listen");                };                host.Open();                Timer timer = new Timer(delegate { GC.Collect(); }, null, 0, 100);                Console.Read();            }        }    }}

==============

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Contract;using System.ServiceModel;namespace Sevice{        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]   public class CalculatorService:ICalculator    {       public CalculatorService()       {           Console.WriteLine("Calculator object has been created");       }       ~CalculatorService()       {           Console.WriteLine("Calculator object has been destoried");       }       private double _result;        void ICalculator.Adds(double x)        {            Console.WriteLine("The Add method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);            this._result += x;        }        double ICalculator.GetResult()        {            Console.WriteLine("The GetResult method is invoked and the current session ID is: {0}", OperationContext.Current.SessionId);            return this._result;        }    }}

 

 

The client configuration is as follows:

  <system.serviceModel>    <client>      <endpoint address="http://localhost:9999/SessionfulCalculator"          binding="wsHttpBinding" contract="Contract.ICalculator"          name="httpEndpoint" />    </client>  </system.serviceModel>

The server configuration is as follows:

  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior name="CalculatorBehavior">          <serviceMetadata httpGetEnabled="true" />        </behavior>      </serviceBehaviors>    </behaviors>    <services>      <service behaviorConfiguration="CalculatorBehavior" name="Sevice.CalculatorService">        <endpoint address="" binding="wsHttpBinding" bindingConfiguration=""          contract="Contract.ICalculator" />        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.