none
How to generate wsdl from WCF service?

Answers

  • The easiest way is to add a ServiceMetadataBehavior on your service (with the property HttpGetEnabled set to true), run it, and point the browser to the service base address.

     

    Example (via code):

     

    public class Post2035994

    {

        [ServiceContract]

        public interface ITest

        {

            [OperationContract]

            string Echo(string input);

        }

        public class Service : ITest

        {

            public string Echo(string input) { return input; }

        }

        public static void Main(string[] args)

        {

            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";

            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;

            host.Description.Behaviors.Add(smb);

            host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");

            host.Open();

            Console.Write("Press ENTER to close");

            Console.ReadLine();

            host.Close();

        }

    }

     

    The WSDL will be at http://localhost:8000/Service?wsdl

     

    In config:

    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    

     

    And on your service definition:

    <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
    

     

    Tuesday, August 21, 2007 8:10 PM

All replies

  • The easiest way is to add a ServiceMetadataBehavior on your service (with the property HttpGetEnabled set to true), run it, and point the browser to the service base address.

     

    Example (via code):

     

    public class Post2035994

    {

        [ServiceContract]

        public interface ITest

        {

            [OperationContract]

            string Echo(string input);

        }

        public class Service : ITest

        {

            public string Echo(string input) { return input; }

        }

        public static void Main(string[] args)

        {

            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";

            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;

            host.Description.Behaviors.Add(smb);

            host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");

            host.Open();

            Console.Write("Press ENTER to close");

            Console.ReadLine();

            host.Close();

        }

    }

     

    The WSDL will be at http://localhost:8000/Service?wsdl

     

    In config:

    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    

     

    And on your service definition:

    <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
    

     

    Tuesday, August 21, 2007 8:10 PM
  • How to generate single wsdl file without any reference?

     

    Tuesday, August 21, 2007 8:44 PM
  • Not really, WCF will always separate the WSDL part from the schema part (XSD). It will also generate the WSDL in different documents if you have different namespaces for the binding/contract/service.

     

    You could, however, merge them yourself, by loading them as XML documents, and replacing the import nodes with the documents read from their locations.

    Tuesday, August 21, 2007 10:35 PM