WCF:たいのですがprogramatically再現されます。configます。
-
08-07-2019 - |
質問
私は、WCFサービスがokの場合のようなサービスを特定しない場合には、拘束力を有したエンドポイントを読んでから、生成された値にアプリを開発する予定だ。config時の登録を行ったのは、WCFよVisual Studio).
私は簡単な方法を返しますサービス参考:
return new SmsServiceReference.SmsEngineServiceClient();
この作品はokでの値から読み取られたconfig).しかし、そういったいこれらの値のデータベースのURI例えば)というようになります:
Binding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );
return new SmsServiceReference.SmsEngineServiceClient(binding,endpointAddress);
この方法でやらねばならない。でに例外をスローしようとサービスの利用を参考にした。
このが私のアプリです。configりのラインアップされていないの提供(も).それでは、どうしたいので複製、以下のアプリです。Config値programatically?
この断片のアプリです。Config:(URIにて変更を保護するinocent).
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISmsEngineService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.myuri.com/Services/Services.svc/basic"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISmsEngineService"
contract="SmsServiceReference.ISmsEngineService" name="BasicHttpBinding_ISmsEngineService" />
</client>
解決
App configの値のほとんどもバインディングのプロパティであり、プログラムで再作成できます。個人的には、以下のようなメソッドを使用してバインディングを作成します
public static BasicHttpBinding CreateBasicHttpBinding()
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.AllowCookies = false;
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.OpenTimeout = new TimeSpan(0, 1, 0);
binding.SendTimeout = new TimeSpan(0, 1, 0);
// add more based on config file ...
//buffer size
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 534288;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
//quotas
binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 8192;
// add more based on config file ...
return binding;
}
そして、エンドポイントアドレスの作成にこのようなものを使用します
public static EndpointAddress CreateEndPoint()
{
return new EndpointAddress(Configuration.GetServiceUri());
}
serviceUriは、 http://www.myuri.comなどのサービスURLです。 /Services/Services.svc/basic
最後にサービスクライアントを作成します
Binding httpBinding = CreateBasicHttpBinding();
EndpointAddress address = CreateEndPoint();
var serviceClient = new MyServiceClient(httpBinding, address);
他のヒント
でも、クライアントのエンドポイントにコンフィグを指定するこURL:
<endpoint address="http://www.myuri.com/Services/Services.svc/basic"
なおサンプルコードを作成します:
EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );
のアドレスが一致した場合、一つのコンフィグを作る必要がありますの変更コード:
EndpointAddress endpointAddress = new EndpointAddress( "http://www.myuri.com/Services/Services.svc/basic" );
されているということをより多少の誤字コードサンプル(my.uri.com 対www.myuri.com,/サービスです。svcの代わりに/サービス/サービスsvc).
な仕組みになっているのを修正endpoint address?
Marc