1

WCF サービスを使用するプロジェクトに取り組んでいます。サービスを構築し、web.config ファイルを構成し、IIS 7 サーバーに展開しました。サービスは HTTPS 経由でアクセスされます (私の開発マシンでは、証明書を自己作成しました)。Visual Studio 2010 で ServiceReference を作成すると、すべて問題なく、クライアントが作成され、正常に動作します。

必要なのは、クライアントをプログラムで作成することです (少し柔軟性が必要です)。そのため、「手動で」接続しようとすると、次のようなエラーが表示されます。

指定された URI スキーム「https」は無効です。「http」が必要です。パラメータ名:経由

web.config のコードは次のとおりです (問題がないことを願っています)。

<system.serviceModel>    
    <services>           
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="WcfService1.IService1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <serviceMetadata httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>


    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

  </system.serviceModel>

WCF サービスにアクセスするために私が書いた手順は次のとおりです。

 void proc()
 {
        string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";
        WSHttpBinding bind= new WSHttpBinding();

        EndpointAddress ea = new EndpointAddress(ADRESASSL);
        var myChannelFactory = new ChannelFactory<IService1>(bind, ea);

        IService1 client = null;
        try
        {
            client = myChannelFactory.CreateChannel();
            client.RunMethod1();
            client.Close();                
            //((ICommunicationObject)client).Close();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            if (client != null)
                client.Close();
        }
    }

IService1 のコード

[ServiceContract]
public interface IService1 : IClientChannel
{
    [OperationContract]
    int RunMethod1();

 //....................................
}

私はここで何か間違ったことをしているようです.プロシージャは私が言及した例外を発生させます. 仕事のためにもっとや​​らなければならないことがありますが、私はそれを理解していませんでした。

あなたが私に与えることができるアドバイスを前もって感謝します。

4

2 に答える 2

3

これはテストしていませんが、ファクトリを作成する前に、バインディングのセキュリティモードを設定する必要があると思います。のセキュリティのデフォルトモードWSHttpBindingSecurityMode.Message、であり、必要ですSecurityMode.Transport

これは、次の3つの方法のいずれかで解決できます。

まず、次のように、オーバーロードされたバージョンのWSHttpBindingコンストラクターを使用してセキュリティモードを指定できます。

WSHttpBinding bind= new WSHttpBinding(SecurityMode.Transport);
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

次に、パラメーターなしのコンストラクターを使用して、次のようにセキュリティモード(およびクライアントクレデンシャルタイプ)を指定できます。

WSHttpBinding bind= new WSHttpBinding();
bind.Security.Mode = SecurityMode.Transport;
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

第3に、次のように、binding構成セクションをクライアント構成に配置し、そのセクションをコンストラクターで参照できます。

WSHttpBinding bind = new WSHttpBinding("TransportSecurity");

wsHttpBinding3番目の例では、クライアント構成ファイルに「TransportSecurity」という名前のセクションがあると想定しています。

詳細については、次のMSDN記事を確認してください。

方法:セキュリティモードを設定する

WSHttpBindingコンストラクタ

于 2013-02-18T06:23:29.480 に答える
1

さて、自己作成証明書の問題を解決しました。プログラムによる接続と Viosual Studio 2010 のサービス参照の両方のエンドポイント アドレスを変更しました。

string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";

今は

string ADRESASSL = "https://eu-pc/ServiciuSSLwsBind/Service1.svc";

アドレスを localhost から PC の名前「eu-pc」に変更しました。証明書が発行されたドメインに関係しています。localhost または 127.0.0.1 を使用すると、いずれかの方法でしか機能しませんでした。

これが、これに遭遇する可能性のある他の人に役立つことを願っています.

于 2013-02-18T20:43:05.007 に答える