zoukankan      html  css  js  c++  java
  • WCF

    WCF - Self Hosting

    Here, the WCF service is hosted in a console application. Given below is the process with suitable steps in a sequential manner that explains the entire process.

    这里演示的wcf服务将托管在控制台应用程序上。

    Step 1 : First, let's create the Service contract and its implementation. Create a console application and name it as MyCalculatorService. This is a simple service to return the addition of two numbers.

    步骤1:首先创建服务契约以及它的实现。创建一个类库,命名为MyCalculatorWCFService。这是一个简单的服务,用来计算2个数字的和。

    Step 2 : Now, right click on the References in the Solution Explorer and click Add References. The following window opens; add System.ServiceModel reference to the project.

    步骤2:右键引用,添加引用。System.ServiceModel

    Step 3 : Create an ISimpleCalculator interface, Add ServiceContract and OperationContract attribute to the class and function as shown below.

    You will know more about these contracts in the later session. These contracts will expose the method to the outside world for using this service.

    创建一个ISimpleCalculator接口,给类以及函数添加如下的服务契约以及操作契约

    在之后的学习中,你会知道更多关于契约的内容。这些契约对外部公开了方法,确保外部可以使用服务

    Step 4 : The code behind this file is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    
    namespace MyCalculatorWCFService
    {
        [ServiceContract]
        public interface ISimpleCalculator
        {
            [OperationContract]
            int Sum(int number1, int number2);
        }
    }

    Step 5 : MyCalculatorService is the implementation class for IMyCalculatorService interface as shown below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyCalculatorWCFService
    {
        public class MyCalculatorService : ISimpleCalculator
        {
            public int Sum(int number1, int number2)
            {
                return number1 + number2;
            }
        }
    }

    Step 6 : Now, we are ready with the service. Let's go for implementing the hosting process. Create a new console application and name it as 'MyCalculatorWCFServiceHost'.

    现在已经准备好了服务,让我们来实现托管进程。创建一个新的控制台应用程序,命名为MyCalculatorWCFServiceHost

    Step 7 : Add the reference of system.servicemodel and the project MyCalculatorWCFService.

    The code behind this is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using MyCalculatorWCFService;
    
    namespace MyCalculatorWCFServiceHost
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a URI to serve as the base address
                Uri httpUrl = new Uri("http://localhost:8090/MyCalculatorWCFService/SimpleCalculator");
    
                //Create ServiceHost
                ServiceHost host = new ServiceHost(typeof(MyCalculatorService), httpUrl);
    
                //Add a service endpoint
                host.AddServiceEndpoint(typeof(ISimpleCalculator), new WSHttpBinding(), "");
    
                //Enable metadata exchange
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
    
                //Start the Service
                host.Open();
                Console.WriteLine("Service is host at " + DateTime.Now.ToString());
                Console.WriteLine("Host is running... Press  key to stop");
                Console.ReadLine();
            }
        }
    }

     

  • 相关阅读:
    大二下学期第一次结对作业(第二阶段)
    大二下学期阅读笔记(人月神话)
    大二下每周总结
    大二下学期第一次结对作业(第二阶段)
    大二下学期第一次结对作业(第二阶段)
    elasticsearch mappings之dynamic的三种状态
    elasticsearch mapping映射属性_source、_all、store和index
    Java学习
    Java学习
    Java学习
  • 原文地址:https://www.cnblogs.com/chucklu/p/4630053.html
Copyright © 2011-2022 走看看