(Code-only) quickly create a wcf rest service, wcfrest
Because there is a small tool that needs to be connected to other services, you can try to see if you can get a method that requires no configuration to quickly interconnect with each other) after (Finally, debugging is made against the wcf configuration object:
1. Create WebHttpBinding
2. Add ServiceMetadataBehavior
3. Get a serverendpoint
4. Specify the WebHttpBehavior format
1 /// <summary> 2 /// quickly create a WCF http service 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. create WebHttpBinding19 var binding = new WebHttpBinding (WebHttpSecurityMode. none); 20 21 binding. security = new WebHttpSecurity () {Mode = WebHttpSecurityMode. none, Transport = null}; 22 23 binding. maxBufferPoolSize = Binding. maxcompute edmessagesize = bufferSize; 24 25 binding. openTimeout = binding. closeTimeout = binding. sendTimeout = binding. receiveTimeout = timeout; 26 27 binding. usedefawebwebproxy = false; 28 29 binding. proxyAddress = null; 30 31 var readerQuotas = new System. xml. xmlDictionaryReaderQuotas (); 32 33 readerQuotas. maxArrayLength = readerQuotas. maxBytesPerRead = readerQuotas. maxDepth = 34 readerQuot As. maxNameTableCharCount = readerQuotas. maxStringContentLength = (int) bufferSize; 35 36 binding. readerQuotas = readerQuotas; 37 38 // 2. add ServiceMetadataBehavior39 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 servi CeHost. description. behaviors. add (behavior); 48} 49 50 if (serviceHost. description. behaviors. find <ServiceThrottlingBehavior> () = null) 51 {52 ServiceThrottlingBehavior behavior = new ServiceThrottlingBehavior (); 53 54 behavior. maxconcurrentcils = behavior. maxConcurrentInstances = behavior. maxConcurrentSessions = (int) bufferSize; 55 56 serviceHost. description. behaviors. add (behavior); 57} 58 // 3. get a ser Verendpoint59 var serviceEndpoint = serviceHost. addServiceEndpoint (contractType, binding, baseAddress); 60 61 // 4. specify the WebHttpBehavior format 62 63 // System must be referenced. serviceModel. web. dll64 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 = isDebug72}; 73 74 serviceEndpoint. behaviors. add (webHttpBehavior); 75 76 serviceHost. opened + = delegate77 {78 Console. writeLine ("{0} started! ", ServiceType. Name); 79}; 80 81 return serviceHost; 82}
To publish a wcf rest service, you only need
1 WCFServiceFactory.CreateWebService(typeof(ITestContract),typeof(TestService),"http://127.0.0.1:39654/",new TimeSpan(1, 0, 0),true).Open();