实战WCF中net.tcp和net.msmq绑定协议
平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些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绑定协议的更多相关文章
- 跟我一起学WCF(10)——WCF中事务处理
一.引言 好久没更新,总感觉自己欠了什么一样的,所以今天迫不及待地来更新了,因为后面还有好几个系列准备些,还有很多东西需要学习总结的.今天就来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事 ...
- 基于MSMQ绑定的WCF服务实现总结
一. 创建消息队列 1 1) 创建一个非事物性的私有队列 1 2)设置消息队列访问权限 2 二.创建WCF服务并绑定消息队列 4 1)创建HelloService服务 4 ...
- WCF中事务处理
一.引言 今天来介绍下WCF对事务的支持. 二.WCF事务详解 2.1 事务概念与属性 首先,大家在学习数据库的时候就已经接触到事务这个概念了.所谓事务,它是一个操作序列,这些操作要么都执行,要么都不 ...
- WCF初探-26:WCF中的会话
理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...
- WCF中常用的binding方式
WCF中常用的binding方式: BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务.用于兼容旧的Web ASMX 服务.WSHttpBinding: 比 Basi ...
- WCF中的标准绑定
使用过WCF的童鞋们都很清楚,绑定是必须的.我将这些绑定总结了下. 一.标准绑定简要说明 1.basicHttpBinding 基于WS-I Basic Profile 1.1 的web服务,所需的. ...
- C# WCF学习笔记(二)终结点地址与WCF寻址(Endpoint Address and WCF Addressing) WCF中的传输协议
URI的全称是 Uniform Rosource Identifire(统一资源标识),它唯一标识一个确定的网绐资源,同时也表示资源所处的位置及访问的方式(资源访问所用的网络协议). 对于Endpoi ...
- [No0000126]SSL/TLS原理详解与WCF中的WS-Security
SSL/TLS作为一种互联网安全加密技术 1. SSL/TLS概览 1.1 整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下: SSL:(Secure Socket La ...
- WCF中常用的binding方式 z
WCF中常用的binding方式: BasicHttpBinding: 用于把 WCF 服务当作 ASMX Web 服务.用于兼容旧的Web ASMX 服务. WSHttpBinding: 比 Bas ...
随机推荐
- 基于bootstrap动态分页
bootstrap本身的分页有分页组件 但是却是静态的,无法满足要求,分页必须根据当前的总页数来展示 使用插件bootstrap-paginator github下载地址 https://github ...
- vue2.0中改变了数组值不能实时反映到页面
页面中点击事件checkContent,改变row数组中的row[99]的值,如果注释更改,那么页面是不能实时获取的,如图更改,则可以 具体原理:http://blog.csdn.net/websof ...
- 【版本控制】VisualSVN Server更改SVN版本库存放路径的方法
最近也玩起了SVN软件版本管理,在本机上安装了VisualSVN Server+TortoiseSVN,感觉还不错吧.但是,版本库存在哪里呢?在安装VisualSVN Server时,已经默认设置了, ...
- Topcoder SRM579 1000pts
石头剪刀布QAQ 一看是个很油的概率dp 首先一看你很快能得出状态的表示F[i][r][p][s] 然后只要考虑r,p,s出现的次数来进行概率dp就好了 具体实现的时候细节很多(少) 如果预处理一下组 ...
- HDOJ.2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- poj3133 Manhattan Wiring
Manhattan Wiring Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2016 Accepted: 1162 ...
- Educational Codeforces Round 56 (Rated for Div. 2) ABCD
题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- angular js module 的理解
module其实就是一个容器,里面可以装controller,service,directive,filter等, 官网的解释是:Module :A container for the differe ...
- missing blocks错误
Datanode的日志中看到: 10/12/14 20:10:31 INFO hdfs.DFSClient: Could not obtain block blk_XXXXXXXXXXXXXXXXXX ...
- jw player学习笔记
一.是否支持IE7/8 本地离线播放不支持IE7/8,部署在服务器上时可以. 本地播放报错示意图 二.如何去Logo 1.网页版--HTML5---破解 桌面浏览器看到的效果: jwplayer(&q ...