2009-05-07 6 views
10

Visual Studio를 통해 WCF를 등록 할 때 App.config에서 생성 된 값에서 바인딩 또는 끝점을 지정하지 않고 서비스를 만들면 정상적으로 작동하는 WCF 서비스가 있습니다. (값은 설정에서 읽기 때문에)WCF : 이러한 App.config 값을 프로그래밍 방식으로 다시 만들려면 어떻게해야합니까?

return new SmsServiceReference.SmsEngineServiceClient(); 

이 작품을 좋아 :

나는 서비스 참조를 반환하는 간단한 방법이있다. 그러나, 나는 데이터베이스 (예를 들어, URI)이 값의 일부를 가지고 싶습니다과 같은 것을 할 싶습니다 :이 작동하지 않습니다

 Binding binding = new BasicHttpBinding(); 
     EndpointAddress endpointAddress = new EndpointAddress("my.uri.com/service.svc"); 

     return new SmsServiceReference.SmsEngineServiceClient(binding,endpointAddress); 

. 서비스 참조를 사용하려고하면 예외가 throw됩니다.

내 App.config에 두 줄이 제공되지 않는다는 정보가 더 많기 때문에 의심 스럽습니다. 문제는 어떻게 App.Config 값을 프로그래밍 방식으로 복제 할 수 있습니까?

다음은 내 App.Config의 일부입니다 (URI가 inocent를 보호하기 위해 변경되었습니다).

<endpoint address="http://www.myuri.com/Services/Services.svc/basic" 

하지만 코드 샘플에서

, 당신은 만들 :

EndpointAddress endpointAddress = new EndpointAddress("my.uri.com/service.svc"); 

주소는 일치해야합니다

<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> 

답변

16

앱 설정에있는 값의 대부분은 또한 바인딩의 속성이며, 프로그램 다시 할 수 있습니다. 개인적으로, 나는 아래 하나가 결합

 

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는 서비스 URL 될 것 같은 http://www.myuri.com/Services/Services.svc/basic

마지막

 

Binding httpBinding = CreateBasicHttpBinding(); 
EndpointAddress address = CreateEndPoint(); 
var serviceClient = new MyServiceClient(httpBinding, address); 
 
+0

트릭을했습니다. 감사. –

1

음, 설정의 클라이언트 엔드 포인트는이 URL을 지정합니다 - 설정에있는 코드가 작동하면 코드를

로 변경해야합니다.
EndpointAddress endpointAddress = new EndpointAddress("http://www.myuri.com/Services/Services.svc/basic"); 

코드 샘플 (my.uri.com 대 www.myuri.com, /Services/Services.svc 대신 /service.svc)에 여러 가지 오타가 있습니다.

수정 된 엔드 포인트 주소로 작동합니까?

마크

+0

내가 구경에게 사과 서비스 클라이언트를 만드는 방법 r "작은 오타", 나는 단지 "단순하게"하려고 노력했다. 코드에서 (및 앱 구성에서) 엔드 포인트 주소 사용은 동일하지만 작동하지 않습니다. 한 가지 차이점이 있습니다. App.config는/기본적인 것을 가지고 있습니다. 그것은 URI의 일부가 아닙니다. 이유는 VSTUDIO가이를 추가했기 때문입니다. 해당 URL (/ basic 포함)을 탐색하면 작동하지 않습니다. 말할 것도없이, 나는 둘 다 시도했다.App.config에있는 "끝점"만들기에 뭔가 빠졌다고 가정합니다. 사실, 내 모든 매개 변수를 제공하지는 않지만 uri는 다른 APp.config 정보는 무엇입니까? –

+0

URL의 정확한 철자가 중요하므로 여기에서 '단순한'것은 혼란을 초래합니다. 서버 부분에 대한 설정 파일을 보여줄 수 있습니까? –

+0

안녕하세요 마크, 문제는 URI가 아니었지만, 클라이언트에서 web.config에있는 일부 매개 변수는 프로그래밍 방식으로 만들 때 바인딩에 전달하지 못했습니다. web.config (Emmanuel의 제안을 사용하여)와 똑같이 모든 것을 통과 시켰습니다. 당신의 도움을 주셔서 감사합니다! +1 당신 –

관련 문제