浅谈辄止WCF:完成最基本的WCF项目(1)
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。
WCF的所有服务都会公开契约。契约包含以下四种类型
1.服务契约
2.数据契约
3.错误契约
4.消息契约
今天主要介绍服务契约实现WCF项目,服务契约描述了客户端能够执行的服务操作。
项目结构截图:

1.定义和实现服务契约
通过将ServiceContractAttribute属性标记到接口或者类型上。遵循了面向服务的原则,所有的契约必须要明确要求:只有接口和类可以标记为Servicecontract属性,从而为WCF服务,即便标记了ServiceContract属性,类的所有成员也不一定就是契约的一部分,我们必须使用OperationContract属性表明哪些方法需要暴露为WCF契约中的一部分。
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WCFServiceHosting
{
[ServiceContract]//标记为服务,不标记则客户端无法访问
public interface IDataExChangeService
{
[OperationContract]
void ShowMsg(string str);
//不标记不会成为契约一部分
void ShowMsg1(string str);
}
}
需要一个类去实现这个接口,表明方法的实际作用,我们只需要去将暴露为服务的方法实现。这里只是简单的将传入的参数写入了文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WCFServiceHosting
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DataExChangeService : IDataExChangeService
{
private FileStream fs;
public void ShowMsg(string str)
{
fs = new FileStream("c://Warrenwell.log//log.txt",FileMode.Create,FileAccess.ReadWrite);
byte[] messages = System.Text.Encoding.Default.GetBytes(str);
fs.Write(messages, , messages.Length);
fs.Close();
} public void ShowMsg1(string str)
{
throw new NotImplementedException();
}
}
}
2.托管
wcf服务不能凭空存在,每个服务都必须托管到windows进程中,该进程叫做宿主进程。
我们这里使用自托管,我们托管到windows窗体应用程序中,托管应用程序配置文件通常会列出所有希望托管和公开的服务类型
<services>
<service name="WCFServiceHosting.DataExChangeService">
<host>
<baseAddresses>
<add baseAddress="http://192.168.0.44:8002/ReceiveMessage/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFServiceHosting.IDataExChangeService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
在windows窗体应用程序中托管服务的代码为
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WCFServiceHosting
{
public partial class ServiceHostHandler:ServiceBase
{
private ServiceHost host;
private System.Timers.Timer timer = new System.Timers.Timer();
private const int timerInterval = ;
public ServiceHostHandler()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
if (host != null)
{
host.Close();
}
host = new ServiceHost(typeof(DataExChangeService));
host.Open();
timer.Interval = timerInterval;
timer.Start();
}
catch(Exception ex)
{
throw;
}
}
protected override void OnStop()
{
try
{
if (timer.Enabled)
{
timer.Close();
timer.Dispose();
}
if (host != null)
{
host.Close();
host = null;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
最后在窗体程序的program.cs文件中设置为启动这个窗体程序,由于服务托管于此窗体程序中,所以相当于启动了服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WCFServiceHosting
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ServiceBase[] serviceToRun;
serviceToRun = new ServiceBase[]
{
new ServiceHostHandler()
};
ServiceBase.Run(serviceToRun);
}
}
}
3.将程序做成服务安装到服务器中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WCFServiceHosting
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
}
namespace WCFServiceHosting
{
partial class ProjectInstaller
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller
//
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;
//
// serviceInstaller
//
this.serviceInstaller.Description = "Exchange data with Hospital Information System.";
this.serviceInstaller.DisplayName = "";//服务名字
this.serviceInstaller.ServiceName = "ReceiveMessage";
this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller,
this.serviceInstaller});
} #endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller;
private System.ServiceProcess.ServiceInstaller serviceInstaller;
}
}
4.安装我们的WCF服务到服务器上
打开命令提示符,右击管理员身份运行,输入cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
继续输入InstallUtil.exe C:\Users\123\Documents\VisualStudio2017\Projects\P2P\WindowsFormsApp2\bin\Debug\WindowsFormsApp2.exe
由于我的服务已经存在了所以就提示我已经存在。

之后就能在计算机管理——服务里面看到我们的123服务了,打开123服务后,别人就能够调用了,关于调用方法,下章继续,拜拜=V=。

项目整体源码下载地址 http://download.csdn.net/download/china_zhangdapao/10255850
浅谈辄止WCF:完成最基本的WCF项目(1)的更多相关文章
- 浅谈WebService的版本兼容性设计
在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...
- 浅谈线程池(中):独立线程池的作用及IO线程池
原文地址:http://blog.zhaojie.me/2009/07/thread-pool-2-dedicate-pool-and-io-pool.html 在上一篇文章中,我们简单讨论了线程池的 ...
- 浅谈 Fragment 生命周期
版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...
- 浅谈 LayoutInflater
浅谈 LayoutInflater 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/View 文中如有纰漏,欢迎大家留言指出. 在 Android 的 ...
- 浅谈Java的throw与throws
转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...
- 浅谈SQL注入风险 - 一个Login拿下Server
前两天,带着学生们学习了简单的ASP.NET MVC,通过ADO.NET方式连接数据库,实现增删改查. 可能有一部分学生提前预习过,在我写登录SQL的时候,他们鄙视我说:“老师你这SQL有注入,随便都 ...
- 浅谈angular2+ionic2
浅谈angular2+ionic2 前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别. 1. 项目所用:angular2+ionic2 ...
- iOS开发之浅谈MVVM的架构设计与团队协作
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- Linux特殊符号浅谈
Linux特殊字符浅谈 我们经常跟键盘上面那些特殊符号比如(?.!.~...)打交道,其实在Linux有其独特的含义,大致可以分为三类:Linux特殊符号.通配符.正则表达式. Linux特殊符号又可 ...
随机推荐
- JavaScript 如何工作:渲染引擎和性能优化技巧
翻译自:How JavaScript works: the rendering engine and tips to optimize its performance 这是探索 JavaScript ...
- Docker 镜像的制作和使用
镜像 Layer(层) 镜像里的内容是按「层」来组织的,「层」可以复用,一个完整的镜像也可以看做是一个「层」.多个「层」叠加在一起就形成了一个新的镜像,这个镜像也可以作为别的镜像的基础「层」进行更加复 ...
- PTA数据结构之 List Leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- P4313 文理分科 最小割
$ \color{#0066ff}{ 题目描述 }$ 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格 ...
- .net core UseHttpsRedirection() 正式环境无效
莫名其妙遇到这样的问题.这样的配置在本地可以,正式环境就不行了. ··· public void Configure(IApplicationBuilder app, Microsoft.AspNet ...
- WCF 客户端连接慢
WCF客户端第一次连接超过1分钟,以后再连接就快了. 在 Config中加入 <basicHttpBinding> <binding name="BasicHttpBind ...
- css文章
前端HTML-CSS规范:https://yq.aliyun.com/articles/51487 jQuery+d3绘制流程图:https://blog.csdn.net/zitong_ccnu/a ...
- 安装python发行版本,并用conda来管理Environments,Python,packages
简介:anaconda指的是一个开源的python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大(约 515 MB ...
- shim和polyfill,前端术语
最近项目临近发布,JS的bug大都修改完毕,终于进入了我在这家公司实习+入职为数不多的摸鱼时刻.(想想真是有点感人啊) 因为项目要兼容IE8,所以我们的代码里常常要用到 shim 以支持ES5 的相关 ...
- JavaWeb学习笔记(十三)—— JDBC时间类型的处理
一.Java中的时间类型 Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后 ...