Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#

WCF Ping

Rate me:
Please Sign up or sign in to vote.
4.63/5 (5 votes)
28 Jun 2010CPOL4 min read 61.6K   2.9K   25  
A Command-line utility to ping WCF Services
using System;
using System.Linq;
using System.Text;
using System.Reflection;

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

using System.CodeDom;
using System.CodeDom.Compiler;

using System.Xml.Serialization;
using System.Runtime.Serialization;
using WSvc = System.Web.Services.Description;

using System.Data;
using System.Data.Design;

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WCFPing.Lib
{
    class MetadataImporter
    {
        internal static ServiceProperties Import(CodeGenProvider codeGenProvider, string url, string contractName)
        {
            try
            {
                Uri baseAddress = new Uri(url);
                Binding binding = CreateBinding(baseAddress);

                MetadataExchangeClient mexClient = new MetadataExchangeClient(binding);
                MetadataSet metadataSet = mexClient.GetMetadata(baseAddress, MetadataExchangeClientMode.MetadataExchange);
                WsdlImporter wsdlImporter = new WsdlImporter(metadataSet);

                //DataContractImporter options
                XsdDataContractImporter xsdDataContractImporter = new XsdDataContractImporter(codeGenProvider.CodeCompileUnit);
                xsdDataContractImporter.Options = new ImportOptions();
                xsdDataContractImporter.Options.ImportXmlType = false;//default is false
                xsdDataContractImporter.Options.CodeProvider = codeGenProvider.CodeDomProvider;
                wsdlImporter.State.Add(typeof(XsdDataContractImporter), xsdDataContractImporter);

                foreach (IWsdlImportExtension importXt in wsdlImporter.WsdlImportExtensions)
                {
                    DataContractSerializerMessageContractImporter dcCtr =
                        importXt as DataContractSerializerMessageContractImporter;
                    if (dcCtr != null) dcCtr.Enabled = true;
                }

                //XMLImporter options
                XmlSerializerImportOptions importOpt = new XmlSerializerImportOptions(codeGenProvider.CodeCompileUnit);
                importOpt.CodeProvider = codeGenProvider.CodeDomProvider;
                importOpt.WebReferenceOptions = new WSvc.WebReferenceOptions();
                importOpt.WebReferenceOptions.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateOrder;
                importOpt.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(TypedDataSetSchemaImporterExtension).AssemblyQualifiedName);
                importOpt.WebReferenceOptions.SchemaImporterExtensions.Add(typeof(DataSetSchemaImporterExtension).AssemblyQualifiedName);
                wsdlImporter.State.Add(typeof(XmlSerializerImportOptions), importOpt);

                //Importing Binding, Contracts, Endpoints
                List<Binding> bindings = wsdlImporter.ImportAllBindings().ToList<Binding>();
                List<ContractDescription> contracts = wsdlImporter.ImportAllContracts().ToList<ContractDescription>();
                List<ServiceEndpoint> endpoints = wsdlImporter.ImportAllEndpoints().ToList<ServiceEndpoint>();
                List<MetadataConversionError> metadataConversionErrors = wsdlImporter.Errors.ToList<MetadataConversionError>();
                metadataConversionErrors.ThrowExceptionOnError();

                return new ServiceProperties(bindings, contracts, endpoints);
            }
            catch (Exception ex) { throw new WCFPingException("Import MetadataSet failed.", ex); }
            
        }

        private static Binding CreateBinding(Uri baseAddress) 
        {
            Binding binding = MetadataExchangeBindings.CreateMexHttpBinding();
            if (baseAddress.Scheme == Uri.UriSchemeHttp)
                binding = new WSHttpBinding(SecurityMode.None);
            else if (baseAddress.Scheme == Uri.UriSchemeNetTcp)
                binding = new NetTcpBinding(SecurityMode.None);
            else if (baseAddress.Scheme == Uri.UriSchemeNetPipe)
                binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            else
                throw new WCFPingException("CreateBinding failed. UriScheme is not supported.");

            binding.GetType().GetProperty("MaxReceivedMessageSize").SetValue(binding, Int32.MaxValue, null);
            return binding;
        }
    }
}


//private static bool UriSchemeSupportsDisco(Uri serviceUri)
//{
//    if (!(serviceUri.Scheme == Uri.UriSchemeHttp))
//    {
//        return (serviceUri.Scheme == Uri.UriSchemeHttps);
//    }
//    return true;
//}

//private static IEnumerable<MetadataSection> ResolveMetadata(EndpointAddress epr, bool useDisco, ClientSection clientSection)
//{
//    MetadataSet metadata;
//    try
//    {
//        if (useDisco)
//        {
//            DiscoveryClientProtocol protocol = new DiscoveryClientProtocol();
//            protocol.UseDefaultCredentials = true;
//            protocol.AllowAutoRedirect = true;
//            protocol.DiscoverAny(epr.Uri.AbsoluteUri);
//            protocol.ResolveAll();
//            metadata = new MetadataSet();
//            foreach (object obj2 in protocol.Documents.Values)
//            {
//                AddDocumentToSet(metadata, obj2);
//            }
//        }
//        else
//        {
//            MetadataExchangeClient client = CreateMetadataExchangeClient(epr, clientSection);
//            client.OperationTimeout = TimeSpan.FromMinutes(5.0);
//            metadata = client.GetMetadata(epr);
//        }
//    }
//    catch (Exception exception)
//    {
//        throw new ToolInputException(exception.Message, exception);
//    }
//    return metadata.MetadataSections;
//}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions