コード例 #1
0
        private ServiceHost CreateHost(Plugin <IWcfService> plugin)
        {
            try
            {
                var type = plugin.Value.GetServiceType();
                Log.Debug("Loading service {0}", type.Name);
                plugin.Value.Start();

                ServiceHost host;
                if (plugin.Value is ISingleInstanceWcfService)
                {
                    object instance = Activator.CreateInstance(type);
                    (plugin.Value as ISingleInstanceWcfService).SetInstance(instance);
                    host = new ServiceHost(instance, BaseAddresses.GetForService((string)plugin.Metadata["ServiceName"]));
                }
                else
                {
                    host = new ServiceHost(type, BaseAddresses.GetForService((string)plugin.Metadata["ServiceName"]));
                }

                // configure security if needed
                if (Configuration.Authentication.Enabled)
                {
                    foreach (var endpoint in host.Description.Endpoints)
                    {
                        // do not enable auth for stream or unauthorized endpoints
                        if (endpoint.Name == "StreamEndpoint" || endpoint.Name.StartsWith("Unauthorized"))
                        {
                            continue;
                        }

                        if (endpoint.Binding is BasicHttpBinding)
                        {
                            ((BasicHttpBinding)endpoint.Binding).Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
                            ((BasicHttpBinding)endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                        }
                        else if (endpoint.Binding is WebHttpBinding)
                        {
                            ((WebHttpBinding)endpoint.Binding).Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                            ((WebHttpBinding)endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                        }
                    }
                }

                return(host);
            }
            catch (Exception ex)
            {
                Log.Error(String.Format("Failed to load service {0}", plugin.Metadata["ServiceName"]), ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: WCFHost.cs プロジェクト: fLaSkin/MPExtended
        private ServiceHost CreateHost(ServiceAssemblyAttribute srv)
        {
            try
            {
                Log.Debug("Loading service {0}", srv.WCFType.Name);

                ServiceHost host;
                if (typeof(ISingletonService).IsAssignableFrom(srv.WCFType)) // .NET Reflection's way of writing srv.WCFType is ISingletonService
                {
                    object instance = Activator.CreateInstance(srv.WCFType);
                    (instance as ISingletonService).SetAsInstance();
                    host = new ServiceHost(instance, BaseAddresses.GetForService(srv.Service));
                }
                else
                {
                    host = new ServiceHost(srv.WCFType, BaseAddresses.GetForService(srv.Service));
                }

                // configure security if needed
                if (Configuration.Authentication.Enabled)
                {
                    foreach (var endpoint in host.Description.Endpoints)
                    {
                        // do not enable auth for stream or unauthorized endpoints
                        if (endpoint.Name == "StreamEndpoint" || endpoint.Name.StartsWith("Unauthorized"))
                        {
                            continue;
                        }

                        if (endpoint.Binding is BasicHttpBinding)
                        {
                            ((BasicHttpBinding)endpoint.Binding).Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
                            ((BasicHttpBinding)endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                        }
                        else if (endpoint.Binding is WebHttpBinding)
                        {
                            ((WebHttpBinding)endpoint.Binding).Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                            ((WebHttpBinding)endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                        }
                    }
                }

                return(host);
            }
            catch (Exception ex)
            {
                Log.Error(String.Format("Failed to load service {0}", srv.WCFType.Name), ex);
                return(null);
            }
        }