需求:在同一台机子上,有一个B/S程序,和一个C/S程序(不要问为什么,事实就是这样),B/S程序需要主动和C/S程序通信(C/S程序主动与B/S程序通信的情况这里暂不讨论)。

下面以最快的速度写一个B/S程序和一个C/S程序实现,具体细节不解释,自己翻书看去。

一、建了两个工程,如下图所示:

二、先看C/S程序,跑起来就是这样的,如下图所示(简单吧):

程序结构如下图:

WCF的接口WCFServer、数据结构DataStruct、Winform程序Client

IClientServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using DataStruct; namespace WCFServer
{
[ServiceContract]
public interface IClientServer
{
[OperationContract]
TestData Test(string param);
}
}

ClientServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using DataStruct; namespace WCFServer
{
[ServiceBehavior()]
public class ClientServer : IClientServer
{
/// <summary>
/// 测试
/// </summary>
public TestData Test(string param)
{
TestData data = new TestData();
data.Name = param;
data.Code = "";
return data;
}
}
}

TestData:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace DataStruct
{
[DataContract]
public class TestData
{
[DataMember]
public string Name { get; set; } [DataMember]
public string Code { get; set; }
}
}

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WCFServer; namespace CSServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
OpenClientServer();
} /// <summary>
/// 启动服务
/// </summary>
private void OpenClientServer()
{
WSHttpBinding wsHttp = new WSHttpBinding();
wsHttp.MaxBufferPoolSize = ;
wsHttp.MaxReceivedMessageSize = ;
wsHttp.ReaderQuotas.MaxArrayLength = ;
wsHttp.ReaderQuotas.MaxStringContentLength = ;
wsHttp.ReaderQuotas.MaxBytesPerRead = ;
wsHttp.ReaderQuotas.MaxDepth = ;
wsHttp.ReaderQuotas.MaxNameTableCharCount = ;
wsHttp.CloseTimeout = new TimeSpan(, , );
wsHttp.OpenTimeout = new TimeSpan(, , );
wsHttp.ReceiveTimeout = new TimeSpan(, , );
wsHttp.SendTimeout = new TimeSpan(, , );
wsHttp.Security.Mode = SecurityMode.None; Uri baseAddress = new Uri("http://127.0.0.1:9999/clientserver");
ServiceHost host = new ServiceHost(typeof(ClientServer), baseAddress); ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb); ServiceBehaviorAttribute sba = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
sba.MaxItemsInObjectGraph = ; host.AddServiceEndpoint(typeof(IClientServer), wsHttp, ""); host.Open();
}
}
}

C/S程序跑起来就OK了。

三、B/S程序

程序结构如下图:

WCF的接口DataService、Web程序

DataService需要引用数据结构DataStruct和WCFServer,是C/S程序编译后的dll拿过来的,Web需要引用DataStruct,如下图:

ClientServer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using DataStruct;
using WCFServer; namespace DataService
{
public class ClientServer
{
ChannelFactory<IClientServer> channelFactory;
IClientServer proxy; /// <summary>
/// 创建连接客户终端WCF服务的通道
/// </summary>
public void CreateChannel()
{
string url = "http://127.0.0.1:9999/clientserver";
WSHttpBinding wsHttp = new WSHttpBinding();
wsHttp.MaxBufferPoolSize = ;
wsHttp.MaxReceivedMessageSize = ;
wsHttp.ReaderQuotas.MaxArrayLength = ;
wsHttp.ReaderQuotas.MaxStringContentLength = ;
wsHttp.ReaderQuotas.MaxBytesPerRead = ;
wsHttp.ReaderQuotas.MaxDepth = ;
wsHttp.ReaderQuotas.MaxNameTableCharCount = ;
wsHttp.SendTimeout = new TimeSpan(, , );
wsHttp.Security.Mode = SecurityMode.None; channelFactory = new ChannelFactory<IClientServer>(wsHttp, url);
foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
{
DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = ;
}
}
} /// <summary>
/// 测试
/// </summary>
public TestData Test(string param)
{
proxy = channelFactory.CreateChannel(); try
{
return proxy.Test(param);
}
catch (Exception ex)
{
return null;
}
finally
{
(proxy as ICommunicationObject).Close();
}
}
}
}

TestController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataService;
using DataStruct; namespace BSClient.Controllers
{
public class TestController : Controller
{ public ActionResult Index()
{
ClientServer client = new ClientServer();
client.CreateChannel();
ViewData["data"] = client.Test("ABCD"); return View();
} }
}

Index.cshtml:

@using DataStruct;
@{
ViewBag.Title = "Index";
TestData data = ViewData["data"] as TestData;
if (data == null) { data = new TestData(); }
} <h2>Index</h2> <h2>@data.Name</h2>
<h2>@data.Code</h2>

先启动C/S程序,再启动B/S程序,B/S的运行结果如下图所示:

上面以最快的速度写了一个WCF的简单的Demo,程序跑起来了,后面的再慢慢学吧。

WCF快速上手的更多相关文章

  1. WCF快速上手(二)

    服务端是CS程序,客户端(调用者)是BS程序 一.代码结构: 二.服务接口Contract和实体类Domain INoticeService: using Domain; using System; ...

  2. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  3. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  4. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手

    原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...

  5. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  6. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...

  7. Git版本控制Windows版快速上手

    说到版本控制,之前用过VSS,SVN,Git接触不久,感觉用着还行.写篇博文给大家分享一下使用Git的小经验,让大家对Git快速上手. 说白了Git就是一个控制版本的工具,其实没想象中的那么复杂,咱在 ...

  8. Objective-C快速上手

    最近在开发iOS程序,这篇博文的内容是刚学习Objective-C时做的笔记,力图达到用最短的时间了解OC并使用OC.Objective-C是OS X 和 iOS平台上面的主要编程语言,它是C语言的超 ...

  9. Netron开发快速上手(二):Netron序列化

    Netron是一个C#开源图形库,可以帮助开发人员开发出类似Visio的作图软件.本文继前文”Netron开发快速上手(一)“讨论如何利用Netron里的序列化功能快速保存自己开发的图形对象. 一个用 ...

随机推荐

  1. 【C语言学习】《C Primer Plus》第10章 数组和指针

    学习总结 1.数组初始化方式: int a[]={1,2,3} int a[SIZE]={1,2,3} //SIZE是宏定义,数组初始化个数不能大于SIZE,否则报错:当个数小 //SIZE,自动补0 ...

  2. 以代码爱好者角度来看AMD与CMD

    随着浏览器功能越来越完善,前端已经不仅仅是切图做网站,前端在某些方面已经媲美桌面应用.越来越庞大的前端项目,越来越复杂的代码,前端开发者们对于模块化的需求空前强烈.后来node出现了,跟随node出现 ...

  3. 作业七:团队项目——Alpha版本冲刺阶段-09

    昨天进展:代码编写. 今天安排:代码编写.

  4. Android自定义View的构造函数

    自定义View是Android中一个常见的需求,每个自定义的View都需要实现三个基本的构造函数,而这三个构造函数又有两种常见的写法. 第一种 每个构造函数分别调用基类的构造函数,再调用一个公共的初始 ...

  5. HTML5打造的炫酷本地音乐播放器-喵喵Player

    将之前捣腾的音乐频谱效果加上一个播放列表就成了现在的喵喵播放器(Meow meow Player,额知道这名字很二很装萌~),全HTML5打造的网页程序,可本地运行也可以挂服务器上用. 在线Demo及 ...

  6. 深入了解Java程序执行顺序

    Java中main方法,静态,非静态的执行顺序详解 Java程序运行时,第一件事情就是试图访问main方法,因为main相等于程序的入口,如果没有main方法,程序将无法启动,main方法更是占一个独 ...

  7. 每天一个linux命令(53):route命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  8. Atitit usrqbg1834 html的逻辑化流程化 规范标准化解决方案

    Atitit usrqbg1834 html的逻辑化流程化 规范标准化解决方案 常用指令1 ..v-if.v-else指令2 v-for指令3 MVVM大比拼4 常用指令 本来按照Vue文档说明,常用 ...

  9. 对于System.Net.Http的学习(二)——使用 HttpClient 进行连接

    对于System.Net.Http的学习(一)——System.Net.Http 简介  使用 HttpClient 进行连接 使用 System.Net.Http 命名空间中的 HttpClient ...

  10. String详解

    在开发中,我们都会频繁的使用String类,掌握String的实现和常用方法是必不可少的,当然,我们还需要了解它的内部实现. 一. String的实现 在Java中,采用了一个char数组实现Stri ...