将WCF公开为RESTful

时间:2012-03-27 19:56:47

标签: c# wcf web-services rest

我正在尝试关注博客文章 Using a REST WCF Service

但我不知道Global.asax页面在哪里。从博客中添加该位是否很重要?

当我尝试使用以下代码从我的Windows Forms应用程序进行连接时:

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
            }
}

我收到此错误:

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.

我的其他项目中的Web.Config文件是主机,如下所示:

<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>

这是我禁用的主机,只是单独运行了wcfservice:

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();
        }
    }
}

当我尝试运行此操作时,我收到与博客和我的web.config文件相关的错误:

  

此服务需要ASP.NET兼容性,并且必须在IIS中托管。   在启用了ASP.NET兼容性的IIS中托管服务   web.config或设置   AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode   属性为必需值以外的值。

我尝试在我的配置文件中评论该部分,但之后它不会运行。这可以解释为什么我无法连接我的Windows窗体应用程序。

但请注意,在我的web.config文件和AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode下面的代码中,两者都已设置:

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

2 个答案:

答案 0 :(得分:2)

global.asax页面是.NET个网站上的页面。它有点像网页的引导程序。当网页首次旋转时,它会执行所有启动操作(在这种情况下,将服务URL作为路由连接,但我不确定具体是什么,因为我从未亲自使用过该路由)。

关于您的错误,可能来自您的配置。它似乎指向localhost:29174,但您的错误来自localhost:8000。我不确定这是不是你的问题。但是,您可能还想在关闭防火墙的情况下尝试它,看看会发生什么,因为它可能是您的防火墙阻止了呼叫。

答案 1 :(得分:2)

您正在考虑的示例假定您正在制作一个托管在IIS中的ASP.NET Web应用程序,该文档将具有Global.asax文件。

由于你是在一个控制台应用程序中托管它,你没有这个文件,如果把它放在那里它也不会做任何事情。


此外,在您的配置中,您有:

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

但是在你的代码中,

"http://localhost:8000/Hello"

这是非常不同的。


启动控制台应用程序时收到的错误说明:

  

此服务需要ASP.NET兼容性,并且必须在IIS中托管。在IIS中托管服务并在web.config中启用ASP.NET兼容性,或将AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode属性设置为值而不是

但您的代码已将该属性设置为必需。控制台应用程序无法提供ASP.NET兼容模式,只有像IIS或CassiniIIS Express这样的真实Web服务器才能提供。因此,当它设置为必需时会出现错误。您可以尝试将其设置为除必需之外的其他值,如错误消息所示。但是,如果没有ASP.NET兼容模式,某些其他内容可能无法正常工作。


最好的办法是在ASP.NET Web应用程序中重新开展工作,除非您真正计划制作必须作为控制台应用程序运行的真实项目或{{3} }。

相关问题