前言:

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


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

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. Trie树学习

    这几天在看Hadoop的排序,用到了有TotalSortPartition,其中用到了一种叫做trie树的数据结构,每次看到这种自己之前没有听过的数据结构就想去看一下原理,然后再网上看几篇博客,有时间 ...

  2. 题外话:计算密集型 vs IO密集型

    我们把任务分为计算密集型和IO密集型,erlang作为IO密集型的语言,适合网关等相关的场景,而对计算达到某一量级后,可能处理效率下降的很明显. erlang不适合数值计算.erlang是解释型的,虽 ...

  3. 如何使用eclipse创建Maven工程及其子模块

    http://blog.csdn.net/jasonchris/article/details/8838802 http://www.tuicool.com/articles/RzyuAj 1,首先创 ...

  4. _DataStructure_C_Impl:图的最小生成树

    #include<stdio.h> #include<stdlib.h> #include<string.h> typedef char VertexType[4] ...

  5. 【很强大的Android图表引擎 - AChartSDK】

    在手机移动App开发中,图表在app中越来越占领举足轻重的地图.而在Android领域.AchartEngine 图表引擎可谓无人不知无人不晓. 可是今天就给各位推荐更为强大的图表引擎. 为什么说更为 ...

  6. python多进程-----multiprocessing包

    multiprocessing并非是python的一个模块,而是python中多进程管理的一个包,在学习的时候可以与threading这个模块作类比,正如我们在上一篇转载的文章中所提,python的多 ...

  7. Django多对多的创建

    1.多对多创建的应用场景: 在某表中创建一行数据是,有一个可以多选的下拉框 例如:创建用户信息,需要为用户指定多个爱好 2.创建方式: 方式一:自定义关系表,手动创建一张表用于关联其他多张表的关系 c ...

  8. SAP后台作业记录操作

    [转http://blog.163.com/liang_ce_521@126/blog/static/709202152013073376596/]后台作业信息存储在透明表TBTCP(批作业步骤概述) ...

  9. Springmvc注解启用

      http://www.blogbus.com/wanping-logs/235898637.html 使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用 ...

  10. 找出旋转有序数列的中间值python实现

    题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...