2014-03-31 74 views
2

我已經創建了wcf服務,但是當我運行時,它顯示這樣的錯誤。無法運行我的wcf服務

enter image description here 我已經使用重命名工具更改名稱類和接口名稱。 下面是service.cs類代碼

public class MyTest : MyServices 
{ 
    public string MyTask1(string a) 
    { 
     return "Hello " + a; 
    } 
    public string MyTask2(DataContract1 dc) 
    { 
     return "Hello " + dc.fname; 
    } 
} 

這裏是我的界面代碼:

[ServiceContract] 
public interface MyServices 
{ 

    [OperationContract] 
    string MyTask1(string myValue); 
    [OperationContract] 
    string MyTask2(DataContract1 dcValue); 

} 

// Use a data contract as illustrated in the sample below to add composite types to service operations. 
[DataContract] 
public class DataContract1 
{ 
    string firstName; 
    string lastName; 
    [DataMember] 
    public string fname 
    { 
     get { return firstName; } 
     set { firstName = value; } 
    } 
    public string lname 
    { 
     get { return lastName; } 
     set { lastName = value; } 
    } 
} 

我已經編輯我的web.config文件,並添加這些行(我在黑閱讀書)

<service name="Service" behaviorConfiguration="ServiceBehavior"> 
     <!--Service EndPoints--> 
     <endpoint address="" binding="wsHttpBinding" contract="MyServices"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/> 
     </service> 

這裏是我的system.servicemodel代碼

<system.serviceModel> 
    <services> 
     <!-- My Custimization --> 
     <service name="Service" behaviorConfiguration="ServiceBehavior"> 
     <!--Service EndPoints--> 
     <endpoint address="" binding="wsHttpBinding" contract="MyServices"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
+0

我不明白這一行,它是爲什麼寫的

+1

這是獲取元數據或服務描述的綁定。如果禁用,則無法獲取wsdl。 –

+0

你可以顯示你的'ServiceBehavior'配置嗎?或者最好是整個''部分? – Szymon

回答

0

拼寫錯誤我想..

IMetadatExchange -- > IMetadataExchange 

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/> 

試試這個...

<system.serviceModel> 
    <services> 
     <!-- My Custimization --> 
     <service name="Service" behaviorConfiguration="ServiceBehavior"> 
     <!--Service EndPoints--> 
     <endpoint address="" binding="wsHttpBinding" contract="MyServices"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
      <endpoint address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
+0

沒關係。但你能告訴我,爲了什麼目的,該行的代碼是寫,(我的意思是什麼是mex)

1

1)在傳統的Web服務,我們使用WSDL創建代理。 WSDL公開Web服務元數據。在wcf另一個術語來說,mex端點也暴露元數據,但wsdl仍然活在wcf.i是新的wcf,因此我混淆了什麼是mex端點之間的差異?

這是一樣的事情,但mex旨在支持非HTTP協議和高級配置/安全方案。 WSDL是傳統方式,MEX是WCF的新改進版本。

2)是什麼意思httpGetEnabled =「假」或httpGetEnabled =「真」

它將通過defautl網址通過WSDL公開的元數據,即使你沒有爲定義的MEX終結你的服務。

3)如果我設置httpGetEnabled =「false」,那麼會發生什麼?這是否意味着客戶端將無法從IDE添加服務引用?但我設置httpGetEnabled =「false」,並看到客戶端可以添加服務引用。所以對於我來說,httpGetEnabled是錯誤的還是真的會讓人感到困惑?

只有啓用了httpGetEnabled/httpsGetEnabled或者如果您在服務配置中定義了mex端點,客戶端纔可以在VS中添加引用。最佳實踐是在開發環境中公開元數據,但不在生產環境中公開元數據。您還可以通過單獨的程序集分發您的服務合同並使用ChannelFactory。 4)有一個人說:MEX和WSDL是告訴潛在客戶你服務結構的兩種不同方案。因此,您可以選擇將服務合約公開爲(MEX)或WSDL。如果上述說法屬實,那麼告訴我何時使用MEX &何時使用WSDL?

WSDL通常通過http或https獲取您無法真正配置的URL(例如出於安全限制或向後兼容性)。 MEX端點通過可配置的端點公開元數據,並可以使用不同類型的傳輸(如TCP或HTTP)以及不同類型的安全機制。

因此MEX更具可配置性,而WSDL更能與舊版本的客戶端和使用WSDL的非.NET客戶端互操作。

5)我怎麼能禁用MEX僅透過WSDL暴露我的服務

不要specifiy一個MEX終結在你的配置和使用httpGetEnabled

6)WSDL支持所有bidning像wshttp,wsdualhttp或TCP etc ...

公開元數據與調用服務完全不同。

UPDATE

重新嘗試意味着應該有配置沒有MEX終結點相關的條目,並httpgetenable看起來就像

是的,你沒有指定一個MEX端點httpGetEnabled。只需要一個公開元數據。請勿指定httpGetUrl,因爲這取決於您的託管環境。

你說mex是可配置的,但wsdl不是。你試圖表示mex是可配置的...請討論什麼樣的配置mex支持&如何配置。

MEX端點是特殊的端點,它允許客戶端通過使用SOAP消息而不是http請求來接收服務的元數據。您可以創建可通過http,https,tcp甚至命名管道訪問的MEX端點。 HttpGetEnable允許您通過HTTP GET方法公開元數據,通常是後綴爲'?wsdl'的服務地址

MEX和WSDL都輸出幾乎相同的東西。

在大多數情況下,不需要MEX端點 - 使用帶有http get的WSDL通常就足夠了。

我理解你打算理解這個部分,但是不要花太多時間在這方面:還有很多其他複雜的功能!

+0

http://stackoverflow.com/a/21522849/391994 – foldinglettuce