프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.Station / Helper / WcfHostHelper.cs @ 53c9637d

이력 | 보기 | 이력해설 | 다운로드 (3.75 KB)

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
13
{
14
    public static class WebServiceHelper
15
    {
16
        public static ServiceHost WcfCreate(this ServiceHost serviceHost, object service, Type serviceInterface, IEnumerable<Uri> HostEndpoints)
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, HostEndpoints.ToArray());
30
            
31
            if (HostEndpoints.GroupBy(f => f.Scheme).Count() != HostEndpoints.Count())
32
            {
33
                throw new Exception("Host Uri's schema must be unique.");
34
            }
35

    
36
            ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
37
          
38
            mBehave.HttpGetEnabled = true;
39
            serviceHost.Description.Behaviors.Add(mBehave);
40

    
41
            //ServiceBehaviorAttribute attribute = new ServiceBehaviorAttribute();
42
            //attribute.InstanceContextMode = InstanceContextMode.Single;
43
            //serviceHost.Description.Behaviors.Add(attribute);
44

    
45
            foreach (var item in HostEndpoints)
46
            {
47
                System.ServiceModel.Channels.Binding binding = null;
48
                System.ServiceModel.Channels.Binding bindingMex = null;
49

    
50
                if (item.Scheme == Uri.UriSchemeHttp)
51
                {
52
                    binding = new BasicHttpBinding();
53
                    bindingMex = MetadataExchangeBindings.CreateMexHttpBinding();
54

    
55
                    BindingElementCollection bindElemColl = new BindingElementCollection();
56
                    BinaryMessageEncodingBindingElement bindElem = new BinaryMessageEncodingBindingElement();
57
                    bindElem.MaxReadPoolSize = 64;
58
                    bindElem.MaxWritePoolSize = 16;
59
                    bindElem.MaxSessionSize = 2048;
60
                    XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
61
                    readerQuotas.MaxDepth = 32;
62
                    readerQuotas.MaxStringContentLength = Int32.MaxValue;
63
                    readerQuotas.MaxArrayLength = 16384;
64
                    readerQuotas.MaxBytesPerRead = 4096;
65
                    readerQuotas.MaxNameTableCharCount = 16384;
66
                    bindElem.ReaderQuotas = readerQuotas;
67

    
68
                    binding.CreateBindingElements().Add(bindElem);
69
                }
70
                else if (item.Scheme == Uri.UriSchemeHttps)
71
                {
72
                    binding = new BasicHttpsBinding();
73
                    bindingMex = MetadataExchangeBindings.CreateMexHttpsBinding();
74
                }
75
                else if (item.Scheme == Uri.UriSchemeNetTcp)
76
                {
77
                    binding = new NetTcpBinding();
78
                    bindingMex = MetadataExchangeBindings.CreateMexTcpBinding();
79
                }
80
                else
81
                {
82
                    throw new Exception($"{item.ToString()} is Not Support Schema.");
83
                }
84
                
85
                serviceHost.AddServiceEndpoint(serviceInterface, binding, item);
86
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), bindingMex, "mex");
87
            }
88

    
89

    
90
            var behavior = serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
91
            behavior.InstanceContextMode = InstanceContextMode.Single;
92

    
93
            serviceHost.Open();
94
            return serviceHost;
95
        }
96
    }
97
}
클립보드 이미지 추가 (최대 크기: 500 MB)