1
|
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 { TransferMode = TransferMode.Buffered};
|
39
|
|
40
|
httpbinding.CloseTimeout = new TimeSpan(0, 10, 0);
|
41
|
httpbinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
|
42
|
httpbinding.SendTimeout = new TimeSpan(0, 10, 0);
|
43
|
httpbinding.OpenTimeout = new TimeSpan(0, 10, 0);
|
44
|
|
45
|
Binding bindingMex = MetadataExchangeBindings.CreateMexHttpBinding();
|
46
|
bindingMex.CloseTimeout = new TimeSpan(0, 10, 0);
|
47
|
bindingMex.ReceiveTimeout = new TimeSpan(0, 10, 0);
|
48
|
bindingMex.SendTimeout = new TimeSpan(0, 10, 0);
|
49
|
bindingMex.OpenTimeout = new TimeSpan(0, 10, 0);
|
50
|
|
51
|
httpbinding.CreateBindingElements().Add(gBindingElement);
|
52
|
|
53
|
var httpEndpoint = serviceHost.AddServiceEndpoint(serviceInterface, httpbinding, "");
|
54
|
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), bindingMex, "mex");
|
55
|
|
56
|
Binding restBinding = new WebHttpBinding();// WebHttpSecurityMode.None);
|
57
|
restBinding.CloseTimeout = new TimeSpan(0, 10,0);
|
58
|
restBinding.ReceiveTimeout = new TimeSpan(0, 10,0);
|
59
|
restBinding.SendTimeout = new TimeSpan(0, 10,0);
|
60
|
|
61
|
restBinding.CreateBindingElements().Add(gBindingElement);
|
62
|
|
63
|
var restEndpoint = serviceHost.AddServiceEndpoint(serviceInterface, restBinding, "Rest");
|
64
|
|
65
|
|
66
|
restEndpoint.EndpointBehaviors.Add(new WebHttpBehavior {
|
67
|
HelpEnabled = true,
|
68
|
DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped,
|
69
|
AutomaticFormatSelectionEnabled =false,
|
70
|
DefaultOutgoingResponseFormat= System.ServiceModel.Web.WebMessageFormat.Json
|
71
|
});
|
72
|
|
73
|
}
|
74
|
|
75
|
var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
|
76
|
behavior.ConcurrencyMode = ConcurrencyMode.Multiple;
|
77
|
behavior.InstanceContextMode = InstanceContextMode.Single;
|
78
|
|
79
|
serviceHost.Open();
|
80
|
return serviceHost;
|
81
|
}
|
82
|
|
83
|
|
84
|
public static BinaryMessageEncodingBindingElement gBindingElement = new BinaryMessageEncodingBindingElement
|
85
|
{
|
86
|
MaxReadPoolSize = Int32.MaxValue,
|
87
|
MaxWritePoolSize = Int32.MaxValue,
|
88
|
MaxSessionSize = Int32.MaxValue,
|
89
|
ReaderQuotas = GetReaderQuotas()
|
90
|
};
|
91
|
|
92
|
public static XmlDictionaryReaderQuotas GetReaderQuotas()
|
93
|
{
|
94
|
return new XmlDictionaryReaderQuotas
|
95
|
{
|
96
|
MaxDepth = Int16.MaxValue,
|
97
|
MaxStringContentLength = Int32.MaxValue,
|
98
|
MaxArrayLength = Int32.MaxValue,
|
99
|
MaxBytesPerRead = Int32.MaxValue,
|
100
|
MaxNameTableCharCount = Int32.MaxValue
|
101
|
};
|
102
|
}
|
103
|
}
|
104
|
}
|