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 摘要 在项目开发中,我们可能会适时的选 ...
随机推荐
- FileUpload1上传控件
<asp:FileUpload ID="FileUpload1" runat="server" /> string fn = System.IO.P ...
- 手动设置Windows7锁屏界面背景
windows7系统安装之后锁屏.关机界面.开机欢迎界面都是系统默认的背景,其实这些背景就像桌面壁纸一样是可以更改的,如果没有修改过的话,按下面步骤就可以修改了. 首先选择一张喜欢的背景图片,分辨率不 ...
- reportng的使用
1.首先安装testng 2.下载reportng jar包 http://pan.baidu.com/s/1i3KdlQH 3.添加到project build path 注意:需要同时引入goog ...
- ABAP 内表的行列转换-发货通知单-打印到Excel里-NEW-(以运单号为单位显示ALV然后保存输出)
*********************************************************************** * Title : ZSDF003 ...
- EMIS系统运行时提示【无法验证发行者,您确实要运行此软件吗? 】
无法验证发行者,您确实要运行此软件吗? 遇到这个提示你怎么办? 运行 gpedit.msc 进入组策略用户配置 ==>管理模板==> winows组件 ==> 附件管理器在 &quo ...
- problem-record-mysql
#!/bin/bash # # Update_Problem - updates problem record in database ################################ ...
- C# Arraylist + struct 综合练习 枚举ENUE 递归
枚举类型 一组常量的组合, 在不制定任何索引的情况下,默认第一个字段从0开始,之后的依次+1 在指定了某个索引的情况下,之后的依次+1 若之前定义的某字段的索引指向了之后的某个默认字段,那么他俩完全相 ...
- c#简易计算器
微软MSDN的代码库就有示例 http://code.msdn.microsoft.com/Simple-Calculator-54ec8e4a using System; using System. ...
- GridView如何合并同类项
/// <summary> /// 合并GridView中某列相同信息的行(单元格) /// </summary> /// <param name ...
- jsonp注意事项
自己测试的: <?php '); } }); } </script> <!DOCTYPE htm ...