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. popen pclose 不等待命令执行完毕

    $handle = popen("start D:\\test.bat", "r"); //exec("start D:\\test.bat" ...

  2. ###学习《C++ Primer》- 2

    点击查看Evernote原文. #@author: gr #@date: 2014-10-01 #@email: forgerui@gmail.com Part 2: STL顺序容器(第9章) 一.标 ...

  3. 文本框限制输入类型<input>的输入框

    最近在开发完一个项目后,又测试人员测试bug,然后我根据他们测试出来的bug一个一个的改,然后就遇到了一个问题,文本框是用来搜索,但是,比如这个文本框是用来搜索年龄的区间,输入条件的时候,如果输入了非 ...

  4. 创建Unity新项目并编译成游戏程序

    注:本人所使用的Unity版本为:Unity5.3.5f1,所使用的VS版本为:Visual.Studio.2013.Ultimate 折腾了快一个月了,终于有时间做自己的啦,哈哈: ) 步骤一:启动 ...

  5. 转载:一句代码改变Swing难看的字体

    Swing 皮肤的一个键值:swing.boldMetal 默认为 true因此造成了默认字体极度难看: 其实一句代码就能解决问题:UIManager.put("swing.boldMeta ...

  6. 实习笔记-2:sql 分组不一定要group by

    今天在公司写代码的时候,遇到一个sql语句构建问题. 情形是这样的: 我需要获取不同小组下前N条记录. select top 10 * from dbo.Topic where GroupID in ...

  7. SQL技巧之分类汇总

    数据表结构username type numaaaa   玉米 1212aaaa   玉米  212bbb     小麦  2323bbb .... 只有两种产品 玉米和小麦,玉米价格1.5,小麦价格 ...

  8. demo_01 css3中的radius

    css属性:border-radius :border:边框:radius:弧度:所以这个属性的意思很明了. 下面实现一个小demo: <!doctype html> <html&g ...

  9. 安装JDK设置环境变量

    PS:之前在CSDN上写的文章,现在转到博客园~ 在安装过程中第一次让选择jdk的安装路径,第二次让选择jre的安装路径.两者不可以在同一个文件夹下,否则在cmd中运行javac时会报:摘不到或无法加 ...

  10. Erlang 开发者的福音:IntelliJ IDEA 的 Erlang 插件

    IntelliJ IDEA 的 Erlang 插件,主要特性: 智能编辑器:  Erlang 代码补全.语法和错误高亮.代码检查 代码导航:项目和文件结构视图.在文件.模型.函数和用例之间快速跳转 工 ...