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

WCF - Self Hosting的更多相关文章

  1. WCF - WAS Hosting

    WCF - WAS Hosting To understand the concept of WAS hosting, we need to comprehend how a system is co ...

  2. WCF - IIS Hosting

    WCF - IIS Hosting Hosting a WCF service in IIS (Internet Information Services) is a step-by-step pro ...

  3. Learning WCF Chapter1 Hosting a Service in IIS

    How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...

  4. (WCF) WCF Service Hosting.

    3 Options. 1. Host inside of an application. 2. Host into Windows service. 3. Host into IIS 参考: http ...

  5. WCF - Hosting WCF Service

    After creating a WCF service, the next step is to host it so that the client applications can consum ...

  6. WCF - Hosting WCF Service 四种托管方式

    https://www.tutorialspoint.com/wcf/wcf_hosting_service.htm After creating a WCF service, the next st ...

  7. WCF学习系列汇总

    最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...

  8. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  9. WCF 入门(29)

    前言 最近工作比较忙,加了会班就不想再写东西了,就想洗洗睡. 但是这个视频真的不能断,不能像过去一样写了几集就停了. 现在公司在做一个MVC框架的项目,话说已经一年没有写MVC了,重新上手的感觉还可以 ...

随机推荐

  1. 方法 :PHP开发环境搭建(phpstorm + xampp+mongodb)

    phpstorm 安装下载       百度网盘资源 phpstorm 9.0.1(有序列号)   http://pan.baidu.com/s/1kTvX0jl xampp 安装下载         ...

  2. IO流02_文件过滤器

    [简述] 在File类的list方法中可以接收一个FilenameFilter参数,通过该参数可以列出只满足要求的文件. FilenameFilter接口里包含了一个accept(File dir, ...

  3. 九度OJ 1283 第一个只出现一次的字符

    题目地址:http://ac.jobdu.com/problem.php?pid=1283 题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现 ...

  4. OpenJudge/Poj 1316 Self Numbers

    1.链接地址: http://poj.org/problem?id=1316 http://bailian.openjudge.cn/practice/1316 2.题目: 总时间限制: 1000ms ...

  5. 配置php的CAS客户端

    1.下载安装xmapp 2.开启Apache服务. 3.下载php的CAS客户端源码包(我使用的是CAS-1.2.0.tgz),解压到xmap的htdocs目录下(D:\xmapp\htdocs),进 ...

  6. Less使用——让老司机带你飞

    为什么我要使用Less less的作为编写css的工具插件,省时.方便.检测,具体的安装,请参考我的一篇文章<sublime text3 个人使用心得>,里面我讲解了安装方法,使用webs ...

  7. PHP学习笔记(5) - 选择一个合格的框架

    作为一个合格的PHP开发框架,个人觉得起码需要满足以下几点. 一.上梁不正下梁歪,好的框架首先自身要有良好的编码规范和文件目录结构,代码要易于阅读理解. 二.为了可以更好地适应OOP,框架必须可以提供 ...

  8. 推荐一款java的验证码组件——kaptcha

    使用方法: 项目中导入kaptcha-2.3.jar包 在web.xml里面新增:   <!-- 登陆验证码Kaptcha --> <servlet> <servlet- ...

  9. Ms SQLServer中的Union和Union All的使用方法和区别

    Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...

  10. java 中的 & | ~ ^ 运算符分析

    1.与运算符与运算符用符号“&”表示,其使用规律如下:两个操作数中位都为1,结果才为1,否则结果为0,例如下面的程序段.public class data13{public static vo ...