0

svcutil.exe を使用して Web サービスのプロキシ クラスを作成しました。以下のコードを使用してWebサービスに接続しようとしていました。以下の例外が発生しました

「アドレス: http://example.com:8123/blmrg/test_ws/Service1.svc 」という名前のエンドポイント要素が見つかりませんでした。およびコントラクト 'IService1' を ServiceModel クライアント構成セクションで". これは、アプリケーションの構成ファイルが見つからなかったことが原因である可能性があります。

ブラウザでその Web サービスの URL にアクセスしようとすると、正常に動作します。以下のコードで何が間違っていたのか教えてください。

私のコード:

ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElement endpoint = clientSection.Endpoints[0];
string endpointStr = string.Format("Address: {0}; Binding: {1}; Contract: {2}", endpoint.Address.ToString(), endpoint.Binding, endpoint.Contract);
Service1Client proxy = new Service1Client(endpointStr); // Getting exception here
CitizenType citizen = proxy.GetMan(562054);

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>  
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://example.com:8123'/blmrg/test_ws/Service1.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
      contract="IService1" name="BasicHttpBinding_IService1" />
</client>
 </system.serviceModel>
</configuration>

プロキシ クラス:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1
{

    public Service1Client()
    {
    }

    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {

    }

    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

    public CitizenType GetMan(decimal id)
    {
        return base.Channel.GetMan(id);
    }

    public System.Threading.Tasks.Task<CitizenType> GetManAsync(decimal id)
    {
        return base.Channel.GetManAsync(id);
    }
}
4

1 に答える 1

1

Service1Client使用している (またはコンパイラが使用していると見なしている)オーバーロードは、エンドポイント構成名を取るものです。

public Service1Client(string endpointConfigurationName) : 
    base(endpointConfigurationName)

しかし、あなたが渡しているのはAddress:http://132.158.6.6:8123/blmrg/test_ws/Service1.svc; and contract 'IService1'、どうやらこの行のためです(ただし、バインディングがなく、「and」という単語が含まれていません):

string endpointStr = string.Format("Address: {0}; Binding: {1}; Contract: {2}", endpoint.Address.ToString(), endpoint.Binding, endpoint.Contract);

(投稿されたコードに基づいて)ConfigurationManager構成ファイルのクライアント セクションを取得するために使用する必要はありません。次のように、コンストラクターでエンドポイント セクションの名前を渡すことができます。

Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");

上記の行は、「BasicHttpBinding_IService1」という名前のエンドポイントから必要な情報を取得します。

コンストラクターには他にもいくつかのオーバーロードがあることに注意してください。

public Service1Client(string endpointConfigurationName, string remoteAddress) : 
    base(endpointConfigurationName, remoteAddress)

これは、エンドポイント構成名とサービスのアドレスを取ります (通常はエンドポイント要素にありますが、環境などに基づいてオーバーライドする必要がある場合があります)。

public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
    base(endpointConfigurationName, remoteAddress)

前のものと似ていますが、 ではなく を渡しEndpointAddressますstring

public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
    base(binding, remoteAddress)

これはBindingとを渡しEndpointAddressます。

于 2014-12-22T06:13:28.017 に答える