wcfhostwcf-hosting

How to host WCF service in console apps


i am learning wcf. so i create wcf project and that has one class. code as follows

namespace TestWcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    public string GetData(string value)
    {
        return string.Format("You entered: {0}", "Welcome " + value);
    }

}
}

now when i am trying to add the wcf service reference to my console apps like add service reference and the service url like this http://localhost:21541/Service1.svc then i am getting error called Metadata contains a reference that cannot be resolved: 'http://localhost:21541/Service1.svc'.

so i am just not being able to achieve my goal. i know some where i am missing something and that is why i am getting error. so please guide me how to add service ref to console apps. app.config will be updated automatically or do i need to write anything there. help please. thanks


Solution

  • In the configuration double check that the service behavior is set up to allow service metadata:

    <serviceMetadata httpGetEnabled="true"/>

    and in the services section add a metadata endpoint

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

    EDIT: Example config

    <system.serviceModel>
      <services>
        <service behaviorConfiguration="BehaviorConfig"
          name="[ServiceNameGoesHere]">
          <endpoint address="" binding="wsHttpBinding" contract="[ServiceContractHere]">
          </endpoint>
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="BehaviorConfig">
            <serviceMetadata httpGetEnabled="True"/>
            <!-- To receive exception details in faults for debugging purposes, 
            set the value below to true.  Set to false before deployment 
            to avoid disclosing exception information -->
            <serviceDebug includeExceptionDetailInFaults="False" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
    

    There is a pretty good writeup on Dan Rigsby's blog called WCF Metadata which explains in greater details about setting up the MEX endpoints (which are required for Add Service Reference to work).