我正在嘗試關注博客文章Using a REST WCF Service。WCF暴露爲RESTful
但我不知道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
文件:
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.
我tryed評論的那部分在我的配置文件,但隨後它不會運行。這可以解釋爲什麼我無法連接到我的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);
}
}
global.asax也許? A for App,C for Control – 2012-03-27 20:05:55
和8000!= 29174,請問你的郵遞員。 – 2012-03-27 20:07:29
是Global.asax不知道你的第二個評論意味着什麼。 – 2012-03-27 20:18:22