Ejemplo n.º 1
0
            internal MetadataSet GenerateMetadata()
            {
                if (this.behavior.ExternalMetadataLocation == null || this.behavior.ExternalMetadataLocation.ToString() == string.Empty)
                {
                    if (this.metadataGenerationException != null)
                    {
                        throw this.metadataGenerationException;
                    }

                    try
                    {
                        MetadataExporter             exporter          = this.behavior.MetadataExporter;
                        XmlQualifiedName             serviceName       = new XmlQualifiedName(this.description.Name, this.description.Namespace);
                        Collection <ServiceEndpoint> exportedEndpoints = new Collection <ServiceEndpoint>();
                        foreach (ServiceEndpoint endpoint in this.description.Endpoints)
                        {
                            ServiceMetadataContractBehavior contractBehavior = endpoint.Contract.Behaviors.Find <ServiceMetadataContractBehavior>();

                            // if contract behavior exists, generate metadata when the behavior allows metadata generation
                            // if contract behavior doesn't exist, generate metadata only for non system endpoints
                            if ((contractBehavior != null && !contractBehavior.MetadataGenerationDisabled) ||
                                (contractBehavior == null && !endpoint.IsSystemEndpoint))
                            {
                                EndpointAddress    address            = null;
                                EndpointDispatcher endpointDispatcher = GetListenerByID(this.host.ChannelDispatchers, endpoint.Id);
                                if (endpointDispatcher != null)
                                {
                                    address = endpointDispatcher.EndpointAddress;
                                }
                                ServiceEndpoint exportedEndpoint = new ServiceEndpoint(endpoint.Contract);
                                exportedEndpoint.Binding = endpoint.Binding;
                                exportedEndpoint.Name    = endpoint.Name;
                                exportedEndpoint.Address = address;
                                foreach (IEndpointBehavior behavior in endpoint.Behaviors)
                                {
                                    exportedEndpoint.Behaviors.Add(behavior);
                                }
                                exportedEndpoints.Add(exportedEndpoint);
                            }
                        }
                        WsdlExporter wsdlExporter = exporter as WsdlExporter;
                        if (wsdlExporter != null)
                        {
                            // Pass the BindingParameterCollection into the ExportEndpoints method so that the binding parameters can be using to export WSDL correctly.
                            // The binding parameters are used in BuildChannelListener, during which they can modify the configuration of the channel in ways that might have to
                            // be communicated in the WSDL. For example, in the case of Multi-Auth, the AuthenticationSchemesBindingParameter is used during BuildChannelListener
                            // to set the AuthenticationSchemes supported by the virtual directory on the HttpTransportBindingElement.  These authentication schemes also need
                            // to be in the WSDL, so that clients know what authentication schemes are supported by the service.  (see CSDMain #180381)
                            Fx.Assert(this.host != null, "ServiceHostBase field on MetadataExtensionInitializer should never be null.");
                            wsdlExporter.ExportEndpoints(exportedEndpoints, serviceName, this.host.GetBindingParameters(exportedEndpoints));
                        }
                        else
                        {
                            foreach (ServiceEndpoint endpoint in exportedEndpoints)
                            {
                                exporter.ExportEndpoint(endpoint);
                            }
                        }

                        if (exporter.Errors.Count > 0 && DiagnosticUtility.ShouldTraceWarning)
                        {
                            TraceWsdlExportErrors(exporter);
                        }

                        return(exporter.GetGeneratedMetadata());
                    }
                    catch (Exception e)
                    {
                        this.metadataGenerationException = e;
                        throw;
                    }
                }
                return(null);
            }
 internal MetadataSet GenerateMetadata()
 {
     if ((this.behavior.ExternalMetadataLocation == null) || (this.behavior.ExternalMetadataLocation.ToString() == string.Empty))
     {
         if (this.metadataGenerationException != null)
         {
             throw this.metadataGenerationException;
         }
         try
         {
             MetadataExporter             metadataExporter = this.behavior.MetadataExporter;
             XmlQualifiedName             wsdlServiceQName = new XmlQualifiedName(this.description.Name, this.description.Namespace);
             Collection <ServiceEndpoint> endpoints        = new Collection <ServiceEndpoint>();
             foreach (ServiceEndpoint endpoint in this.description.Endpoints)
             {
                 ServiceMetadataContractBehavior behavior = endpoint.Contract.Behaviors.Find <ServiceMetadataContractBehavior>();
                 if (((behavior != null) && !behavior.MetadataGenerationDisabled) || ((behavior == null) && !endpoint.IsSystemEndpoint))
                 {
                     EndpointAddress    endpointAddress = null;
                     EndpointDispatcher listenerByID    = ServiceMetadataBehavior.GetListenerByID(this.host.ChannelDispatchers, endpoint.Id);
                     if (listenerByID != null)
                     {
                         endpointAddress = listenerByID.EndpointAddress;
                     }
                     ServiceEndpoint item = new ServiceEndpoint(endpoint.Contract)
                     {
                         Binding = endpoint.Binding,
                         Name    = endpoint.Name,
                         Address = endpointAddress
                     };
                     foreach (IEndpointBehavior behavior2 in endpoint.Behaviors)
                     {
                         item.Behaviors.Add(behavior2);
                     }
                     endpoints.Add(item);
                 }
             }
             WsdlExporter exporter2 = metadataExporter as WsdlExporter;
             if (exporter2 != null)
             {
                 exporter2.ExportEndpoints(endpoints, wsdlServiceQName);
             }
             else
             {
                 foreach (ServiceEndpoint endpoint3 in endpoints)
                 {
                     metadataExporter.ExportEndpoint(endpoint3);
                 }
             }
             if ((metadataExporter.Errors.Count > 0) && DiagnosticUtility.ShouldTraceWarning)
             {
                 TraceWsdlExportErrors(metadataExporter);
             }
             return(metadataExporter.GetGeneratedMetadata());
         }
         catch (Exception exception)
         {
             this.metadataGenerationException = exception;
             throw;
         }
     }
     return(null);
 }