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服务寄宿应用程序的更多相关文章

  1. 创建WCF服务寄宿到IIS

    一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的win ...

  2. WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘

    原文:WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘 通过<再谈IIS与ASP.NET管道>的介绍,相信读者已经对IIS和ASP.NET的请求处理管道有了一个大致 ...

  3. WCF服务寄宿到IIS

    一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的wind ...

  4. WCF服务寄宿IIS与Windows服务 - C#/.NET

    WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...

  5. 关于WCF引用方式之WCF服务寄宿控制台

    1.创建解决方案WCFService 依次添加四个项目,如上图,Client和Hosting为控制台应用程序,Service和Service.Interface均为类库. 2.引用关系 Service ...

  6. WCF服务寄宿IIS与Windows服务

      WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的 ...

  7. WCF服务寄宿Windows

    windows服务的介绍 Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合.它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件日志.计 ...

  8. 使用C#创建WCF服务控制台应用程序

    本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Info ...

  9. 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)

    原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...

随机推荐

  1. WinForm开发框架--动态读取DLL模式

    1\ WinForm开发框架--动态读取DLL模式   http://www.2cto.com/kf/201306/217199.html 2\ 广州爱奇迪     http://www.iqidi. ...

  2. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  3. spring的对象属性相同(类型,名字)拷贝

    A类: package test; /** * Created by gmq on 2015/12/4. */ public class A { private String aa; private ...

  4. Centos7 修改SSH 端口

    修改/etc/ssh/sshd_config vi /etc/ssh/sshd_config #Port 22 //这行去掉#号,防止配置不好以后不能远程登录,还得去机房修改,等修改以后的端口能使用以 ...

  5. 手动安装python后,交互模式下退格键乱码

    没有安装readline相关的模块 yum install readline readline-devel 再重新变异安装python就可以了

  6. Qt 使用sqlserver

    1. pro 添加 QT       +=sql 2. void MainWindow::connectSqlServer() { QSettings *setIni = new QSettings( ...

  7. MySQL 主键范围查找问题

    背景: 今天遇到一个主键范围查找的情况: id是主键,每次取10000.上面的这个查询id范围越往后面消耗的时间越久.通过id自增主键去查找数据应该不会出现这个现象的.以前都没有注意这个奇怪的现象,现 ...

  8. monitor system

    #!/bin/bash # #Snapshot_Stats - produces a report for system stats # This report will mail to root. ...

  9. ATS(App Transport Security)对HTTP协议屏蔽引起的问题

    一.问题描述 在学习网络处理的过程,发现代码都没错,运行时会收到如下错误提示: App Transport Security has blocked a cleartext HTTP (http:// ...

  10. oracle触发器设置uuid变量

    create or replace trigger tri_org_department after insert or update on bjlt.sys_org for each row dec ...