• 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();
                }

            }
        }
    }
  • 相关阅读:
    通过另外一个应用程序给多个文本框赋值, 模拟单击事件
    AngularJS
    九章算法
    实现继承
    二分查找
    NET Core依赖注入解读&使用Autofac替代实现
    NET SignalR 与 LayIM2.0
    WebVR
    依赖注入
    如何实现配置与源文件的同步
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1931487.html
Copyright © 2020-2023  润新知