2010-03-23 3 views
3

HTTP를 통해 동일한 WCF 서비스를 모두 실행하는 원격 시스템이 있습니다. 나는 런타임에 이들 중 어느 것을 연결할지를 결정해야하는 중앙 구성 유틸리티를 가지고있다. 모든 데이터베이스 구동이므로 구성 파일의 모든 끝점을 정의하고 싶지 않습니다.런타임시 WCF 끝점의 IP 주소를 지정하십시오.

나는 순진하게도이 시도 :

CustomerServiceClient 내 서비스 참조 프록시 클래스를하지만, (당연히) 그것이 나에게 다음과 같은 오류 준
CustomerServiceClient GetClientForIPAddress(string ipAddress) 
{ 
string address = String.Format("http://{0}/customerservice.svc", ipAddress); 
var client = new CustomerServiceClient("?", address); 
return client; 
} 

: 그래서

Could not find endpoint element with name '?' and contract 'SkyWalkerCustomerService.ICustomerService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

을 어떻게 엔드 포인트를 선언 할 런타임에 내 서비스 참조를 가리키고 있습니까? 이 난은 실버 라이트 응용 프로그램 내 엔드 포인트를 구성하는 데 사용하는 코드의 일부입니다 .NET 3.5

+0

나는 이것이 중복라고 생각합니다 http://stackoverflow.com/questions/1978962/how-to-change-endpoint-address-programatically- 인 - 더 - 클라이언트 사이트 –

답변

3

다음 엔드 포인트이 전달

private void initEndpoint(ServiceEndpoint endPoint, string serviceName) 
    { 
     Uri hostUri = Application.Current.Host.Source; 
     string vdir = hostUri.LocalPath.Substring(0, hostUri.LocalPath.IndexOf("/ClientBin", StringComparison.InvariantCultureIgnoreCase)); 
     string wcfBaseUri = string.Format("{0}://{1}:{2}{3}/WebServices/", hostUri.Scheme, hostUri.Host, hostUri.Port, vdir); 

     endPoint.Address = new EndpointAddress(new Uri(wcfBaseUri + serviceName)); 
    } 

구성 할 수있는 엔드 포인트이며, serviceNameMyLoggingService.svc과 같은 서비스의 이름입니다. 내가하는 모든 일은 새로운 주소 (이 경우 호스팅 웹 사이트 내의 알려진 위치)를 가리 키기 만하면됩니다. 이를 샘플로 사용하고 어디에서나 자신의 문자열 주소를 전달하십시오. 이 도움이

_loggingService = new LoggingServiceClient(); 
initEndpoint(_loggingService.Endpoint, "LoggingService.svc"); 

희망 :

는 그것은 다음과 같습니다 코드의 비트와 함께 호출된다. 그것을 가지고 그것을 함께 실행, 그것을 자르고 자신의 그것을 확인하십시오 :)

관련 문제