zoukankan      html  css  js  c++  java
  • WCF Server Console

    代码
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;

    namespace Microsoft.ServiceModel.Samples
    {
        
    // Define a service contract.
        [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
        
    public interface ICalculator
        {
            [OperationContract]
            
    double Add(double n1, double n2);
            [OperationContract]
            
    double Subtract(double n1, double n2);
            [OperationContract]
            
    double Multiply(double n1, double n2);
            [OperationContract]
            
    double Divide(double n1, double n2);
        }

        
    // Service class that implements the service contract.
        
    // Added code to write output to the console window.
        public class CalculatorService : ICalculator
        {
            
    public double Add(double n1, double n2)
            {
                
    double result = n1 + n2;
                Console.WriteLine(
    "Received Add({0},{1})", n1, n2);
                Console.WriteLine(
    "Return: {0}", result);
                
    return result;
            }

            
    public double Subtract(double n1, double n2)
            {
                
    double result = n1 - n2;
                Console.WriteLine(
    "Received Subtract({0},{1})", n1, n2);
                Console.WriteLine(
    "Return: {0}", result);
                
    return result;
            }

            
    public double Multiply(double n1, double n2)
            {
                
    double result = n1 * n2;
                Console.WriteLine(
    "Received Multiply({0},{1})", n1, n2);
                Console.WriteLine(
    "Return: {0}", result);
                
    return result;
            }

            
    public double Divide(double n1, double n2)
            {
                
    double result = n1 / n2;
                Console.WriteLine(
    "Received Divide({0},{1})", n1, n2);
                Console.WriteLine(
    "Return: {0}", result);
                
    return result;
            }
        }
        
    class Program
        {
            
    static void Main(string[] args)
            {

                
    // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
                Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

                
    // Step 2 of the hosting procedure: Create ServiceHost
                ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

                
    try
                {


                    
    // Step 3 of the hosting procedure: Add a service endpoint.
                    selfHost.AddServiceEndpoint(
                        
    typeof(ICalculator),
                        
    new WSHttpBinding(),
                        
    "CalculatorService");


                    
    // Step 4 of the hosting procedure: Enable metadata exchange.
                    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled 
    = true;
                    selfHost.Description.Behaviors.Add(smb);

                    
    // Step 5 of the hosting procedure: Start (and then stop) the service.
                    selfHost.Open();
                    Console.WriteLine(
    "The service is ready.");
                    Console.WriteLine(
    "Press <ENTER> to terminate service.");
                    Console.WriteLine();
                    Console.ReadLine();

                    
    // Close the ServiceHostBase to shutdown the service.
                    selfHost.Close();
                }
                
    catch (CommunicationException ce)
                {
                    Console.WriteLine(
    "An exception occurred: {0}", ce.Message);
                    selfHost.Abort();
                }

            }
        }
    }
  • 相关阅读:
    RTC驱动程序分析
    Linux下的RTC子系统
    [置顶] 谈EXPORT_SYMBOL使用
    《Linux内核修炼之道》精华分享与讨论(5)——Kernel地图:Kconfig与Makefile
    写出高效优美的单片机C语言代码
    哈夫曼树
    如何提高浮点数变整数的精度
    CF798C Mike and gcd problem
    CF822C Hacker, pack your bags!
    CF821C Okabe and Boxes
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1931487.html
Copyright © 2011-2022 走看看