protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        string[] urlTokens = baseAddresses[0].AbsolutePath.Split('/');
        string   version   = urlTokens[1].ToUpper();

        Type[] contractInterfaces = serviceType.GetInterfaces().Where(i => i.GetCustomAttributes(true).Where(a => a.GetType() == typeof(System.ServiceModel.ServiceContractAttribute)).Any()).ToArray();
        Type   contractType       = contractInterfaces.Where(i => i.Namespace.ToUpper().Contains(version)).Single();

        ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);

        WSHttpBinding wsHttpBinding = new WSHttpBinding();

        wsHttpBinding.SendTimeout            = new TimeSpan(0, 5, 0);
        wsHttpBinding.MaxReceivedMessageSize = Int32.MaxValue;
        wsHttpBinding.BypassProxyOnLocal     = true;
        wsHttpBinding.Security.Mode          = SecurityMode.None;
        //wsHttpBinding.TransactionFlow = true;
        //wsHttpBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;

        //ServiceEndpoint endpoint = host.AddServiceEndpoint(contractType, wsHttpBinding, "");

        WebHttpBinding webHttpBinding = new WebHttpBinding();

        webHttpBinding.Security.Mode = WebHttpSecurityMode.None;
        webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        webHttpBinding.Security.Transport.ProxyCredentialType  = HttpProxyCredentialType.None;
        webHttpBinding.BypassProxyOnLocal = true;

        ServiceEndpoint endpoint = host.AddServiceEndpoint(contractType, webHttpBinding, "");

        WebHttpBehavior webHttpBehavior = new WebHttpBehavior();

        webHttpBehavior.DefaultOutgoingRequestFormat = WebMessageFormat.Json;
        //behavior.DefaultBodyStyle = WebMessageBodyStyle.Bare;
        webHttpBehavior.DefaultBodyStyle      = WebMessageBodyStyle.Wrapped;
        webHttpBehavior.FaultExceptionEnabled = true;

        endpoint.Behaviors.Add(webHttpBehavior);

        ServiceMetadataBehavior metadataBehaviour;

        if ((host.Description.Behaviors.Contains(typeof(ServiceMetadataBehavior))))
        {
            metadataBehaviour = (ServiceMetadataBehavior)host.Description.Behaviors[typeof(ServiceMetadataBehavior)];
        }
        else
        {
            metadataBehaviour = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(metadataBehaviour);
        }
        metadataBehaviour.HttpGetEnabled = true;

        ServiceDebugBehavior debugBehaviour;

        if (host.Description.Behaviors.Contains(typeof(ServiceDebugBehavior)))
        {
            debugBehaviour = (ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)];
        }
        else
        {
            debugBehaviour = new ServiceDebugBehavior();
            host.Description.Behaviors.Add(debugBehaviour);
        }
        debugBehaviour.IncludeExceptionDetailInFaults = true;

        return(host);
    }
예제 #2
0
        public static ServiceHost AddMetadataBehaviors(
            this ServiceHost host,
            MetadataFeatures features = MetadataFeatures.All)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }


            var serviceMetadataBehavior = host.Description.Behaviors.Find <ServiceMetadataBehavior>();

            // if no metadata is requested - remove the behavior altogether
            if (features == MetadataFeatures.None)
            {
                if (serviceMetadataBehavior != null)
                {
                    host.Description.Behaviors.Remove(serviceMetadataBehavior);
                }

                return(host);
            }

            // add the metadata behavior, if it is not there yet
            if (serviceMetadataBehavior == null)
            {
                serviceMetadataBehavior = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(serviceMetadataBehavior);
            }

            // disable the help page if not requested or if we have REST-ful endpoints
            if (!features.HasFlag(MetadataFeatures.HelpPage) ||
                host.Description.Endpoints.Any(ep => ep.Binding is WebHttpBinding))
            {
                var serviceDebugBehavior = host.Description.Behaviors.Find <ServiceDebugBehavior>();

                if (serviceDebugBehavior != null)
                {
                    serviceDebugBehavior.HttpHelpPageEnabled      =
                        serviceDebugBehavior.HttpsHelpPageEnabled = false;
                }

                var ep = host.Description.Endpoints.Where(e => e.Binding is WebHttpBinding).FirstOrDefault();

                if (ep != null)
                {
                    if (ep.EndpointBehaviors.Where(b => b is WebHttpBehavior).FirstOrDefault() is WebHttpBehavior epb)
                    {
                        epb.HelpEnabled = false;
                    }
                }
            }

            // disable the Web HTTP (REST) help page
            if (!features.HasFlag(MetadataFeatures.WebHttpHelpPage))
            {
                var ep = host.Description.Endpoints.Where(e => e.Binding is WebHttpBinding).FirstOrDefault();

                if (ep != null)
                {
                    if (ep.EndpointBehaviors.Where(b => b is WebHttpBehavior).FirstOrDefault() is WebHttpBehavior epb)
                    {
                        epb.HelpEnabled = false;
                    }
                }
            }

            // add the GET WSDL behavior if requested
            if (features.HasFlag(MetadataFeatures.Wsdl))
            {
                serviceMetadataBehavior.HttpsGetEnabled = host.BaseAddresses.Any(a => a.Scheme == Uri.UriSchemeHttps);
#if DEBUG
                serviceMetadataBehavior.HttpGetEnabled = host.BaseAddresses.Any(a => a.Scheme == Uri.UriSchemeHttp);
#endif

                Debug.WriteLineIf(
                    !serviceMetadataBehavior.HttpGetEnabled && !serviceMetadataBehavior.HttpsGetEnabled,
                    "\n**** If you need a GET service metadata (WSDL) behavior, define a base address with an http(s) scheme. ***\n");
            }
            else
            {
                serviceMetadataBehavior.HttpGetEnabled      =
                    serviceMetadataBehavior.HttpsGetEnabled = false;
                serviceMetadataBehavior.HttpGetUrl          =
                    serviceMetadataBehavior.HttpsGetUrl     = null;
            }

            // if IMetadataExchange contract is requested - add it
            if (features.HasFlag(MetadataFeatures.Mex))
            {
                host.AddMexEndpoints();
            }

            return(host);
        }
예제 #3
0
        private void InitializeServer()
        {
            BasicHttpBinding BasicBinding = new BasicHttpBinding();

            BasicBinding.Name                   = "sonesBasic";
            BasicBinding.Namespace              = Namespace;
            BasicBinding.MessageEncoding        = WSMessageEncoding.Text;
            BasicBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            BasicBinding.MaxBufferSize          = 268435456;
            BasicBinding.MaxReceivedMessageSize = 268435456;
            BasicBinding.SendTimeout            = new TimeSpan(1, 0, 0);
            BasicBinding.ReceiveTimeout         = new TimeSpan(1, 0, 0);
            XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();

            readerQuotas.MaxDepth = 2147483647;
            readerQuotas.MaxStringContentLength = 2147483647;
            readerQuotas.MaxBytesPerRead        = 2147483647;
            readerQuotas.MaxNameTableCharCount  = 2147483647;
            readerQuotas.MaxArrayLength         = 2147483647;
            BasicBinding.ReaderQuotas           = readerQuotas;


            BasicHttpBinding StreamedBinding = new BasicHttpBinding();

            StreamedBinding.Name                   = "sonesStreamed";
            StreamedBinding.Namespace              = Namespace;
            StreamedBinding.MessageEncoding        = WSMessageEncoding.Text;
            StreamedBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            StreamedBinding.MaxReceivedMessageSize = 2147483647;
            StreamedBinding.MaxBufferSize          = (UseStreaming == true) ? 4096 : 2147483647;
            StreamedBinding.SendTimeout            = new TimeSpan(1, 0, 0, 0);
            StreamedBinding.ReceiveTimeout         = new TimeSpan(1, 0, 0, 0);
            if (UseStreaming == true)
            {
                StreamedBinding.TransferMode = TransferMode.Streamed;
            }

            WebHttpBinding WebBinding = new WebHttpBinding();

            WebBinding.Name      = "sonesWeb";
            WebBinding.Namespace = Namespace;
            WebBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;

            if (IsSecure)
            {
                BasicBinding.Security.Mode    = BasicHttpSecurityMode.Transport;
                StreamedBinding.Security.Mode = BasicHttpSecurityMode.Transport;
            }


            RPCServiceContract ContractInstance = new RPCServiceContract(_GraphDS);


            _ServiceHost = new ServiceHost(ContractInstance, this.URI);
            _ServiceHost.Description.Namespace = Namespace;

            #region Streamed Contract

            ContractDescription StreamedContract = ContractDescription.GetContract(typeof(IStreamedService));
            StreamedContract.Namespace = Namespace;
            ServiceEndpoint StreamedService = new ServiceEndpoint(StreamedContract, StreamedBinding, new EndpointAddress(this.URI.ToString() + "/streamed"));
            _ServiceHost.AddServiceEndpoint(StreamedService);

            #endregion


            #region GraphDS API Contract

            ContractDescription APIContract = ContractDescription.GetContract(typeof(IGraphDS_API));
            APIContract.Namespace = Namespace;

            #region SOAP
            ServiceEndpoint APIService = new ServiceEndpoint(APIContract, BasicBinding, new EndpointAddress(this.URI.ToString()));
            _ServiceHost.AddServiceEndpoint(APIService);
            #endregion

            #region json
            //ServiceEndpoint APIServiceJson = new ServiceEndpoint(APIContract, WebBinding, new EndpointAddress(this.URI.ToString() + "/json"));
            //APIServiceJson.Behaviors.Add(new WebScriptEnablingBehavior());
            //_ServiceHost.AddServiceEndpoint(APIServiceJson);
            #endregion

            #endregion

            #region Type Services

            #region VertexTypeService

            ContractDescription VertexTypeServiceContract = ContractDescription.GetContract(typeof(IVertexTypeService));
            VertexTypeServiceContract.Namespace = Namespace;
            ServiceEndpoint VertexTypeService = new ServiceEndpoint(VertexTypeServiceContract, BasicBinding, new EndpointAddress(this.URI.ToString()));
            _ServiceHost.AddServiceEndpoint(VertexTypeService);

            #endregion

            #region VertexInstanceService

            ContractDescription VertexServiceContract = ContractDescription.GetContract(typeof(IVertexService));
            VertexServiceContract.Namespace = Namespace;
            ServiceEndpoint VertexService = new ServiceEndpoint(VertexServiceContract, BasicBinding, new EndpointAddress(this.URI.ToString()));
            _ServiceHost.AddServiceEndpoint(VertexService);

            #endregion

            #region EdgeTypeService

            ContractDescription EdgeTypeServiceContract = ContractDescription.GetContract(typeof(IEdgeTypeService));
            EdgeTypeServiceContract.Namespace = Namespace;
            ServiceEndpoint EdgeTypeService = new ServiceEndpoint(EdgeTypeServiceContract, BasicBinding, new EndpointAddress(this.URI.ToString()));
            _ServiceHost.AddServiceEndpoint(EdgeTypeService);

            #endregion

            #region EdgeInstanceService

            ContractDescription EdgeInstanceServiceContract = ContractDescription.GetContract(typeof(IEdgeService));
            EdgeInstanceServiceContract.Namespace = Namespace;
            ServiceEndpoint EdgeInstanceService = new ServiceEndpoint(EdgeInstanceServiceContract, BasicBinding, new EndpointAddress(this.URI.ToString()));
            _ServiceHost.AddServiceEndpoint(EdgeInstanceService);

            #endregion

            #endregion

            #region Metadata Exchange

            /*
             * When using automatic wsdl generation, there are errors when not using C# Visual Studio:
             *   - (server side) Mono does not yet support wsdl generation
             *   - Java Stub generation using Axis causes the server to hang up
             *   - Some importers may not support multiple markup files
             *
             * Therefore, a static wsdl file is provided to the client.
             */

            _MexServiceHost = new ServiceHost(typeof(MonoMEX), MexUri);
            _MexServiceHost.Description.Namespace = Namespace;
            Binding MexBinding = new WebHttpBinding();
            MexBinding.Namespace = Namespace;

            ContractDescription MexContract = ContractDescription.GetContract(typeof(IMonoMEX));
            ServiceEndpoint     MexService  = new ServiceEndpoint(MexContract, MexBinding, new EndpointAddress(MexUri.ToString()));
            _MexServiceHost.AddServiceEndpoint(MexService);
            _MexServiceHost.Description.Endpoints[0].Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());


            #region auto generation
            #if __MonoCS__
            #else
            ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
            metadataBehavior.HttpGetEnabled = true;
            Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
            mexBinding.Namespace = Namespace;
            _ServiceHost.Description.Behaviors.Add(metadataBehavior);
            ServiceEndpoint mexEndpoint = _ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, mexBinding, "mex");
            foreach (ServiceEndpoint endpoint in _ServiceHost.Description.Endpoints)
            {
                //export just one file
                endpoint.Behaviors.Add(new WsdlExtensions(new WsdlExtensionsConfig()
                {
                    SingleFile = true
                }));
            }
            #endif
            #endregion


            #endregion
        }
        internal static IMirasyInterfaceService Initialise(MirasysVaInterface _MirasysVaInterface)
        {
            InsertLog.AddLog("MirasyInterfaceService Initialise()");
            MirasysVaInterface = _MirasysVaInterface;
            var service = new MirasyInterfaceService();
            Uri httpUrl = new Uri("https://localhost:6530/MirasyInterfaceService");

            WebServiceHost controllerHost
                = new WebServiceHost(typeof(MirasyInterfaceService), httpUrl);

            WebHttpBinding _webhttpbis = new WebHttpBinding(WebHttpSecurityMode.Transport);

            _webhttpbis.ReceiveTimeout         = TimeSpan.Parse("00:10:00");
            _webhttpbis.CloseTimeout           = TimeSpan.Parse("00:10:00");
            _webhttpbis.OpenTimeout            = TimeSpan.Parse("00:10:00");
            _webhttpbis.SendTimeout            = TimeSpan.Parse("00:10:00");
            _webhttpbis.MaxBufferPoolSize      = 2147483647; //524288;
            _webhttpbis.MaxBufferSize          = 2147483647;
            _webhttpbis.MaxReceivedMessageSize = 2147483647; //1073741823;
            _webhttpbis.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;


            var endpoint = controllerHost.AddServiceEndpoint(typeof(IMirasyInterfaceService), _webhttpbis, "");//WSHttpBinding

            ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
            {
                MaxConcurrentCalls     = 500,
                MaxConcurrentInstances = 500,
                MaxConcurrentSessions  = 500,
            };

            controllerHost.Description.Behaviors.Add(throttleBehavior);

            var _ServiceMetadataBehavior = controllerHost.Description.Behaviors.Find <ServiceMetadataBehavior>();

            if (_ServiceMetadataBehavior == null)
            {
                _ServiceMetadataBehavior = new ServiceMetadataBehavior();
                controllerHost.Description.Behaviors.Add(_ServiceMetadataBehavior);
            }

            _ServiceMetadataBehavior.HttpGetEnabled  = true;
            _ServiceMetadataBehavior.HttpsGetEnabled = true;

            var _ServiceDebugBehavior = controllerHost.Description.Behaviors.Find <ServiceDebugBehavior>();

            if (_ServiceDebugBehavior == null)
            {
                _ServiceDebugBehavior = new ServiceDebugBehavior();
                controllerHost.Description.Behaviors.Add(_ServiceDebugBehavior);
            }
            _ServiceDebugBehavior.IncludeExceptionDetailInFaults = true;

            var serviceCredential = controllerHost.Description.Behaviors.Find <ServiceCredentials>();

            if (serviceCredential == null)
            {
                serviceCredential = new ServiceCredentials();
                controllerHost.Description.Behaviors.Add(serviceCredential);
            }

            serviceCredential.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,
                                                                StoreName.My,
                                                                X509FindType.FindByThumbprint,
                                                                "f55bf424542522c6a6d833f933431a31baaf43de");
            controllerHost.Open();
            InsertLog.AddLog("MirasyInterfaceService Started Successfully");
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);   //amit 04112016

            return(null);
        }