2013-11-01 30 views
0

我在控制檯應用程序中擁有一個自託管的wcf服務。 一個簡單的自我託管服務是沒有問題的,有足夠的例子。Selfhosting WCF服務和basicHttpBinding:表示調用者的Windows標識不是通過綁定提供的

現在我想模擬wcf服務的調用者。雖然我跟着這個MSDN文章http://msdn.microsoft.com/en-us/library/ff648505.aspx我得到follwing錯誤:

The contract operation 'GetProduct' requires Windows identity for automatic impersonation. A Windows identity that represents the caller is not provided by binding

這是我在控制檯應用程序的WCF服務的App.config中:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="SelfHostingWCFService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 
    <applicationSettings> 
    <SelfHostingWCFService.Properties.Settings> 
     <setting name="URL" serializeAs="String"> 
     <value>http://localhost:8081/ProductService</value> 
     </setting> 
    </SelfHostingWCFService.Properties.Settings> 
    </applicationSettings> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ProductServiceBehavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
      <serviceAuthorization impersonateCallerForAllOperations="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <services> 
     <service name="SelfHostingWCFService.ProductService" 
       behaviorConfiguration="ProductServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8081/ProductService"/> 
      </baseAddresses> 
     </host> 
     <endpoint address="soap" binding="basicHttpBinding" contract="SelfHostingWCFService.IProductService"/> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    </system.serviceModel> 
</configuration> 

這裏IST的服務代碼:

public class ProductService : IProductService 
{ 
    [OperationBehavior(Impersonation = ImpersonationOption.Required)] 
    public Product GetProduct(int productId) 
    { 
     Product prod = new Product(); 
     prod.ID = productId; 
     prod.Name = productId.ToString() + " XDS"; 

     return prod; 
    } 

    [OperationBehavior(Impersonation = ImpersonationOption.Required)] 
    public string User() 
    { 
     string Name = string.Empty; 

     if (OperationContext.Current.ServiceSecurityContext != null) 
      Name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name; 

     if (ServiceSecurityContext.Current != null) 
      Name = ";" + ServiceSecurityContext.Current.WindowsIdentity.Name; 

     Name = ";" + Thread.CurrentPrincipal.Identity.Name; 

     return Name; 
    } 
} 

這是客戶端代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     ProductService.ProductService srv = new ProductService.ProductService(); 

     //srv.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     ProductService.Product prod = srv.GetProduct(1, true); 
     label1.Text = prod.Name; 
     label2.Text = srv.User(); 

    } 

我做錯了什麼? 請讓我知道。

我可以使用basicHTTPBinding還是必須使用wsHTTPBinding?

許多感謝您的幫助

回答

0

我沒有看到你的客戶端配置 - 您可能需要設置您的客戶端綁定Security.Transport.ClientCredentialType

在這裏看到:http://msdn.microsoft.com/en-us/library/ms729700(v=vs.110).aspx

+0

如果我我將WCF服務添加爲Service Reference而不是Web Reference(如上所述),app.config會自動生成,並且調用可以正常工作!非常感謝。在你的幫助下,我發現間接的解決方案。 – johnsheridan

相關問題