前言:

  以各种应用程序做自我寄宿的宿主原理方法大同小异,故:这儿直接上案例!


步骤一:创建服务契约和服务

1.新建解决方案:添加WCF服务库项目。

2、为了演示,我把自动生成的接口以及实现接口的类删除,自己添加一个WCF Service

3、撰写服务函数(同时,因为将原有的自动生成的接口与类删除了,故而需要将配置文件作相应的改动:)

namespace wcfself02
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService02" in both code and config file together.
[ServiceContract]
public interface IMe02
{
[OperationContract]
string showName(string str);
}
}
namespace wcfself02
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service02" in both code and config file together.
public class Me02 : IMe02
{

string IMe02.showName(string str)
{
string strr;
strr = str;
Console.WriteLine(strr);
return "啦啦啦" + strr;
}
}
}


步骤二:创建服务宿主

创建一个Windows应用程序来实现WCF服务的自我寄宿方式【添加Windows应用程序,引入WcfService.Library_01的引用,添加using System.ServiceModel;库文件引用。】,具体的实现以及代码如下所示:

Program.cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wcfhost02
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

switch (CheckWindowsIdentity())
{
case 0: Application.Run(new Form1()); break;
case 1: Application.Exit(); break;
}

}

static int CheckWindowsIdentity()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return 0; //管理员

//普通用户,使用启动对象启动程序,以确保使用管理员身份运行创建启动对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try
{
Process.Start(startInfo);
return 1;//普通用户
}
catch
{
return -1;//无权运行或用户放弃
}

}

}
}

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using wcfself02;
namespace wcfhost02
{
public partial class Form1 : Form
{
Me02 MM = new Me02();
ServiceHost host = null;
public Form1()
{
InitializeComponent();
}

private void open_Click(object sender, EventArgs e)
{
if(host==null)
{
host = new ServiceHost(typeof(Me02));
host.Open();
}
}

private void close_Click(object sender, EventArgs e)
{
if(host !=null)
{
host.Close();
host = null;
}
}

private void Form1_Load(object sender, EventArgs e)
{
close_Click(sender, e);
}
}
}

利用配置文件的形式的方式进行终结点的添加和服务行为的定义

App.config[其中一种配置方式]:

<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name ="wcfself02.Me02">
<endpoint address="http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/"
binding="wsHttpBinding"
contract="wcfself02.IMe02"/>
</service>
</services>
</system.serviceModel>
</configuration>

编译宿主程序,在所在的文件位置处,用管理员身份打开旗.exe文件;点击“Open”输入配置文件中的地址(http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/metadata),可打开,点击“Close”,无法连接,说明创建成功!!

App.config[其中第一种配置方式]:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="wcfself02.Me02">
<endpoint address="" binding="basicHttpBinding" contract="wcfself02.IMe02">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/" />
</baseAddresses>
</host>
</service>
<service name="wcfself02.Me02">
<endpoint address="" binding="basicHttpBinding" contract="wcfself02.IMe02">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

编译宿主程序,在所在的文件位置处,用管理员身份打开旗.exe文件;点击“Open”输入配置文件中的地址(http://localhost:8733/Design_Time_Addresses/wcfself02/Me02/),可打开,点击“Close”,无法连接,说明创建成功!!

特别注意!!在运行宿主应用程序时,一定以管理员权限运行宿主应用程序!!!


步骤三:创建客户端(引用服务,验证上面创建的服务)

创建一个控制台应用程序作为客户端引用上述的服务,添加服务应用时注意使服务是开启状态!!!

引用服务端的函数,可实现相应功能,这里不多赘述!

【欢迎转载】

转载请表明出处: 乐学习

WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Windows应用程序宿主】的更多相关文章

  1. UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例

    Note:不知道为什么通过Txt读取的JsonString,如果TXT 不是ANSI编码的话,会报JsonArrayStringToUStruct  Unable to parse. bool UWg ...

  2. WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Console应用为宿主】

    前言: 由于最近的项目 中需要用到WCF,所以又回头翻了翻,阅读了大量园中大神的博文,故而做个总结. 谬误之处,万望不吝指教! 闲话不叙! 一.寄宿(Host)WCF服务  1)一种是为一组WCF服务 ...

  3. WCF 学习总结1 -- 简单实例

    从VS2005推出WCF以来,WCF逐步取代了Remoting, WebService成为.NET上分布式程序的主要技术.WCF统一的模型整合了以往的 WebService.Remoting.MSMQ ...

  4. 创建WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  5. WCF服务自我寄宿

    WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为 ...

  6. WCF服务自我寄宿 Windows服务

    WCF寄宿有自我寄宿跟IIS寄宿 服务代码: [ServiceContract] ---服务契约 public interface ICustomerService { [OperationContr ...

  7. wcf系列(一)--- 寄宿方式

    一.自我寄宿(self-hosting) 1.wcf采用基于终结点(Endpoint)的通信手段:终结点由:地址(Address)+绑定(Binding)+契约(Contract)组成:  Enpoi ...

  8. WCF之Host宿主

    Self_hosting自托管宿主. 过程:手动创建Host实例,把服务端点添加到Host实例上,把服务接口与Host关联. 一个Host只能指定一个服务类型,但是可以添加多个服务端点,也可以打开多个 ...

  9. wcf iis host 打开exe失败 不能显示界面

    最近谷歌没法用了,我的freegate经常性的崩溃 无奈之下,用了必应,貌似也不错 http://stackoverflow.com/questions/8414514/iis7-does-not-s ...

随机推荐

  1. Android Studio 使用笔记:工具窗口浮动与布局恢复

    Android Studio 的工具窗口都可以变成浮动窗口,如果有多个显示器的话,这种模式非常方便.方法如下: 然后就像下图那样,可以拖拽了.如果你不小心关了,没有关系.再次点击工具栏,浮动窗口就回显 ...

  2. Google Code Jam 2014 资格赛:Problem D. Deceitful War

    This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...

  3. phpexcel导入xlsx文件报错xlsx is not recognised as an OLE file 怎么办

    最初的做法  代码如下 1 include 'classes/PHPExcel/IOFactory.php'; 2 $inputFileName = $target; 3 $objReader = n ...

  4. JSP开发中的基础语法

    JSP 语法 JSP开发中的基础语法. 脚本程序 脚本程序可以包含任意量的Java语句.变量.方法或表达式,只要它们在脚本语言中是有效的. 脚本程序的语法格式: <% 代码片段 %> 或者 ...

  5. vue实践---vue不依赖外部资源实现简单多语

    vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...

  6. css3中font-face属性的用法详解

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...

  7. Sitemesh3的使用心得

    项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...

  8. 【BZOJ3168】[Heoi2013]钙铁锌硒维生素 高斯消元求矩阵的逆+匈牙利算法

    [BZOJ3168][Heoi2013]钙铁锌硒维生素 Description 银河队选手名单出来了!小林,作为特聘的营养师,将负责银河队选手参加宇宙比赛的饮食.众所周知,前往宇宙的某个星球,通常要花 ...

  9. [转]jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别

    转自:http://www.cnblogs.com/keyi/p/5933981.html   jquery中innerWidth(),outerWidth(),outerWidth(true)和wi ...

  10. Pipeline outbound

    netty源码死磕8 Pipeline outbound 出站流程揭秘 1. Pipeline outbound流程 1.1. 出站的定义 简单回顾一下. 出站(outbound) 操作,通常是处于上 ...