以各种应用程序做自我寄宿的宿主原理方法大同小异,故:这儿直接上案例!
步骤一:创建服务契约和服务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>
<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>