0

コンソール アプリケーションにセルフ ホスティング wcf サービスがあります。シンプルなセルフ ホスティング サービスは問題ありません。十分な例があります。

ここで、wcf サービスの呼び出し元になりすます必要があります。この msdn の記事 http://msdn.microsoft.com/en-us/library/ff648505.aspxに従いましたが、次のエラーが発生します。

コントラクト操作 'GetProduct' では、自動偽装のために Windows ID が必要です。呼び出し元を表す Windows ID がバインディングによって提供されない

CONSOLE アプリケーションの 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>

サービスコードは次のとおりです。

    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 を使用する必要がありますか?

助けてくれて本当にありがとうございます

4

1 に答える 1

0

クライアント側の構成が表示されません - クライアント バインディングの Security.Transport.ClientCredentialType を設定する必要がある場合があります

ここを参照してください: http://msdn.microsoft.com/en-us/library/ms729700(v=vs.110).aspx

于 2013-11-01T18:31:37.230 に答える