markus / ConvertService / ServiceBase / Markus.Service.WcfService / Helper / WcfHostHelper.cs @ 8ff1bf3a
이력 | 보기 | 이력해설 | 다운로드 (7.42 KB)
1 | 53c9637d | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.ServiceModel; |
||
7 | using System.ServiceModel.Description; |
||
8 | using System.Web.Services; |
||
9 | using System.ServiceModel.Channels; |
||
10 | using System.Xml; |
||
11 | |||
12 | namespace Markus.Service.IWcfService |
||
13 | { |
||
14 | public static class WebServiceHelper |
||
15 | { |
||
16 | public static ServiceHost WcfCreate(this ServiceHost serviceHost, object service, Type serviceInterface, Uri HttpHostEndpoint) |
||
17 | { |
||
18 | if (serviceHost != null) |
||
19 | { |
||
20 | foreach (var item in serviceHost.ChannelDispatchers) |
||
21 | { |
||
22 | item.Abort(); |
||
23 | } |
||
24 | |||
25 | serviceHost.Abort(); |
||
26 | serviceHost = null; |
||
27 | } |
||
28 | |||
29 | serviceHost = new ServiceHost(service,new []{ HttpHostEndpoint}); |
||
30 | |||
31 | ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); |
||
32 | mBehave.HttpGetEnabled = true; |
||
33 | mBehave.HttpsGetEnabled = true; |
||
34 | serviceHost.Description.Behaviors.Add(mBehave); |
||
35 | |||
36 | if (HttpHostEndpoint != null) |
||
37 | { |
||
38 | Binding httpbinding = new BasicHttpBinding(); |
||
39 | Binding bindingMex = MetadataExchangeBindings.CreateMexHttpBinding(); |
||
40 | |||
41 | httpbinding.CreateBindingElements().Add(gBindingElement); |
||
42 | |||
43 | var httpEndpoint = serviceHost.AddServiceEndpoint(serviceInterface, httpbinding, ""); |
||
44 | serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), bindingMex, "mex"); |
||
45 | |||
46 | Binding restBinding = new WebHttpBinding();// WebHttpSecurityMode.None); |
||
47 | restBinding.CloseTimeout = new TimeSpan(0, 10,0); |
||
48 | restBinding.ReceiveTimeout = new TimeSpan(0, 10,0); |
||
49 | restBinding.SendTimeout = new TimeSpan(0, 10,0); |
||
50 | |||
51 | restBinding.CreateBindingElements().Add(gBindingElement); |
||
52 | |||
53 | var restEndpoint = serviceHost.AddServiceEndpoint(serviceInterface, restBinding, "Rest"); |
||
54 | |||
55 | |||
56 | restEndpoint.EndpointBehaviors.Add(new WebHttpBehavior { |
||
57 | HelpEnabled = true, |
||
58 | DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, |
||
59 | AutomaticFormatSelectionEnabled =false, |
||
60 | DefaultOutgoingResponseFormat= System.ServiceModel.Web.WebMessageFormat.Json |
||
61 | }); |
||
62 | |||
63 | } |
||
64 | |||
65 | var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>(); |
||
66 | behavior.InstanceContextMode = InstanceContextMode.Single; |
||
67 | |||
68 | serviceHost.Open(); |
||
69 | return serviceHost; |
||
70 | } |
||
71 | |||
72 | |||
73 | public static BinaryMessageEncodingBindingElement gBindingElement = new BinaryMessageEncodingBindingElement |
||
74 | { |
||
75 | MaxReadPoolSize = Int32.MaxValue, |
||
76 | MaxWritePoolSize = Int32.MaxValue, |
||
77 | MaxSessionSize = Int32.MaxValue, |
||
78 | ReaderQuotas = GetReaderQuotas() |
||
79 | }; |
||
80 | |||
81 | public static XmlDictionaryReaderQuotas GetReaderQuotas() |
||
82 | { |
||
83 | return new XmlDictionaryReaderQuotas |
||
84 | { |
||
85 | MaxDepth = Int16.MaxValue, |
||
86 | MaxStringContentLength = Int32.MaxValue, |
||
87 | MaxArrayLength = Int32.MaxValue, |
||
88 | MaxBytesPerRead = Int32.MaxValue, |
||
89 | MaxNameTableCharCount = Int32.MaxValue |
||
90 | }; |
||
91 | } |
||
92 | |||
93 | //rest 추가로 변경 |
||
94 | private static ServiceHost WcfCreate_old(this ServiceHost serviceHost, object service, Type serviceInterface, IEnumerable<Uri> HostEndpoints) |
||
95 | { |
||
96 | if (serviceHost != null) |
||
97 | { |
||
98 | foreach (var item in serviceHost.ChannelDispatchers) |
||
99 | { |
||
100 | item.Abort(); |
||
101 | } |
||
102 | |||
103 | serviceHost.Abort(); |
||
104 | serviceHost = null; |
||
105 | } |
||
106 | |||
107 | serviceHost = new ServiceHost(service, HostEndpoints.ToArray()); |
||
108 | |||
109 | if (HostEndpoints.GroupBy(f => f.Scheme).Count() != HostEndpoints.Count()) |
||
110 | { |
||
111 | throw new Exception("Host Uri's schema must be unique."); |
||
112 | } |
||
113 | |||
114 | |||
115 | ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); |
||
116 | mBehave.HttpGetEnabled = true; |
||
117 | mBehave.HttpsGetEnabled = true; |
||
118 | serviceHost.Description.Behaviors.Add(mBehave); |
||
119 | |||
120 | //ServiceBehaviorAttribute attribute = new ServiceBehaviorAttribute(); |
||
121 | //attribute.InstanceContextMode = InstanceContextMode.Single; |
||
122 | //attribute.IncludeExceptionDetailInFaults = false; |
||
123 | |||
124 | //serviceHost.Description.Behaviors.Add(attribute); |
||
125 | |||
126 | //ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>(); |
||
127 | //sdb.IncludeExceptionDetailInFaults = true; |
||
128 | |||
129 | foreach (var item in HostEndpoints) |
||
130 | { |
||
131 | System.ServiceModel.Channels.Binding binding = null; |
||
132 | System.ServiceModel.Channels.Binding bindingMex = null; |
||
133 | |||
134 | if (item.Scheme == Uri.UriSchemeHttp) |
||
135 | { |
||
136 | binding = new BasicHttpBinding(); |
||
137 | bindingMex = MetadataExchangeBindings.CreateMexHttpBinding(); |
||
138 | |||
139 | BindingElementCollection bindElemColl = new BindingElementCollection(); |
||
140 | BinaryMessageEncodingBindingElement bindElem = new BinaryMessageEncodingBindingElement(); |
||
141 | bindElem.MaxReadPoolSize = 64; |
||
142 | bindElem.MaxWritePoolSize = 16; |
||
143 | bindElem.MaxSessionSize = 2048; |
||
144 | XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas(); |
||
145 | readerQuotas.MaxDepth = 32; |
||
146 | readerQuotas.MaxStringContentLength = Int32.MaxValue; |
||
147 | readerQuotas.MaxArrayLength = Int32.MaxValue; |
||
148 | readerQuotas.MaxBytesPerRead = Int32.MaxValue; |
||
149 | readerQuotas.MaxNameTableCharCount = Int32.MaxValue; |
||
150 | bindElem.ReaderQuotas = readerQuotas; |
||
151 | |||
152 | binding.CreateBindingElements().Add(bindElem); |
||
153 | } |
||
154 | else if (item.Scheme == Uri.UriSchemeHttps) |
||
155 | { |
||
156 | binding = new BasicHttpsBinding(); |
||
157 | bindingMex = MetadataExchangeBindings.CreateMexHttpsBinding(); |
||
158 | |||
159 | |||
160 | |||
161 | } |
||
162 | else if (item.Scheme == Uri.UriSchemeNetTcp) |
||
163 | { |
||
164 | binding = new NetTcpBinding(); |
||
165 | bindingMex = MetadataExchangeBindings.CreateMexTcpBinding(); |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | throw new Exception($"{item.ToString()} is Not Support Schema."); |
||
170 | } |
||
171 | |||
172 | var endpoint = serviceHost.AddServiceEndpoint(serviceInterface, binding, item); |
||
173 | |||
174 | if (binding is BasicHttpBinding) |
||
175 | { |
||
176 | WebHttpBinding webHttp = new WebHttpBinding(); |
||
177 | var serviceEndpoint = serviceHost.AddServiceEndpoint(serviceInterface, webHttp, item); |
||
178 | serviceEndpoint.EndpointBehaviors.Add(new WebHttpBehavior()); |
||
179 | } |
||
180 | |||
181 | serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), bindingMex, "mex"); |
||
182 | } |
||
183 | |||
184 | |||
185 | var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>(); |
||
186 | behavior.InstanceContextMode = InstanceContextMode.Single; |
||
187 | |||
188 | serviceHost.Open(); |
||
189 | return serviceHost; |
||
190 | } |
||
191 | } |
||
192 | } |