Binding

Binding就是提前制定好的协议栈(都会包含transport和encoding这两个元素)

如何选择绑定

 

 

  1.  WS* 开头的绑定用于跨语言(平台)通信
    第一种WebService模式 (BasicHttpBinding,WSHttpBinding)
    第二种Restful模式(WebHttpBinding)
  2. Net开头的绑定用于.net程序间的访问
    第一种直连式访问 (netTcpbinding,NetNamePipedbinding)
    第二种断开式访问 (netMSMQBinding)

绑定性能比较

NetNamePiped > netTcp > MsmqBinding > WS 

遗憾

TCP不支持跨语言通信,没有一个标准来统一,而HTTP有WS标准
googel开源的thrift支持跨语言通信。

NetNamedPipeBinding

netNamepipedBinding 是和Windows内核互通的,endpoint地址不需要写端口号。

 

 

 

 

 

 

 

队列的好处

    1. 削峰
    2. 同步架构异步化,提高系统稳定性。生产者消费者结构,不再强依赖。
    3. 并行串行化

 

/private/testqueue1是队列的路径,如下图在计算机右键-管理-消息队列-右键新建专用队列时

 

 

 注意Operation需要标识位IsOneWay

 

 不然会报错如下:

 

webHttpBinding

1.webHttpBinding+HTTP 实现了Restful通信方式(风格)

2.Restful本质:使用URL定位唯一资源,使用GET,POST,PUT,DELETE等动态描述对资源的CRUD操作
GET:获取,POST:提交,PUT:update,DELETE:delete

3.Restful和Soap和传统模式的比较

  1. 原来:
    http://localhost:80/Student/Get/1
    http://localhost:80/Student/Add/
    http://localhost:80/Student/Modify/
    http://localhost:80/Student/Delete/
    Restful:
    http://localhost:80/Student/1 【get】
    http://localhost:80/Student/ 【post】
    http://localhost:80/Student/ 【put】
    http://localhost:80/Student/ 【delete】
    url中不使用动词,除了Get其他的url保持一致
  2. Soap跟专注用于企业内网,更关注安全,事务等,而内部网不太关心带宽
    Restful 在互联网上传输,比Soap更加节省带宽

4.测试代码

 [ServiceContract]
    public interface IService4
    {
        //WebGet WebInvoke相当于MVC中的Route定义路由
        [OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.Json
            , ResponseFormat = WebMessageFormat.Json
            , BodyStyle = WebMessageBodyStyle.Bare
            , UriTemplate = "test/{id}")]
        Student0 DoGet(string id);

        [OperationContract]
        [WebInvoke(Method = "POST"
            , UriTemplate = "/test/"
            , BodyStyle = WebMessageBodyStyle.Bare
            , RequestFormat = WebMessageFormat.Json
            , ResponseFormat = WebMessageFormat.Json)]
        Student0 DoPost(Student0 student);

        [OperationContract]
        [WebInvoke(Method = "PUT"
            , UriTemplate = "/test/"
            , BodyStyle = WebMessageBodyStyle.Bare
            , RequestFormat = WebMessageFormat.Json
            , ResponseFormat = WebMessageFormat.Json)]
        Student0 DoPut(Student0 student);

        [OperationContract]
        [WebInvoke(Method = "DELETE"
           , UriTemplate = "/test/{id}"
           , BodyStyle = WebMessageBodyStyle.Bare
           , RequestFormat = WebMessageFormat.Json
           , ResponseFormat = WebMessageFormat.Json)]
        Student0 DoDelete(string id);
    }

    public class Student0
    {
        public string Action { get; set; }
    }
WebGet WebInvoke相当于MVC中的Route定义路由

public class Service4 : IService4
    {
        public Student0 DoDelete(string id)
        {
            Console.WriteLine("delete");
            return new Student0() { Action = "delete" };
        }

        public Student0 DoGet(string id)
        {
            Console.WriteLine("get");
            return new Student0() { Action = "get" };
        }

        public Student0 DoPost(Student0 student)
        {
            Console.WriteLine("post");
            return new Student0() { Action = "post" };
        }

        public Student0 DoPut(Student0 student)
        {
            Console.WriteLine("put");
            return new Student0() { Action = "put" };
        }
    }

 

http://localhost:8080/HomeService/Student/1

 

 

 var info = WebPostUtils.DoPost("http://localhost:8080/HomeService/test/", "{\"Action\":\"info\"}", "PUT");

var info = WebPostUtils.DoPost("http://localhost:8080/HomeService/test/", "{\"Action\":\"info\"}", "POST");

var info = WebPostUtils.DoPost("http://localhost:8080/HomeService/test/1", "", "DELETE");

 

public class WebPostUtils
    {
        public static string DoPost(string url, string param, string method)
        {
            string msg = string.Empty;

            byte[] postData = Encoding.UTF8.GetBytes(param);

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = method;

            myRequest.ContentLength = postData.Length;
            myRequest.ContentType = "application/json;charset=UTF-8";
            myRequest.Timeout = 1000 * 120;
            myRequest.Accept = "*/*";
            myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
            myRequest.Headers.Add(HttpRequestHeader.KeepAlive, "TRUE");

            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(postData.ToArray(), 0, postData.Length);

            newStream.Flush();
            newStream.Close();

            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
            {
                if (myResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    msg = reader.ReadToEnd();
                }
            }

            return msg;
        }
    }

  

 

posted @ 2020-02-03 21:22  vvf  阅读(501)  评论(0)    收藏  举报