2011-04-07 2 views

답변

2

한 가지 방법은 try입니다. 구성 파일에서 첫 번째로드를 시도하고 catch에 끝점을 하드 코딩합니다. EG는 :

MyServiceClient client = null; 
try 
{ 
    client = new MyServiceClient(); 
} 
catch (InvalidOperationException) 
{ 
    EndpointAddress defaultAddress = new EndpointAddress(...); 
    Binding defaultBinding = new Binding(...); 
    client = new MyServiceClient(defaultBinding, defaultAddress); 
} 
+0

고맙지 만 서버 측 코드를 찾고있었습니다. 클라이언트에서도 똑같이 할 수 있습니다. – Qwertie

+0

자체 호스팅 서비스 (예 : ServiceHost를 사용하는 경우)의 경우 동일한 기술이 작동합니다. –

2

당신은 아래의 구성 섹션을 얻을 수 있습니다 :

var clientSection = System.Configuration.ConfigurationManager.GetSection("system.serviceModel/client"); 

값이 null 또는 clientSection.Endpoints 다음 제로 요소가 포함되어있는 경우가 정의되어 있지 않은 경우.

1

시도해보세요. 테스트되지 않았지만 잘 작동 할 것입니다. 일치하는 계약이있는 엔드 포인트의 구성을 점검합니다. 이름으로 일치 시키거나, 다른 정보를 반환하거나, 상황에 맞는 것이 무엇이든 변경할 수 있습니다. 일치하는 항목이 없으면 논리를 넣어 기본 끝점을 만들 수 있습니다.

public List<EndpointAddress> GetEndpointAddresses(Type t) 
    { 
     string contractName = t.FullName; 
     List<EndpointAddress> endpointAddresses = new List<EndpointAddress>(); 
     ServicesSection servicesSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection; 

     foreach (ServiceElement service in servicesSection.Services) 
     { 
      foreach (ServiceEndpointElement endpoint in service.Endpoints) 
      { 
       if (string.Compare(endpoint.Contract, contractName) == 0) 
       { 
        endpointAddresses.Add(new EndpointAddress(endpoint.Address)); 
       } 
      } 
     } 

     if (endpointAddresses.Count == 0) 
     { 
      //TODO: Add logic to determine default 
      endpointAddresses.Add(new EndpointAddress("Your default here")); 
     } 

     return endpointAddresses; 
    } 
2

app.config 파일없이 독립 실행 형 서비스 클라이언트를 구현하려는 경우 동일한 문제가 발생했습니다. 그리고 마침내 나는 그것을 소트 할 수 있었다. 아래 코드 샘플을 따르십시오. 그것은 잘 작동하고 나는 그것을 테스트했습니다.

BasicHttpBinding binding = new BasicHttpBinding(); 
binding.Name = "BasicHttpBinding_ITaskService"; 
binding.CloseTimeout = TimeSpan.FromMinutes(1); 
binding.OpenTimeout = TimeSpan.FromMinutes(1); 
binding.ReceiveTimeout = TimeSpan.FromMinutes(10); 
binding.SendTimeout = TimeSpan.FromMinutes(1); 
binding.AllowCookies = false; 
binding.BypassProxyOnLocal = false; 
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
binding.MaxBufferSize = 65536; 
binding.MaxBufferPoolSize = 524288; 
binding.MaxReceivedMessageSize = 65536; 
binding.MessageEncoding = WSMessageEncoding.Text; 
binding.TextEncoding = System.Text.Encoding.UTF8; 
binding.TransferMode = TransferMode.Buffered; 
binding.UseDefaultWebProxy = true; 
binding.Security.Mode = BasicHttpSecurityMode.None; 
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None; 
binding.Security.Transport.Realm = ""; 
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; 
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default; 

Uri endPointAddress = new Uri("http://www.kapanbipan.com/TaskService.svc"); 
ChannelFactory<taskmgr.TaskService.ITaskServiceChannel> factory = new ChannelFactory<ITaskServiceChannel>(binding, endPointAddress.ToString()); 
taskmgr.TaskService.ITaskServiceChannel client = factory.CreateChannel();