博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF - Self Hosting
阅读量:6708 次
发布时间:2019-06-25

本文共 3349 字,大约阅读时间需要 11 分钟。

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

 

 

转载地址:http://nkilo.baihongyu.com/

你可能感兴趣的文章
国际化环境下系统架构演化
查看>>
C#跟着阿笨玩一起玩异步Task实战(一)
查看>>
Sqoop-1.4.6安装部署及详细使用介绍
查看>>
oracle 存储过程 示例
查看>>
正态分布与中心极限定理
查看>>
cf1027F. Session in BSU(并查集 匈牙利)
查看>>
Chrome 主页被篡改
查看>>
openlayers入门开发系列之批量叠加zip压缩SHP图层篇
查看>>
gcc中的-Wl,rpath=<your_lib_dir>选项
查看>>
Javascript调用Webservice的多种方法 .
查看>>
Linux 启动、关闭、重启网络服务
查看>>
[转载]定制CentOS 6.3 自动安装盘
查看>>
js生成动态的飘过效果
查看>>
Java进阶05 多线程
查看>>
SQLSERVER性能监控级别步骤
查看>>
Java使用ScriptEngine(javax.script)
查看>>
Nhibernate中 Many-To-One 中lazy="proxy" 延迟不起作用的原因
查看>>
svn权限设置
查看>>
MVC验证11-对复杂类型使用jQuery异步验证
查看>>
C++static关键字用法
查看>>