G Gr
G Gr

Reputation: 6080

Exposing WCF as RESTful

I'm trying to follow the blog post Using a REST WCF Service.

But I don't know where the Global.asax page is. Is it important to add that bit from the blog?

Also when I try to connect from my Windows Forms application with the below code:

private void button1_Click(object sender, EventArgs e)
{
    using (ChannelFactory<ServiceReference1.IService1> factory = new
        ChannelFactory<ServiceReference1.IService1>(
            new WebHttpBinding(),
            "http://localhost:8000/Hello"))
            {
                factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
                var helloService = factory.CreateChannel();
                label1.Text = helloService.GetData(Convert.ToString(textBox1.Text));
                // The above line
            }
}

I get this error:

Could not connect to http://localhost:8000/Hello/GetData. TCP error
code 10061: No connection could be made because the target machine
actively refused it 127.0.0.1:8000.

My Web.Config file from my other project which is the host looks like this:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="http://localhost:29174/Service1.svc" binding="webHttpBinding" contract="WcfService2.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WcfService2.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

This is my host which I disabled and just ran the wcfservice alone:

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            WebHttpBinding binding = new WebHttpBinding();
            WebServiceHost host =
            new WebServiceHost(typeof(Service1));
            host.AddServiceEndpoint(typeof(IService1),
            binding,
            "http://localhost:8000/Hello");

            host.Open(); // this line gives an error
            Console.WriteLine("Hello world service");
            Console.WriteLine("Press <RETURN> to end service");
            Console.ReadLine();
        }
    }
}

When I try to run this I get this error related to the blog and my web.config file:

This service requires ASP.NET compatibility and must be hosted in IIS. Either host the service in IIS with ASP.NET compatibility turned on in web.config or set the AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode property to a value other than Required.

I tryed commenting that part out in my configuration file but then it wouldn't run. This may explain why I can't connect with my Windows Forms application.

Note however in my web.config file and in the code below AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode both are set:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1 : IService1
{
    public string GetData(string value)
    {
        return string.Format("You entered: {0}", value);
    }
}

Upvotes: 0

Views: 1043

Answers (2)

Justin Pihony
Justin Pihony

Reputation: 67065

The global.asax page is a page that is found on .NET websites. It acts kind of like the bootstrapper of the webpage. When the webpage first spins up, it performs all of the startup actions (in this case wiring the service URL as a route, but I am not sure exactly on the specifics as I have never used that route personally).

As to your error, it might be from your configuration. It seems to be pointing to localhost:29174, yet your error is coming from localhost:8000. I am not sure if that is your issue or not. But, you might also want to try it with your firewall off and see what happens, as it could be your firewall blocking the call.

Upvotes: 2

CodingWithSpike
CodingWithSpike

Reputation: 43698

The example you are looking at assumes you are making an ASP.NET web application, hosted in IIS, which would have a Global.asax file.

Since you are hosting it in a console application, you don't have this file, nor would it do anything if you put one there.


Also, in your configuration you have:

<endpoint address="http://localhost:29174/Service1.svc"

but in your code,

"http://localhost:8000/Hello"

Which is very different.


The error you receive on startup of you console application states:

This service requires ASP.NET compatibility and must be hosted in IIS. Either host the service in IIS with ASP.NET compatibility turned on in web.config or set the AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode property to a value other than Required.

But your code has that property set to Required. A console application can not provide ASP.NET compatibility mode, only a real web server like IIS or Cassini or IIS Express can provide that. Hence you get an error when it is set to Required. You could try setting it to a different value, other than Required, as the error message suggests. However, certain other things might not work correctly without ASP.NET Compatibility mode.


The best thing to do would be to re-do your work here as an ASP.NET Web Application, unless you actually plan on making a real-life project that has to be run as a console application or Windows service.

Upvotes: 2

Related Questions