• WCF、MongoDB


    http://www.cnblogs.com/quietwalk/archive/2011/08/09/2132573.html

    http://www.cnblogs.com/huangxincheng/p/4609168.html

    http://www.cnblogs.com/VinC/archive/2011/02/25/Use-GSon-Hand-JsonData-For-Android-Device.html

    如何在调用WCF服务之前弹出一个确认对话框?

    数据契约:存在于SOAP的BODY部分

    应用场景:传输类实体

    消息契约:提供完整的SOAP

    构建(SOAP)头、体(应用场景:上传文件)

    WCF元数据公布的2种方式:httpGetEnabled与mex

    WCF元数据发布的2种方式:httpGetEnabled与mex
    一、元数据即WSDL,描述了服务的细节,以便客户端使用。


    二、必须为服务配置ServiceMetadata行为,才能为其生成WSDL,才能再使用httpGetEnabled或mex将其公布出去


    三、这两种方式公布出去的WSDL无区别。但公布的方式有区别
    1、httpGetEnabled=true,类似的还有httpsGetEnabled=true
    此方式通过在服务在的URL后加“?wsdl”的方式公布WSDL,可直接通过HTTP访问得到。

    2、mex
    此方式以一般的终结点方式公布,支持各种协议:http、tcp、NamedPipe

    告别烦恼的config配置

    ---------------------------------------------------------------------------------------

    <configuration>
      <appSettings>
        <add key ="baseurl" value="http://localhost:19200/HomeService"/>
        <add key ="endpoindurl" value="net.tcp://localhost:1920/HomeService"/>
      </appSettings>

    服务端

    class Program1
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(HomeService), new Uri(ConfigurationManager.AppSettings["baseurl"]));
    
                host.AddServiceEndpoint(typeof(IHomeService), new NetTcpBinding(), ConfigurationManager.AppSettings["endpoindurl"]);
    
                //公布元数据
                host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    
                host.Open();
    
                Console.WriteLine("服务已经开启。。。");
    
                Console.Read();
            }
        }

    客户端 

    static void Main(string[] args)
            {
                ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new NetTcpBinding(), "net.tcp://localhost:1920/homeservice");
    
                var channel = factory.CreateChannel();
    
                var result = channel.GetLength("12345");
            }

    ---------------------------------------------------------------------------------------- 

    WebGet和WebInvoke正是用了UriTemplate,才具有了路由转向的功能,还有就是默认返回的是xml,这里就用json值作为服务返回的格式

    [ServiceContract]
        public interface IHomeService
        {
            [OperationContract]
            [WebGet(UriTemplate = "Get/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            Student Get(string id);
    
            [OperationContract]
            [WebInvoke(Method = "POST", UriTemplate = "Add", RequestFormat = WebMessageFormat.Json,
                       ResponseFormat = WebMessageFormat.Json)]
            string Add(Student stu);
        }
    View Code
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel" switchValue="ActivityTracing">
            <listeners>
              <add name="mylisteners" type="System.Diagnostics.XmlWriterTraceListener" initializeData="E:1.txt" />
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging" switchValue="ActivityTracing">
            <listeners>
              <add name="messagelogging" type="System.Diagnostics.XmlWriterTraceListener" initializeData="E:2.txt"/>
            </listeners>
          </source>
        </sources>
        <trace autoflush="true"/>
      </system.diagnostics>
    
      <system.serviceModel>
    
        <diagnostics>
          <messageLogging logEntireMessage="true" logMalformedMessages="true"  logMessagesAtTransportLevel="true" />
        </diagnostics>
    
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webbehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
    
        <services>
          <service name="MyService.HomeService">
            <endpoint address="HomeService" binding="webHttpBinding" behaviorConfiguration="webbehavior"
              contract="MyService.IHomeService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://127.0.0.1:1920" />
              </baseAddresses>
            </host>
          </service>
        </services>
    
      </system.serviceModel>
    
    </configuration>
    View Code

    ------------------------------------------------------------------------------------------

    自定义FaultException 

    public class HomeService : IHomeService
        {
            public Student Get(string id)
            {
                try
                {
                    //这里必然会抛出异常。。。
                    var result = Convert.ToInt32(id) / Convert.ToInt32("0");
    
                    return new Student() { ID = Convert.ToInt32(id), Name = "hxc", SNS = "001" };
                }
                catch (Exception ex)
                {
                    var reason = new FaultReason("你这个战斗力只有五的渣渣。。。 这么简单的错误都出来了,搞个鸡巴毛");
    
                    var code = new FaultCode("500");
    
                    var faultException = new FaultException(reason, code, "是Get这个王八蛋");
    
                    throw faultException;
                }
            }
        }

     -------------------------------------------------------------------------------------------

    数据传输量,传输量不能大于64k,否则请求就会在client端拒绝 

     <bindings>
          <netTcpBinding>
            <binding name="MySessionBinding" maxReceivedMessageSize="2147483647"/>
          </netTcpBinding>
        </bindings>

     使用MaxBufferSize 和 MaxBufferPoolSize,就是用来增加缓冲区和缓冲池的大小。

    当并发数达到800左右的时候,servcie端就开始拒绝client端过来的请求了,并且之后的1min的时间里,client端开始出现超时异常,在wcf里面有一个叫做ServiceThrottlingElement绑定元素,它就是用来控制服务端的并发数

    <system.serviceModel>
        <behaviors >
          <serviceBehaviors >
            <behavior name="nettcpBehavior">
              <serviceMetadata httpGetEnabled="false" />
              <!--是否在错误中包含有关异常的详细信息-->
              <serviceDebug includeExceptionDetailInFaults="True" />
              <serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <bindings>
          <netTcpBinding>
            <binding name="MySessionBinding" />
          </netTcpBinding>
        </bindings>
    
        <services>
          <service behaviorConfiguration="nettcpBehavior" name="MyService.HomeService">
            <endpoint address="net.tcp://127.0.0.1:19200/HomeService" binding="netTcpBinding"
              bindingConfiguration="MySessionBinding" contract="MyService.IHomeService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://127.0.0.1:1920" />
              </baseAddresses>
            </host>
          </service>
        </services>
    
      </system.serviceModel>

    -------------------------------------------------------------------------------------------

     其实Binding就是一个预先默认配置好的信道栈,每一种Binding都有属于自己的BindingElements,

    恰恰这些Elements是可以跨Binding的,也就是说可以自由组合Elements,这样可以最大的灵活性,例如:

    BasicHttpBinding有两个绑定元素,其中对soap消息进行的是TextMessageEncoding编码对吧,而netTcpBinding对soap进行的BinaryMessageEncoding。

    自定义绑定:

    class Program1
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920"));
    
                var customBinding = new CustomBinding();
    
                customBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
                customBinding.Elements.Add(new HttpTransportBindingElement());
    
                host.AddServiceEndpoint(typeof(IHomeService), customBinding, "HomeServie");
    
                host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
    
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    
                host.Open();
    
                Console.WriteLine("服务已经开启!!!");
    
                Console.Read();
            }
        }

    ------------------------------------------------------------------------------------------

    高级玩法之自定义Behavior

    你必须要了解的3种通信模式

    你需要了解的三个小技巧   服务是端点的集合 Host寄宿多个Service  Tcp中的端口共享

    通信单元Message

    client如何知道server提供的功能清单 wsdl

    ------------------------------------------------------------------------------------------

    MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。

    在高负载的情况下,添加更多的节点,可以保证服务器性能。

    MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。

    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

     

    http://blog.csdn.net/u011630900/article/details/52926363

  • 相关阅读:
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
  • 原文地址:https://www.cnblogs.com/ecollab/p/6169559.html
Copyright © 2020-2023  润新知