wcf服务端

1,新建一个“windows窗体程序”名称为WCFServer1。

2,然后添加一个“WCF服务”名称为Service1。具体步骤为:解决方案试图中,选中“WCFServer1”项目,右键,在弹出菜单中选择“添加->新建项”。

3,双击主窗体,在它的Load事件中编写代码启动wcf服务:BasicHttpBinding方式启动wcf服务。此文件代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.ServiceModel;

using System.ServiceModel.Description;

namespace WCFServer1

{

public partial class Form1 : Form

{

ServiceHost m_ServiceHost;

publicForm1()

{

InitializeComponent();

}

privatevoid Form1_Load(objectsender, EventArgs e)

{

m_ServiceHost = new ServiceHost(typeof(Service1));//Service1是wcf服务的类名称

//BasicHttpBinding方式启动wcf服务

ServiceMetadataBehaviormetadataBehavior;

metadataBehavior =m_ServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

if(metadataBehavior == null)

{

metadataBehavior = new ServiceMetadataBehavior();

metadataBehavior.HttpGetEnabled= true;

metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1"));

m_ServiceHost.Description.Behaviors.Add(metadataBehavior);

}

else

{

metadataBehavior.HttpGetEnabled= true;

metadataBehavior.HttpGetUrl = new Uri(string.Format("http://localhost:10085/WCFHostServer/Service1"));

}

m_ServiceHost.Open();

}

privatevoid FormService_FormClosing(object sender, FormClosingEventArgse)

{

if(m_ServiceHost != null)

{

m_ServiceHost.Close();

m_ServiceHost = null;

}

}

}

}

4,在 IService1.cs中增加一个方法GetSvrTime

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WCFServer1

{

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。

[ServiceContract]

public interface IService1

{

[OperationContract]

voidDoWork();

[OperationContract]

stringGetSvrTime();

}

}

5, 在Service1.cs中实现接口的方法GetSvrTime,获取服务器时间

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WCFServer1

{

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。

public class Service1 : IService1

{

public void DoWork()

{

}

public string GetSvrTime()

{

returnDateTime.Now.ToString();

}

}

}

第二步:WCF客户端

1,新建一个“windows窗体程序”命名为WCFClient1。

2,在Debug中运行WCFHostServer窗体(这一步很重要),然后在项目WCFHostClient中右键--》“添加服务引用”,在地址填入以BasicHttpBinding绑定方式启动wcf服务的地址http://localhost:10085/WCFHostServer/Service1,点击“前往”,然后连接上后点击“确定”,在WCFHostClient项目中就会增加一个Service References(ServiceReference1)

3,主窗体上增加一个按钮,双击此按钮添加响应事件。

using WCFClient1.ServiceReference1;

using System.ServiceModel;

//...

private voidbutton1_Click(object sender, EventArgs e)

{

//basicHttpBinding绑定方式的通讯单工

Service1Clienthost = new ServiceReference1.Service1Client();

stringhtime = host.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间

MessageBox.Show("basicHttpBinding" + "@" + htime);

//NetTcpBinding绑定方式的通讯双工

IService1m_Innerclient;

ChannelFactory<IService1> m_ChannelFactory;

NetTcpBindingbinding = new NetTcpBinding();

UribaseAddress = new Uri("net.tcp://localhost:10085/WCFHostServer/Service1");

m_ChannelFactory = new ChannelFactory<IService1>(binding, newEndpointAddress(baseAddress));

m_Innerclient =m_ChannelFactory.CreateChannel();

stringhtime1 = m_Innerclient.GetSvrTime();//调用GetSvrTime获取到wcf服务器上的时间

MessageBox.Show("NetTcpBinding" + "@" + htime1);

}

WCF只有服务端响应的更多相关文章

  1. django中“url映射规则”和“服务端响应顺序”

    1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单.      查看方式:         import sys        print sys.path   ...

  2. day112:MoFang:种植园使用websocket代替http&服务端基于flask-socketio提供服务&服务端响应信息&种植园页面显示初始化

    目录 1.种植园使用websocket代替http 2.服务端基于socket提供服务 3.服务端响应信息 4.种植园页面展示 1.种植园使用websocket代替http 我们需要完成的种植园,是一 ...

  3. 一: WCF的服务端与客户端在通信时有三种模式:请求响应模式、数据报模式和双工通讯模式。

    说一下基本知识,  1.如果想要将当前接口作为wcf服务器,则一定要加上[ServiceContract] 契约 2.要想将方法作为wcf服务方法发布给外部调用,则一定要加上    [Operatio ...

  4. ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析

    ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Core整个WCF host. 因 ...

  5. ajax客户端请求与服务端响应浅谈

    AJAX,即Asynchronous Javascript And XML,AJAX本质是在HTTP协议的基础上以异步的方式与服务器进行通信. 所谓的异步,是指某段程序执行不会阻塞其他程序执行,其表现 ...

  6. Photon Server 实现注册与登录(四) --- 服务端响应登陆和注册

    前面已经整理过了服务端代码,MyGameServer.cs 和 ClientPeer.cs 对请求和响应进行了拆分.接下来处理对前端的响应 一.响应登陆请求 之前整理中,响应前端请求主要在类Clien ...

  7. JSP基础知识➣客户端请求与服务端响应(三)

    JSP客户端请求 浏览器请求服务器端,信息头的一些重要内容,在以后的网络编程中将会经常见到这些信息: Accept:指定浏览器或其他客户端可以处理的MIME类型.它的值通常为 image/png 或 ...

  8. dubbo服务端响应超时错误一例记录

    错误描述: Portlet J2AppsPortlet::QuickStartPortlet not available: Waiting server-side response timeout. ...

  9. Servlet的服务端响应

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/server-response.html: 当一个Web服务器对浏览器响应一个HTTP请求时,响应 ...

  10. 简单的node 服务端 响应get,返回json数据;

    原文:http://blog.csdn.net/xn_28/article/details/50837019 const http = require('http'); const hostname ...

随机推荐

  1. CentOS 7安装MariaDB 10详解以及相关配置-九五小庞

    第一步:添加 MariaDB yum 仓库 首先在CentOS操作系统中/etc/yum.repos.d/目录下添加 MariaDB 的YUM配置文件MariaDB.repo文件. vi /etc/y ...

  2. git使用每次输入密码问题

    原因:在初始化配置Git用户信息时,设置了用户密码 解决方式:重置用户密码,并设置为空 $ ssh-keygen -p Enter file in which the key is (/c/Users ...

  3. unity 单位 像素 分辨率 正交摄像机size 之间的关系

    这个5表示摄像机的视距 代表摄像机拍摄的一般高度 如果16:9的分辨率,100像素为1 unity单位的情况 填充摄像机所需图片大小尺寸为:高度:5*2*100 = 1000    宽度:1000*1 ...

  4. CF Round946 (Div. 3)A

    Phone Desktop 题目描述 Little Rosie has a phone with a desktop (or launcher, as it is also called). The ...

  5. 搭建一个属于自己的docker镜像加速器

    近期国内的docker镜像加速器已经失效,导致docker镜像拉不下来. 如图所示,阿里云镜像加速器已经失效了: (有人可能会问我,为什么不直接自建harbor仓库把镜像包都放在harbor上,其实这 ...

  6. 小程序如何集成即构IM实现即时通讯发消息聊天

    之前的文章已经介绍了如何实现Web端的即时通讯IM,为了让大家全面的体验通信互动的快乐. 本文介绍如何使用 ZIM SDK 快速实现实现小程序端的基本的消息收发功能,在微信中实现一个mini版微信,也 ...

  7. 循环神经网络的从零开始实现(RNN)

    博客地址:https://www.cnblogs.com/zylyehuo/ 参考 <动手学深度学习>第二版 代码总览 %matplotlib inline import math imp ...

  8. MyEMS 开源能源管理系统:基于 4G 无线传感网络的能源数据闭环管理方案

    一.MyEMS 系统核心架构与技术特性​ MyEMS 作为开源能源管理领域的标杆性解决方案,采用 Python 语言构建核心框架,依托 Django 后端与 React 前端技术栈实现跨平台兼容.其模 ...

  9. 建筑矿山设备工厂南京厂区能源管理升级:MyEMS 开源系统的实践与成效

    一.工厂概况:从全球化布局到本土化深耕 作为全球领先的采矿与基础设施技术企业,建筑矿山设备工厂深耕中国市场三十余年,其南京厂区已成为亚太区核心制造与研发中心.自 1993 年在南京经开区设立首家工厂以 ...

  10. MyEMS开源能源管理系统核心代码解读008

    本期解读: 计量表能耗分析算法:myems/myems-api/reports/meterenergy.py 这段代码是一个用于生成能源报告的Python类,这个类名为Reporting,主要功能是通 ...