平时很少写博文的,以前都是转载其他园友的文章,这几天有时间就自己尝试写一些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. java线程(4)——线程同步的锁技术

    同步 同步,字面来看,有点一起工作的意思.但在线程同步中,"同"意为协同.互相配合. 比如: A.B两个线程,并不是说两个线程必须同时一起工作,而是说互相配合工作,在某个时间可能线 ...

  2. 基于网络的 Red Hat 无人值守安装

    基于网络的 Red Hat 无人值守安装 本文介绍了 PC 平台上的一种快速 Red Hat Linux 安装方案.它具有很高的自动化程度--用户只需手工启动机器并选择从网络启动,就可以完成整个安装过 ...

  3. hdu 1267 下沙的沙子有几粒? (递推)

    下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. 使用def文件简化dll导出

    原文链接地址:http://www.cnblogs.com/TianFang/archive/2013/05/04/3059073.html 在C++中,我们可以通过 __declspec(dllex ...

  5. 工具——SVN常用命令

    SVN一般都是团队合作做一个项目所需用到的,为了是版本的统一 ;1. Check out——从服务器端取得代码    把服务器资料库里存放的某个项目代码取出来,放到本地主机中,这个动作叫做“check ...

  6. Vim使用小记(二)插件管理

    By francis_hao    Mar 8,2017 Vundle Vundle,全称为Vim bundle,是一个插件管理器.可以对vim插件进行安装和卸载. Vundle的安装方法看这里[参考 ...

  7. 使用adobe pdf去除PDF文档中的批量雷同文本

    一.问题的提出 MgoSoft tiff to pdf软件没有提供中国地区的非VISA用户的购买渠道,中国通常都是银联标识走天下,卡不是VISA买不了这样的软件, 那么, MgoSoft tiff t ...

  8. java禁止实例化的工具类

    public class Q { /** * @param args */ public static void main(String[] args) { new Person() } } clas ...

  9. css的padding,border,margin的意思

    下面的内边距(padding).边框(border) .外边距(margin)的意思是我自己的理解. 代码如下: <!DOCTYPE html> <html lang="e ...

  10. python 一些乱七八糟的东西

    import random import os import sys import re class _is: def __init__(self,reg): self.cr=re.compile(r ...