2011-03-04 184 views
3

我需要從DLL中使用WCF服務,因此我沒有任何配置文件來從中讀取綁定配置。消耗WCF沒有app.config

我真的很難得到它的工作。最後,作爲一個非常簡單的解決辦法,我一個引用添加到WCF和實例化它是這樣的:

 WSHttpBinding binding = new WSHttpBinding(); 
     EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc"); 

     ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address); 
     var result = client.Method1(); 

在本地主機這個簡單的工作。當從另一臺機器上嘗試它,我得到這個錯誤:

The request for security token could not be satisfied because authentication failed. 

在主機中,IIS設置爲「無名氏」,所以我想它應該只是工作。

任何幫助?

編輯:服務配置文件

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="Mai.MyPlanner.Service"> 
     <endpoint address="" binding="wsHttpBinding" contract="Mai.MyPlanner.IService"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://MY_SERVICE"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 

      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

<connectionStrings> 
    <!-- PROD --> 

    <!-- TEST --> 
</connectionStrings> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 
+0

顯示您的服務配置,以便我們可以看到需要什麼客戶端設置。很明顯,你正在使用一些安全服務(WsHttpBinding默認在消息級別保護)。 – 2011-03-04 09:34:10

回答

4

使用此代碼:

WSHttpBinding binding = new WSHttpBinding(); 
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("localhost"); 
EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc", identity); 

ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address); 
var result = client.Method1(); 

您仍然需要通過身份的dns價值和地址到你的方法調用此代碼。此外,這種類型的配置只能在Intranet(相同的Windows域)中工作,因爲它通過defult使用消息安全性和Windows身份驗證。

2

使用基本的HTTP綁定相反,如果你不需要安全。

+0

我不能使用基本綁定,因爲它使用SOAP 1.1,WCF使用SOAP 1.2,所以它會給我協議錯誤。我已經試過這種方式了。 – pistacchio 2011-03-04 09:39:26

+0

basichttpbinding是標準的wcf綁定,請參閱http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpbinding.aspx,但您必須配置服務器和客戶端以使用相同的綁定 – 2011-03-04 10:37:54