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. iOS类似QQ好友展开和合并列表的实现代码

    其实原理就是好友列表合上的时候,将这组的cell数设置为0,展开的时候,在变成原来的,就是这么简单 -(); }

  2. iOS UIView 快速修改 frame,

    在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码. 快速实现主要通过 添加类目的方式,对UIView 控件添加了一 ...

  3. gradle使用国内源

    // 设置 maven 库地址 repositories {     maven { url 'http://maven.oschina.net/content/groups/public/' } } ...

  4. 用python下载辞典

    用python下载词源词典Etymoline Online Etymology Dictionary是最好的 English 词源词典,现在来说没有之一.但是,一直在PC上查单词有时不是很方便,遂就想 ...

  5. cics下任务的停止

    cicsterm CEMT I TA TAB==找到要停止的进程 在进程后加入 f或者p 或者fp =========================== 或者找到进程ID号 用命令:kill -9 ...

  6. 通过正则表达式获取url中参数

    url: http://xxxx.com?name=魅力&id=123 js中: var name = getUrlParam("name"); /*通过正则获取url中的 ...

  7. 三种读写XML的方法

    XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...

  8. JLink软件升级到4.92之后,Jlink不能用了

    JLink软件升级到4.92之后,Jlink不能用了                                                       情景描述: Jlink软件升级到4.9 ...

  9. 自定义一个"花瓣"菜单-b

    先来看一下效果 XLCircleMenu.gif 是不是觉得挺好玩的呀. 通过这篇文章你可以学到: 1.系统UITableView的部分设计思想 2.自定义控件常用设计思路 3.动画的具体使用 4.手 ...

  10. js二级下拉菜单

    看似简单的一个菜单,确需要不少的知识点 1. getByClass getElementsByClassName 已经有大部分现代浏览器支持了,只有ie6,ie7,ie8是不支持的.所以对ie6,7, ...