ASP.NET Webサービスの設定
WebアプリケーションでWebサービスをリモートから呼び出す場合、web.configファイルを適切に設定する必要があります。以下に代表的な3つの設定問題を説明します。
1. プロトコルの有効化
ソリューション内のweb.configファイルを見つけ、以下の内容を追加します:
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
2. エンドポイント設定の重複エラー
「ServiceReference1.XXX」のエンドポイント設定を読み込めません」というエラーが発生する場合、いくつかのエンドポイント設定が競合しています。この問題は、web.config内で同じコントラクトに対する複数のエンドポイント定義があるときに発生します。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyServiceSoap" />
<binding name="MyServiceSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.1.100/MyService.asmx" binding="basicHttpBinding"
bindingConfiguration="MyServiceSoap" contract="ServiceReference.MyServiceSoap"
name="MyServiceSoap" />
<endpoint address="http://192.168.1.100/MyService.asmx" binding="basicHttpBinding"
bindingConfiguration="MyServiceSoap1" contract="ServiceReference.MyServiceSoap"
name="MyServiceSoap1" />
</client>
</system.serviceModel>
解決策:重複する方のエンドポイント設定を削除します。MyServiceSoapを使用する場合は、MyServiceSoap1的相关节点を削除してください。
または、コード内で特定のバインディング設定を指定することもできます:
ServiceReference.MyServiceSoap client = new MyServiceSoapClient("MyServiceSoap");
3. メッセージサイズクォータの拡張
Webサービスからデータを取得する際に、「受信メッセージ(65536)の最大サイズを超えました」というエラーが発生する場合、バインディングのMaxReceivedMessageSizeプロパティを調整する必要があります。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
</basicHttpBinding>
<customBinding>
<binding name="DataServiceSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://example.com/DataService.asmx"
binding="basicHttpBinding" bindingConfiguration="DataServiceSoap"
contract="ServiceReference.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>
これらの設定により、大容量のデータを返すWebサービスでも正常に通信できるようになります。