欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-27 17:50:56
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.添加ServiceMetadataBehavior 3.获取一个serverendpoi ......

因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照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();