WCF - Self Hosting

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();
        }
    }
}
复制代码

 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(299)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
历史上的今天:
2014-07-08 AcceptAsync和BeginAccept的区别
点击右上角即可分享
微信分享提示