• (纯代码)快速创建wcf rest 服务


    因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了:

    1.创建WebHttpBinding

    2.添加ServiceMetadataBehavior

    3.获取一个serverendpoint

    4.指定WebHttpBehavior格式

     1         /// <summary>
     2         /// 快速创建一个WCF http服务
     3         /// </summary>
     4         /// <param name="contractType"></param>
     5         /// <param name="serviceType"></param>
     6         /// <param name="url"></param>
     7         /// <param name="timeout"></param>
     8         /// <param name="bufferSize"></param>
     9         /// <param name="isDebug"></param>
    10         /// <returns></returns>
    11         public static ServiceHost CreateWebService(Type contractType, Type serviceType, string url, TimeSpan timeout, long bufferSize = 2147483647, bool isDebug = true)
    12         {
    13             Uri baseAddress = new Uri(url);
    14 
    15             var serviceHost = new ServiceHost(serviceType, baseAddress);
    16 
    17 
    18             //1.创建WebHttpBinding
    19             var binding = new WebHttpBinding(WebHttpSecurityMode.None);
    20 
    21             binding.Security = new WebHttpSecurity() { Mode = WebHttpSecurityMode.None, Transport = null };
    22 
    23             binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = bufferSize;
    24 
    25             binding.OpenTimeout = binding.CloseTimeout = binding.SendTimeout = binding.ReceiveTimeout = timeout;
    26 
    27             binding.UseDefaultWebProxy = false;
    28 
    29             binding.ProxyAddress = null;
    30 
    31             var readerQuotas = new System.Xml.XmlDictionaryReaderQuotas();
    32 
    33             readerQuotas.MaxArrayLength = readerQuotas.MaxBytesPerRead = readerQuotas.MaxDepth = 
    34                 readerQuotas.MaxNameTableCharCount = readerQuotas.MaxStringContentLength = (int)bufferSize;
    35 
    36             binding.ReaderQuotas = readerQuotas;
    37 
    38             //2.添加ServiceMetadataBehavior
    39             if (serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
    40             {
    41                 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    42 
    43                 behavior.HttpGetEnabled = true;
    44 
    45                 //behavior.HttpsGetEnabled = false;
    46 
    47                 serviceHost.Description.Behaviors.Add(behavior);
    48             }
    49 
    50             if (serviceHost.Description.Behaviors.Find<ServiceThrottlingBehavior>() == null)
    51             {
    52                 ServiceThrottlingBehavior behavior = new ServiceThrottlingBehavior();
    53 
    54                 behavior.MaxConcurrentCalls = behavior.MaxConcurrentInstances = behavior.MaxConcurrentSessions = (int)bufferSize;
    55 
    56                 serviceHost.Description.Behaviors.Add(behavior);
    57             }
    58             //3.获取一个serverendpoint
    59             var serviceEndpoint = serviceHost.AddServiceEndpoint(contractType, binding, baseAddress);
    60 
    61             //4.指定WebHttpBehavior格式62 
    63             //需引用System.ServiceModel.Web.dll
    64             WebHttpBehavior webHttpBehavior = new WebHttpBehavior()
    65             {
    66                 AutomaticFormatSelectionEnabled = false,
    67                 DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Bare,
    68                 DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,
    69                 DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json,
    70                 FaultExceptionEnabled = isDebug,
    71                 HelpEnabled = isDebug
    72             };
    73 
    74             serviceEndpoint.Behaviors.Add(webHttpBehavior);
    75 
    76             serviceHost.Opened += delegate
    77             {
    78                 Console.WriteLine("{0}已启动!", serviceType.Name);
    79             };
    80 
    81             return serviceHost;
    82         }

    发布一个wcf  rest 服务只需要

    1  WCFServiceFactory.CreateWebService(typeof(ITestContract),typeof(TestService),"http://127.0.0.1:39654/",new TimeSpan(1, 0, 0),true).Open();
  • 相关阅读:
    软件项目开发典型风险一览
    删除数据库所有表数据
    今天愚人节,教大家一个真正的最强整人方法
    潘正磊谈微软研发团队管理之道(下)
    追MM与23种设计模式
    22个所见即所得在线 Web 编辑器
    神奇的js代码,图片全都飞起来了
    字体 小 中 大
    使用ODP.NET连接Oracle数据库一个OracleCommand运行多条SQL语句的方法
    删除SQL数据库中所有的表
  • 原文地址:https://www.cnblogs.com/yswenli/p/8405165.html
Copyright © 2020-2023  润新知