平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些wcf相关的文章,希望能给有需要的人带来一点帮助吧,水平有限再加上初次动手,写得不好还请多多包含!废话不多说了直接进入正题。

1.首先是项目结构(截图)命名不是很规范——仅自己练手用的

注意:WcfInterface是wcf的服务接口

ProductService是wcf的服务的实现

ProductSVCWebApp服务webapp

productservice.svc通过net.tcp绑定协议对外发布服务

messageservice.svc通过net.msmq绑定协议对外发布服务

主要是通过web.config中的servicemodel配置节点实现,该节点中主要分为3部分完全可通过手工配置:bindings绑定、behaviors行为、services服务配置对外发布的服务终结点endpoint——详情请见以下代码

ServiceModel是相关的数据契约

一、wcfinterface项目是独立的服务契约

1.IMessage服务接口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using ServiceModel; namespace WcfInterface
{
[ServiceContract]
public interface IMessage
{
[OperationContract(IsOneWay=true)]
void Send(Message log);
}
}

IMessage服务接口

2.IServiceTest服务接口

 using ServiceModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks; namespace WcfInterface
{
[ServiceContract]
public interface IServiceTest
{
[OperationContract]
int AddProduct(Product product); [OperationContract]
Product GetProductById(int Id); }
}

IServiceTest服务接口

二、ProductService类库

1.IMessage的服务实现

 using ServiceModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfInterface; namespace ProductService
{
public class MsmqServiceTest:IMessage
{
private static IList<Message> list = new List<Message>(); public void Send(ServiceModel.Message log)
{
list.Add(log);
}
}
}

MsmqServiceTest实现IMessage接口

2.IServiceTest服务

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfInterface;
using ServiceModel; namespace ProductService
{
public class ProductImpl : IServiceTest
{
private static IList<Product> list = new List<Product>();
public int AddProduct(Product product)
{
list.Add(product);
return ;
} public Product GetProductById(int Id)
{
return list.FirstOrDefault(p => p.Id.Equals(Id));
} }
}

IServiceTest服务契约的实现VProductImpl

三、数据契约类库ServiceModel

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace ServiceModel
{
[DataContract]
public class Product
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public decimal Cost { get; set; }
}
}

Product实体

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization; namespace ServiceModel
{
[DataContract]
public class Message
{
[DataMember]
public int Id { get; set; } [DataMember]
public string Header { get; set; } [DataMember]
public string Body { get; set; }
}
}

Message实体

四、ProductSVCWebapp

productservice.svc服务文件

<%@ ServiceHost Language="C#" Debug="true" Service="ProductService.MsmqServiceTest" %>

messageservice.svc服务文件

<%@ ServiceHost Language="C#" Debug="true" Service="ProductService.MsmqServiceTest" %>

web.config中servicemodel配置

 <system.serviceModel>
<services>
<service behaviorConfiguration="endpointbinding" name="ProductService.ProductImpl">
<endpoint address="net.tcp://localhost/ProductService/ProductService.svc" binding="netTcpBinding" bindingConfiguration="tcpbindingtest" name="netTcpBinding_IServiceTest" contract="WcfInterface.IServiceTest"></endpoint>
</service>
<service name="ProductService.MsmqServiceTest">
<endpoint address="net.msmq://localhost/private/ProductService/ProductService.svc" binding="netMsmqBinding" contract="WcfInterface.IMessage" bindingConfiguration="msmqbindingtest" name="netMsmqBinding_IMessage"></endpoint>
</service>
</services>
<bindings>
<netMsmqBinding>
<binding name="msmqbindingtest" exactlyOnce="false">
<security>
<transport msmqProtectionLevel="None" msmqAuthenticationMode="None"/>
<message clientCredentialType="None"/>
</security>
</binding>
</netMsmqBinding>
<netTcpBinding>
<binding name="tcpbindingtest">
<security mode="None">
<transport protectionLevel="None" clientCredentialType="None"></transport>
<message clientCredentialType="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="endpointbinding">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>

web.config

由于我这里用到msmq,建立私有的消息队列名字和address="net.msmq://localhost/private/ProductService/ProductService.svc"里的名字对应起来

五、部署——IIS承载服务及iis中相关配置

服务app以及web站点都需要启用协议net.msmq/net.tcp,另外服务还需要添加msmq和tcp的绑定

我的iis配置ProductService服务建立在defaultweb site下默认的defaultweb site已经添加了net.tcp和net.msmq,http绑定等

testwcf部署web站点

服务运行截图:

本人QQ:443813032 如有不妥之处,还请指正,互相学习,共同进步!!!近期研究go lang中,go将超过java,c成为未来10年最流行的语言,有人说它是20世纪的c语言!希望更多的人加入,一起将go发扬光大!!!

实战WCF中net.tcp和net.msmq绑定协议的更多相关文章

  1. 跟我一起学WCF(10)——WCF中事务处理

    一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...

  2. 基于MSMQ绑定的WCF服务实现总结

    一. 创建消息队列    1 1) 创建一个非事物性的私有队列    1 2)设置消息队列访问权限    2 二.创建WCF服务并绑定消息队列    4 1)创建HelloService服务    4 ...

  3. WCF中事务处理

    一.引言 今天来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事务概念与属性 首先,大家在学习数据库的时候就已经接触到事务这个概念了.所谓事务,它是一个操作序列,这些操作要么都执行,要么都不 ...

  4. WCF初探-26:WCF中的会话

    理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...

  5. WCF中常用的binding方式

    WCF中常用的binding方式: BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务.用于兼容旧的Web ASMX 服务.WSHttpBinding: 比 Basi ...

  6. WCF中的标准绑定

    使用过WCF的童鞋们都很清楚,绑定是必须的.我将这些绑定总结了下. 一.标准绑定简要说明 1.basicHttpBinding 基于WS-I Basic Profile 1.1 的web服务,所需的. ...

  7. C# WCF学习笔记(二)终结点地址与WCF寻址(Endpoint Address and WCF Addressing) WCF中的传输协议

    URI的全称是 Uniform Rosource Identifire(统一资源标识),它唯一标识一个确定的网绐资源,同时也表示资源所处的位置及访问的方式(资源访问所用的网络协议). 对于Endpoi ...

  8. [No0000126]SSL/TLS原理详解与WCF中的WS-Security

    SSL/TLS作为一种互联网安全加密技术 1. SSL/TLS概览 1.1 整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下: SSL:(Secure Socket La ...

  9. WCF中常用的binding方式 z

    WCF中常用的binding方式: BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务.用于兼容旧的Web ASMX 服务. WSHttpBinding: 比 Bas ...

随机推荐

  1. shit element ui & form password validation

    shit element ui & form password validation shit docs https://github.com/yiminghe/async-validator ...

  2. Hiberante可配置参数

    ###################### ### Query Language ### ###################### ## define query language consta ...

  3. 【bzoj1391】[Ceoi2008]order 网络流最小割

    原文地址:http://www.cnblogs.com/GXZlegend/p/6796937.html 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序 ...

  4. C++的一些小操作、常用库及函数(持续更新)

    1. 强制保留n位小数(位数不足则强制补零) 头文件: #include <iomanip> 在输出前: cout<<setprecision(n); 也有不用头文件的方式,在 ...

  5. 你试过不用if写代码吗?

    我在教新手编程时,喜欢给他们一些小小的挑战,比如:不使用if语句(或者三元运算符.switch语句等)解决一些编程问题.这样做有什么意义吗?事实上,它可以迫使你从不同的角度寻找解决方法,也许可以找到更 ...

  6. LinuxUnix time时间戳的处理转换函数

    Linux/Unix time时间戳的处理转换函数 linux下的时间函数 我们在编程中可能会经常用到时间,比如取得系统的时间(获取系统的年.月.日.时.分.秒,星期等),或者是隔一段时间去做某事,那 ...

  7. Kd-tree题表

    bzoj1941: [Sdoi2010]Hide and Seekbzoj2626: JZPFARbzoj4520: [Cqoi2016]K远点对bzoj2989: 数列bzoj2850: 巧克力王国 ...

  8. nfs 和samba

    NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...

  9. 一些奇怪的JavaScript试题

    JavaScript有很多地方和我们熟知的C.Java等的编程习惯不同,这些不同会产生很多让人意想不到的事情.前段时间在知乎有人发了写Javascrtip试题,觉得挺好玩的,这里跟大家分享一下. 01 ...

  10. POJ3159:Candies(差分约束)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 39666   Accepted: 11168 题目链接:h ...