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的更多相关文章
- WCF - WAS Hosting
WCF - WAS Hosting To understand the concept of WAS hosting, we need to comprehend how a system is co ...
- WCF - IIS Hosting
WCF - IIS Hosting Hosting a WCF service in IIS (Internet Information Services) is a step-by-step pro ...
- 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 ...
- (WCF) WCF Service Hosting.
3 Options. 1. Host inside of an application. 2. Host into Windows service. 3. Host into IIS 参考: http ...
- WCF - Hosting WCF Service
After creating a WCF service, the next step is to host it so that the client applications can consum ...
- WCF - Hosting WCF Service 四种托管方式
https://www.tutorialspoint.com/wcf/wcf_hosting_service.htm After creating a WCF service, the next st ...
- WCF学习系列汇总
最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF 入门(29)
前言 最近工作比较忙,加了会班就不想再写东西了,就想洗洗睡. 但是这个视频真的不能断,不能像过去一样写了几集就停了. 现在公司在做一个MVC框架的项目,话说已经一年没有写MVC了,重新上手的感觉还可以 ...
随机推荐
- 使用FOR循环语句在屏幕上输出一个由星号组成的直角三角形
题目要求: 请用C++的信息输出方式,使用循环语句在屏幕上输出一个由星号组成的直角三角形,形状如下: * ** *** **** ***** 要求: 完全使用C++的信息输出方式,即cout以及流插入 ...
- Java创建线程的第二种方式:实现runable接口
/*需求:简单的卖票程序多个窗口买票 创建线程的第二种方式:实现runable接口 *//*步骤1.定义类实现Runable接口2.覆盖Runable接口中的run方法 将线程要运行的代码存放在 ...
- How to: Reading an ini config file from a batch file
Original Link: http://almanachackers.com/blog/2009/12/31/reading-an-ini-config-file-from-a-batch-fil ...
- 模板:函数memset
需要的头文件 <memory.h> or <string.h> memset 函数介绍 void *memset(void *s, int ch, size_t n); 函 ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(二)
着手改造之前,有兴趣可以阅读下官方文档:http://help.adobe.com/zh_CN/air/extensions/index.html 新建工程 NavService 并创建包 nav.w ...
- Yii 关于 find findAll 查找出制定的字段的方法
总所周知 modelName::model() -> find() //找出的是一个对象 modelName::model() -> findALL() //找出的是一个对象集合的数组 如 ...
- js实现克隆一个对象
var app={}; app.cloneobj= function(obj){ var o; if(typeof obj == "object"){ if(obj===null) ...
- 一个基本jquery的评论留言模块
<div class="productDiscuss"> <div class="title"><span class=" ...
- laravel1
生成模型的时候 同时生成migration文件php artisan make:model User --migration
- (转载)Delphi开发经验谈
Delphi开发经验谈 开发环境-------- Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件. ...