WCF服务寄宿应用程序
1.先创建一个WCF服务库


2.创建一个Console控制台,服务将寄宿在该应用程序上,该程序一旦关闭,服务将停止。
控制台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServiceLibrary;
using System.ServiceModel;
using System.ServiceModel.Description; namespace wcfConsole
{
class Program
{
static void Main(string[] args)
{
//这是将来要引用的服务地址,由你自己定义
Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); //MyService是你WCF服务库里面服务的名称
ServiceHost selfHost = new ServiceHost(typeof(MyService), address); try
{
//IService是你服务库里面接口的名称
selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); //设置服务行为
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb); //打开 ServiceHost
selfHost.Open();
Console.WriteLine("success");
Console.WriteLine();
Console.ReadLine(); // 关闭 ServiceHost
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("Error : {0}", ce.Message);
selfHost.Abort();
}
}
}
}
也可以用 app.config 配置服务信息,代码要略作修改。
app.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.MyService" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8123/ServiceDemo/Service" />
</baseAddresses>
</host>
<endpoint address="MyService" binding="basicHttpBinding"
contract="WcfServiceLibrary.IService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
service.name 是你WCF服务库里服务类的完全限定名
service.behaviorConfiguration 要与 behavior.name 相同
endpoint.contract 是你WCF服务库里接口的完全限定名
改动后的控制台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServiceLibrary;
using System.ServiceModel;
using System.ServiceModel.Description; namespace wcfConsole
{
class Program
{
static void Main(string[] args)
{
//这是将来要引用的服务地址,由你自己定义
//Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); //MyService是你WCF服务库里面服务的名称
ServiceHost selfHost = new ServiceHost(typeof(MyService)); try
{
////IService是你服务库里面接口的名称
//selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); ////设置服务行为
//ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
//smb.HttpGetEnabled = true;
//selfHost.Description.Behaviors.Add(smb); //打开 ServiceHost
selfHost.Open();
Console.WriteLine("success");
Console.WriteLine();
Console.ReadLine(); // 关闭 ServiceHost
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("Error : {0}", ce.Message);
selfHost.Abort();
}
}
}
}
测试:
先运行控制台,然后新建项目添加服务引用:http://localhost:8123/ServiceDemo/Service
直接调用WCF服务库默认提供的方法:GetData()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace InvokeWcfServiceTest
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
Response.Write(client.GetData());
Response.End();
}
}
}

结果是可喜的。
WCF服务寄宿应用程序的更多相关文章
- 创建WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的win ...
- WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘
原文:WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘 通过<再谈IIS与ASP.NET管道>的介绍,相信读者已经对IIS和ASP.NET的请求处理管道有了一个大致 ...
- WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的wind ...
- WCF服务寄宿IIS与Windows服务 - C#/.NET
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...
- 关于WCF引用方式之WCF服务寄宿控制台
1.创建解决方案WCFService 依次添加四个项目,如上图,Client和Hosting为控制台应用程序,Service和Service.Interface均为类库. 2.引用关系 Service ...
- WCF服务寄宿IIS与Windows服务
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...
- WCF服务寄宿Windows
windows服务的介绍 Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合.它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件日志.计 ...
- 使用C#创建WCF服务控制台应用程序
本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Info ...
- 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)
原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...
随机推荐
- codecademy-command line_filesystem
$:shell prompt (命令提示符) In the terminal, first you see $. This is called a shell prompt. It appears w ...
- 15. javacript高级程序设计-Canvas绘图
1. Canvas绘图 HTML5的<canvas>元素提供了一组JavaScript API,让我们可以动态的创建图形和图像.图形是在一个特定的上下文中创建的,而上下文对象目前有两种. ...
- 1. javacript高级程序设计-JavaScript简介
JavaScript诞生于1995年,由Netscape公司布兰登·艾奇开发,JavaScript主要包括三个部分: (1). ECMAScript,由ECMA-262定义,提高核心语言功能 (2). ...
- 【leetcode】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【python】pymongo查找某一时间段的数据
python中实现: 下面代码就是查找2016-09-26 00:00:00 ~ 2016-09-27 00:00:00 时间段的数据 from datetime import datetimefor ...
- 【Git】笔记5 分支管理2
来源:廖雪峰 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一 ...
- python学习 登陆验证
#!/usr/bin/env python #-*- coding=utf-8 -*- #----------------导入模块------------------------------ impo ...
- Jquery 提示还可以输入的字数,将多余的字数截取掉
js代码: $(function () { var counter = $("#divform textarea").val().length; //获取文本域的字符串长度 $( ...
- xmpp xml基本语义
基本语义 9.2.1 消息语义 <message/>节种类可被看作“推”机制,一个实体推信息给其它实体,与 EMAIL 系统中发生的通信类似.所有消息节应该拥有‘to’ 属性,指定有意的消 ...
- September 16th 2016 Week 38th Friday
All the treasures of the earth would not bring back one lost moment. 机会失去不再来,千贯万贯难赎回. Cherish your h ...