2012-03-27 190 views
0

我正在嘗試關注博客文章Using a REST WCF ServiceWCF暴露爲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); 
    } 
} 
+0

global.asax也許? A for App,C for Control – 2012-03-27 20:05:55

+2

和8000!= 29174,請問你的郵遞員。 – 2012-03-27 20:07:29

+0

是Global.asax不知道你的第二個評論意味着什麼。 – 2012-03-27 20:18:22

回答

2

您正在查看的示例假設您正在製作一個ASP.NET Web應用程序,該應用程序在IIS中託管,該應用程序將具有Global.asax文件。

由於您將其託管在控制檯應用程序中,因此您沒有該文件,如果您將其放在該文件中,它也不會執行任何操作。


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

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

,但在你的代碼,

"http://localhost:8000/Hello" 

這是非常不同的。


您收到啓動您的控制檯應用程序狀態的錯誤:

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.

但你的代碼有屬性設置爲要求。控制檯應用程序無法提供ASP.NET兼容模式,只有像IIS或CassiniIIS Express這樣的真實Web服務器纔可以提供該功能。因此,如果設置爲,則會出錯。必需。根據錯誤消息的建議,您可以嘗試將其設置爲不同的值,而不是必需的。但是,沒有ASP.NET兼容模式,某些其他事情可能無法正常工作。


的最好的事情將在這裏重新做你的工作作爲一個ASP.NET Web應用程序,除非你做出一個必須運行作爲控制檯應用程序或現實生活中的項目實際上計劃Windows service

2

在Global.asax頁是在.NET網站上找到的頁面。它的行爲類似於網頁的引導程序。當網頁第一次啓動時,它會執行所有啓動操作(在這種情況下,將服務URL作爲路由連接,但我不確定具體情況,因爲我從未親自使用該路由)。

至於你的錯誤,它可能來自你的配置。它似乎指向localhost:29174,但您的錯誤來自localhost:8000。我不確定這是否是您的問題。但是,您可能還想在防火牆關閉的情況下嘗試一下,看看會發生什麼,因爲防火牆可能會阻止呼叫。

+0

抱歉在我粘貼的代碼中滾動,我將localhost設置爲。該服務託管於29174 – 2012-03-27 20:07:31

+0

@Garrith Ahh,如果您手動訪問URL,它是否工作? – 2012-03-27 20:11:40

+0

給我一秒我需要更新這個問題,看看我上次從一個控制檯應用程序託管,但它本身給出了一個錯誤!所以我只是沒有它就跑了wcfservice。 – 2012-03-27 20:13:06