跟着Artech学习WCF(4) MSMQ 绑定

简介: 曾几何时 再看大家讨论各种构架时,经常出现在各个服务器间传递数据,经常出现MSMQ, 那是看到这个心理就郁闷,怎么整天折腾ASP.NET 就没遇见过这个东西 原来这个家伙藏在WCF里面 嘿嘿这次被我发现了 首先 第一次装IIS的话 默认是没有安装msmq 所以需要自己安装的   看Art...

曾几何时 再看大家讨论各种构架时,经常出现在各个服务器间传递数据,经常出现MSMQ,

那是看到这个心理就郁闷,怎么整天折腾ASP.NET 就没遇见过这个东西

原来这个家伙藏在WCF里面

嘿嘿这次被我发现了

首先 第一次装IIS的话 默认是没有安装msmq 所以需要自己安装的

 

看Artech 的介绍 这个东西在不能时刻保持和服务器连接的情况下 使用非常有用

对于以WEB为构架为核心的 系统我虽然不知道这个东西有什么用

但我觉得在移动应用中这个东西应该比较有用

移动客户端和服务器端总是若离若弃

但不知道 移动客户端支不支持 这个东西 例如 WINCE ,wp7 支不支持

项目结构图如下

新浪微博截图_未命名

代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Transactions;
using cfContract;

namespace cfClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>("defaultEndpoint");
            IOrderProcessor channel = channelFactory.CreateChannel();

            Order order = new Order(Guid.NewGuid(), DateTime.Today, Guid.NewGuid(), "A Company");
            order.OrderItems.Add(new OrderItem(Guid.NewGuid(), "PC", 5000, 20));
            order.OrderItems.Add(new OrderItem(Guid.NewGuid(), "Printer", 7000, 2));

            Console.WriteLine("Submit order to server");
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                channel.Submit(order);
                scope.Complete();
            }
            Console.Read();
        }
    }
}

 

 

==================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace cfContract
{
    [ServiceContract]
    [ServiceKnownType(typeof(Order))]
   public interface IOrderProcessor
    {

        [OperationContract(IsOneWay = true)]
        void Submit(Order order);

    }
}

=================================

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace cfContract
{
    [DataContract]
    [KnownType(typeof(OrderItem))]
   public class Order
    {
        #region Private Fields
        private Guid _orderNo;
        private DateTime _orderDate;
        private Guid _supplierID;
        private string _supplierName;
        private IList<OrderItem> _orderItems;
        #endregion

        #region Constructors
        public Order()
        {
            this._orderItems = new List<OrderItem>();
        }

        public Order(Guid orderNo, DateTime orderDate, Guid supplierID, string supplierName)
        {
            this._orderNo = orderNo;
            this._orderDate = orderDate;
            this._supplierID = supplierID;
            this._supplierName = supplierName;

            this._orderItems = new List<OrderItem>();
        }

        #endregion

        #region Public Properties
        [DataMember]
        public Guid OrderNo
        {
            get { return _orderNo; }
            set { _orderNo = value; }
        }

        [DataMember]
        public DateTime OrderDate
        {
            get { return _orderDate; }
            set { _orderDate = value; }
        }

        [DataMember]
        public Guid SupplierID
        {
            get { return _supplierID; }
            set { _supplierID = value; }
        }

        [DataMember]
        public string SupplierName
        {
            get { return _supplierName; }
            set { _supplierName = value; }
        }

        [DataMember]
        public IList<OrderItem> OrderItems
        {
            get { return _orderItems; }
            set { _orderItems = value; }
        }

        #endregion


        #region Public Methods
        public override string ToString()
        {
            string description = string.Format("General Informaion:\n\tOrder No.\t: {0}\n\tOrder Date\t: {1}\n\tSupplier No.\t: {2}\n\tSupplier Name\t: {3}", this._orderNo, this._orderDate.ToString("yyyy/MM/dd"), this._supplierID, this._supplierName);
            StringBuilder productList = new StringBuilder();
            productList.AppendLine("\nProducts:");

            int index = 0;
            foreach (OrderItem item in this._orderItems)
            {
                productList.AppendLine(string.Format("\n{4}. \tNo.\t\t: {0}\n\tName\t\t: {1}\n\tPrice\t\t: {2}\n\tQuantity\t: {3}", item.ProductID, item.ProductName, item.UnitPrice, item.Quantity, ++index));
            }

            return description + productList.ToString();
        }
        #endregion










    }
}

==================================

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Runtime.Serialization;

namespace cfContract
{
   public class OrderItem
    {

        #region Private Fields
        private Guid _productID;
        private string _productName;
        private decimal _unitPrice;
        private int _quantity;
        #endregion

        #region Constructors
        public OrderItem()
        { }

        public OrderItem(Guid productID, string productName, decimal unitPrice, int quantity)
        {
            this._productID = productID;
            this._productName = productName;
            this._unitPrice = unitPrice;
            this._quantity = quantity;
        }
        #endregion

       [DataMember]
        public Guid ProductID
        {
            get { return _productID; }
            set { _productID = value; }
        }

        [DataMember]
        public string ProductName
        {
            get { return _productName; }
            set { _productName = value; }
        }

        [DataMember]
        public decimal UnitPrice
        {
            get { return _unitPrice; }
            set { _unitPrice = value; }
        }

        [DataMember]
        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }
  


    }
}

 

===========================================

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Messaging;
using cfService;

namespace cfHosting
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @".\private$\orders";
            //此计算机上尚未安装消息队列


            if (!MessageQueue.Exists(path))
            {
                MessageQueue.Create(path, true);
            }

            using (ServiceHost host = new ServiceHost(typeof(OrderProcessorService)))
            {
                host.Opened += delegate
                {
                    Console.WriteLine("Service has begun to listen\n\n");
                };

                host.Open();

                Console.Read();



            }
        }
    }
}

 

 

==========================================

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using cfContract;
namespace cfService
{
  public  class OrderProcessorService:IOrderProcessor
    {
      [OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
        void IOrderProcessor.Submit(Order order)
        {

            Orders.Add(order);
            Console.WriteLine("Receive an order.");
            Console.WriteLine(order.ToString());

        }
    }
}

 

 

===========================================================

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cfContract;

namespace cfService
{
 public static   class Orders
    {
        private static IDictionary<Guid, Order> _orderList = new Dictionary<Guid, Order>();

        public static void Add(Order order)
        {
            _orderList.Add(order.OrderNo, order);
        }

        public static Order GetOrder(Guid orderNo)
        {
            return _orderList[orderNo];
        }
    }
}

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

服务器端配置

<system.serviceModel>
  <bindings>
    <netMsmqBinding>
      <binding name="msmqBinding">
        <security>
          <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
          <message clientCredentialType="None" />
        </security>
      </binding>
    </netMsmqBinding>
  </bindings>
  <services>
    <service name="cfService.OrderProcessorService">
      <endpoint address="" binding="netMsmqBinding"
          bindingConfiguration="msmqBinding" contract="cfContract.IOrderProcessor" />
      <host>
        <baseAddresses>
          <add baseAddress="net.msmq://localhost/private/orders"/>
        </baseAddresses>
      </host>
    </service>
  </services>
</system.serviceModel>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

客户端配置

<system.serviceModel>
  <bindings>
    <netMsmqBinding>
      <binding name="msmqBinding">
        <security>
          <transport msmqAuthenticationMode="None" msmqProtectionLevel="None" />
          <message clientCredentialType="None" />
        </security>
      </binding>
    </netMsmqBinding>
  </bindings>
  <client>
    <endpoint address="net.msmq://localhost/private/orders" binding="netMsmqBinding"
        bindingConfiguration="msmqBinding" contract="cfContract.IOrderProcessor"
        name="defaultEndpoint" />
  </client>
</system.serviceModel>

test
相关文章
|
9月前
|
网络协议 网络架构 Windows
框架学习——WCF框架
框架学习——WCF框架
230 0
|
网络协议 安全 Windows
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
|
安全 网络协议 数据格式
WCF系统内置绑定列表与系统绑定所支持的功能
WCF系统内置绑定列表 绑定 配置元素 说明 传输协议 编码格式 BasicHttpBinding 一个绑定,适用于与符合 WS-Basic Profile的Web服务(例如基于 ASP.NET Web 服务(ASMX)的服务)进行的通信。
760 0
|
网络协议
WCF绑定的选择
版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/8593924 格式与编码 每种标准绑定使用的传输协议与编码格式都不相同,如表1-1 所示。
599 0
|
数据库 网络架构
我的服装DRP之即时通讯——为WCF增加UDP绑定(应用篇)
发个牢骚,博客园发博文竟然不能写副标题。这篇既为我的服装DRP系列第二篇,也给为WCF增加UDP绑定系列收个尾。原本我打算记录开发过程中遇到的一些问题和个人见解,不过写到一半发现要写的东西实在太多,有些问题甚至不好描述,又担心误导读者,就作罢了。
623 0
为WCF增加UDP绑定(实践篇)
这两天忙着系统其它功能的开发,没顾上写日志。本篇所述皆围绕为WCF增加UDP绑定(储备篇)中讲到的微软示例,该示例我已上传到网盘。 上篇说道,绑定是由若干绑定元素有序组成,为WCF增加UDP绑定其实就是为绑定增加UDP传输绑定元素,最终目的是在信道栈中生成UDP传输信道。
886 0
|
网络协议 网络架构 安全
为WCF增加UDP绑定(储备篇)
日前我开发的服装DRP需要用到即时通信方面的技术,比如当下级店铺开出零售单时上级机构能实时收到XX店铺XX时XX分卖出XX款衣服X件之类的信息,当然在上级发货时,店铺里也能收到已经发货的提醒。即时通信技术能运用到DRP系统的很多方面,若深入下去,甚至可以开发一个系统内部的通讯模块,类似于QQ。
980 0
|
网络协议
WCF绑定类型选择
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/6070364     WCF绑定类型选择     发布日期:2010年12月10日星期五 作者:EricHu     在开发WCF程序时,如何选择一个适合的绑定对于消息传输的可靠性,传输模式是否跨进程、主机、网络,传输模式的支持、安全性、性能等方面有着重要的影响。
790 0
WCF绑定细节(2)——绑定,绑定元素
绑定这块引出了很多细节。绑定解决了消息交换中的传输协议,传输,编码等问题。如果要公开WCF服务,就要公开终结点Endpoint,WCF服务信息交换就是Endpoint之间的信息交换。终结点三大元素:ABC。
859 0
|
安全 网络架构
消息(7)——WCF编程模型中控制消息(1)绑定,契约
WCF服务要通过终结点来进行通信,终结点三大构成元素:ABC,其中的B,binding是重中之重,它解决了在消息交换过程中的编码,传输协议,安全等问题。 绑定是分层的,一个绑定对象对应一组有序的绑定元素的集合。
745 0