Manually configuring a new WCF endpoint

2019-08-07 21:10发布

I have a simple WCF service:

namespace Vert.Host.VertService
{
    [ServiceContract]
    public interface IRSVP
    {
        [OperationContract]
        bool Attending();

        [OperationContract]
        bool NotAttending();
    }

    public class RSVPService : IRSVP
    {
        public RSVPService()
        {
        }

        public bool Attending()
        {
            return true;
        }

        public bool NotAttending()
        {
            return true;
        }
    }
}

I'd like to self-host in a console application like so:

class Program
{
    public static void Main()
    {
        // Create a ServiceHost
        using (ServiceHost serviceHost = new ServiceHost(typeof(RSVPService)))
        {
            // Open the ServiceHost to create listeners 
            // and start listening for messages.
            serviceHost.Open();

            // The service can now be accessed.
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

So I'm using this app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
  </startup>
  <system.serviceModel>      
    <services>
      <service name="Vert.Host.VertService.RSVPService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/Vert" />
          </baseAddresses>
        </host>
        <endpoint
          address="/RSVP"
          binding="basicHttpBinding"
          contract="Vert.Host.VertService.IRSVP" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

As I understand it, this setup would leave me with http://localhost:8080/Vert/RSVP/Attending as a valid REST URI to call from an arbitrary HTTPClient, but the call is hanging indefinitely or coming back with a 0 No Response (I'm using Advanced REST client)

What am I missing?

标签: c# wcf rest
1条回答
Melony?
2楼-- · 2019-08-07 21:51

You are RIGHT in all of your setup...right up to the point where you stopped typing code and started telling me what you have. :)

What you've created is a std WCF service and you can get to it using a Service proxy or a ChannelFactory, but it will communicate with you as-is using SOAP.

You need this tutorial to turn this webservice into a RESTFUL service giving back Json/pox.

查看更多
登录 后发表回答