public void Start() { this._UniqueId = _serviceConfig.UniqueId; string httpEndPstr = @"http://{0}:{1}/WcfPortal".FormatWith(_serviceConfig.HostAddress, _serviceConfig.HostPort); Uri httpUri = new Uri(httpEndPstr); _serviceHost = new ServiceHost(typeof(SAF.EntityFramework.Server.Hosts.WcfPortal), httpUri); Type t = _serviceHost.GetType(); object obj = t.Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior", true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { false, Int32.MaxValue }, null, null); IServiceBehavior myServiceBehavior = obj as IServiceBehavior; if (myServiceBehavior != null) { _serviceHost.Description.Behaviors.Add(myServiceBehavior); } if (_serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { var behavior = new ServiceMetadataBehavior(); //交换方式 behavior.HttpGetEnabled = true; //元数据交换地址 behavior.HttpGetUrl = httpUri; //将行为添加到宿主上 _serviceHost.Description.Behaviors.Add(behavior); } if (_serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>() == null) { var behavior = new ServiceDebugBehavior(); //交换方式 behavior.IncludeExceptionDetailInFaults = true; //将行为添加到宿主上 _serviceHost.Description.Behaviors.Add(behavior); } _serviceHost.AddServiceEndpoint(typeof(SAF.EntityFramework.Server.Hosts.IWcfPortal), new WSHttpBinding(SecurityMode.None), ""); _serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); _thread = new Thread(RunService); _thread.IsBackground = true; _thread.Start(); }
/// <summary> /// Applies the service host settings. /// </summary> /// <param name="host">Host.</param> /// <param name="useCustomBehaviours">If set to <c>true</c> use custom behaviours.</param> private static void ApplyServiceHostSettings(ServiceHost host, bool useCustomBehaviours = false) { host.OpenTimeout = host.CloseTimeout = _timeout; ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>(); if (sdb == null) host.Description.Behaviors.Add(sdb = new ServiceDebugBehavior()); sdb.IncludeExceptionDetailInFaults = true; host.AddServiceEndpoint(typeof(IRepository), new NetTcpBinding(SecurityMode.None) { MaxBufferSize = 4096, ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxStringContentLength = 32768 } }, new Uri("net.tcp://localhost:3334/ArtefactRepository")); if (useCustomBehaviours) ApplyServiceHostBehaviours(host); host.Opened += (sender, e) => _output.WriteLine("{0}: Opened", host.GetType().Name); host.Opening += (sender, e) => _output.WriteLine("{0}: Opening", host.GetType().Name); host.Closed += (sender, e) => _output.WriteLine("{0}: Closed", host.GetType().Name); host.Closing += (sender, e) => _output.WriteLine("{0}: Closing", host.GetType().Name); host.Faulted += (sender, e) => _error.WriteLine("{0}: Faulted", host.GetType().Name); host.UnknownMessageReceived += (sender, e) => _error.WriteLine("{0}: UnknownMessageReceived", host.GetType().Name); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { ServiceHost host = new ServiceHost(serviceType, baseAddresses); //base.CreateServiceHost(serviceType, baseAddresses); Type contractType = FindServiceContractInterface(serviceType); host.AddServiceEndpoint(contractType, FindBinding(m_BindingType), ""); ServiceMetadataBehavior b1 = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); if (b1 == null) { b1 = new ServiceMetadataBehavior() { HttpGetEnabled = true }; host.Description.Behaviors.Add(b1); } else { b1.HttpGetEnabled = true; } StandardExceptionBehavior b = host.Description.Behaviors.Find<StandardExceptionBehavior>(); if (b == null) { host.Description.Behaviors.Add(new StandardExceptionBehavior(m_BizExceptionTypeName, m_ExceptionHandlerTypeName)); } //if (url.IndexOf("http://") < 0) //{ // b1.HttpGetUrl = new Uri(string.Format("http://{0}{1}{2}", hostIP, (service.Port > 0 ? (":" + service.Port) : string.Empty), url)); //} ServiceDebugBehavior b2 = host.Description.Behaviors.Find<ServiceDebugBehavior>(); if (b2 == null) { host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true }); } else { b2.IncludeExceptionDetailInFaults = true; } ServiceBehaviorAttribute bb = host.Description.Behaviors.Find<ServiceBehaviorAttribute>(); if (bb == null) { bb = new ServiceBehaviorAttribute(); host.Description.Behaviors.Add(bb); } bb.ConcurrencyMode = ConcurrencyMode.Multiple; bb.AddressFilterMode = AddressFilterMode.Any; bb.InstanceContextMode = InstanceContextMode.Single; bb.MaxItemsInObjectGraph = Int32.MaxValue; if (ServiceHostingEnvironment.AspNetCompatibilityEnabled) { AspNetCompatibilityRequirementsAttribute a = host.Description.Behaviors.Find<AspNetCompatibilityRequirementsAttribute>(); if (a == null) { host.Description.Behaviors.Add(new AspNetCompatibilityRequirementsAttribute() { RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed }); } else { a.RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed; } } //------- 设置 dataContractSerializer的 maxItemsInObjectGraph属性为int.MaxValue Type t = host.GetType(); object obj = t.Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior", true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { false, int.MaxValue }, null, null); IServiceBehavior myServiceBehavior = obj as IServiceBehavior; if (myServiceBehavior != null) { host.Description.Behaviors.Add(myServiceBehavior); } //------- return host; }