浅谈辄止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特殊符号又可 ...
随机推荐
- 阿里云RDS外网无法访问解决办法
为了安全起见,阿里云的RDS数据库设置只能内网连接,那么为了方便查询数据,我每次都去连接内网服务器远程桌面. 这次我因为网络问题,远程桌面非常不稳定,一会掉线一会掉线,只能另想办法. 同事那里有个批处 ...
- SpringMVC+Hibernate 使用 session.update(obj) 未更新的问题
1.使用spring控制事务 2.使用session.update(obj)执行更新 spring事务配置: <bean id="transactionBese" class ...
- python-输入
1. python2版本中 咱们在银行ATM机器前取钱时,肯定需要输入密码,对不? 那么怎样才能让程序知道咱们刚刚输入的是什么呢?? 大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入 ...
- 洛谷P2770 航空路线问题(费用流)
传送门 完了这题好厉害……字符串什么的好麻烦…… 要求从$1$到$n$的路径,不重复,经过边数最多 每一个点拆成两个,$A_i,B_i$,然后$A_i$到$B_i$连容量为$1$,费用为$1$的边,保 ...
- 解决因为链表过长,sql查询慢的问题
/** * 解决因为链表过长,sql查询慢的问题 * 使用分治算法,先切分链表,然后查询结果,最后合并结果 * * @author lingpy * @since 1.0 */public clas ...
- MongDB集群部署
http://blog.csdn.net/luonanqin/article/details/8497860 参数解释: dbpath:数据存放目录 logpath:日志存放路径 pidfilepat ...
- CF886E Maximum Element
$ \color{#0066ff}{ 题目描述 }$ 从前有一个叫Petya的神仙,嫌自己的序列求max太慢了,于是将序列求max的代码改成了下面这个样子: int fast_max(int n,in ...
- [ADB Shell]Android Debug Bridge常用命令
ADB用法 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...
- go的三个常用命令go run go build go install
go的三个常用命令 go run go build go install 命令源码文件:含有 main函数 的文件 库源码文件:不包含 main函数 的文件, 主要用于编译成静态文件.a供其他包调用 ...
- XMLHttpRequest 与 Ajax 概要
关于XMLHttpRequest 开发者使用XMLHttpRequest对象与服务端进行交互(发送http请求.接受返回数据等),可以在不打断用户下一步操作的情况下刷新局部页面.XMLHttpRequ ...