将WCF公开为RESTful
本文关键字:RESTful WCF | 更新日期: 2023-09-27 18:29:15
我正在努力关注博客文章使用RESTWCF服务。
但我不知道Global.asax页面在哪里。从博客中添加这一点重要吗?
此外,当我尝试使用以下代码从Windows窗体应用程序连接时:
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.AspNetCompatibility RequirementsMode属性设置为Required以外的值。
我试着在配置文件中注释该部分,但它无法运行。这可能解释了我无法连接到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);
}
}
您所看到的示例假设您正在制作一个托管在IIS中的ASP.NET web应用程序,该应用程序将有一个Global.asax
文件。
由于您将它托管在控制台应用程序中,因此您没有这个文件,如果您将其放在那里,它也不会有任何作用。
此外,在您的配置中,您有:
<endpoint address="http://localhost:29174/Service1.svc"
但在你的代码中,
"http://localhost:8000/Hello"
这是非常不同的。
控制台应用程序启动时收到的错误状态:
此服务需要ASP.NET兼容性,并且必须托管在IIS中。在web.config中启用ASP.NET兼容性的IIS中承载该服务,或者将AspNetCompatibilityRequirementsAttribute.AspNetCompatibility RequirementsMode属性设置为除Required之外的值。
但是您的代码将该属性设置为Required。控制台应用程序无法提供ASP.NET兼容模式,只有像IIS、Cassini或IIS Express这样的真实web服务器才能提供。因此,当它设置为必需时,会出现错误。您可以尝试将其设置为不同的值,而不是Required,如错误消息所示。但是,如果没有ASP.NET兼容性模式,某些其他功能可能无法正常工作。
最好的做法是将您的工作作为ASP.NET Web应用程序重新进行,除非您实际上计划制作一个必须作为控制台应用程序或Windows服务运行的真实项目。
至于您的错误,可能是您的配置造成的。它似乎指向localhost:29174
,但您的错误来自localhost:8000
。我不确定这是否是你的问题。但是,你可能也想在关闭防火墙的情况下尝试一下,看看会发生什么,因为可能是你的防火墙阻止了呼叫。