(純代碼)快速建立wcf rest 服務

由於有一個小工具須要和其它的業務對接數據,因此就試一下看能不能弄一個無需配置快速對接的方法出來,百(以)度(訛)過(傳)後(訛),最後仍是對照wcf配置對象調試出來了:web

1.建立WebHttpBinding工具

2.添加ServiceMetadataBehaviorurl

3.獲取一個serverendpointspa

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 服務只須要rest

1  WCFServiceFactory.CreateWebService(typeof(ITestContract),typeof(TestService),"http://127.0.0.1:39654/",new TimeSpan(1, 0, 0),true).Open();
相關文章
相關標籤/搜索