2013-05-15 25 views
15

Ở đây tôi đang cố gắng để đọc địa chỉ điểm cuối dịch vụ của tôi theo tên từ web.configđọc WCF dịch vụ thiết bị đầu cuối địa chỉ theo tên từ web.config

ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client"); 
var el = clientSection.Endpoints("SecService"); // I don't want to use index here as more endpoints may get added and its order may change 
string addr = el.Address.ToString(); 

Có cách nào tôi có thể đọc địa chỉ điểm cuối dựa trên tên ?

Đây là web.config tập tin của tôi

<system.serviceModel> 
<client> 
    <endpoint address="https://....................../FirstService.svc" binding="wsHttpBinding" bindingConfiguration="1ServiceBinding" contract="abc.firstContractName" behaviorConfiguration="FirstServiceBehavior" name="FirstService" /> 
    <endpoint address="https://....................../SecService.svc" binding="wsHttpBinding" bindingConfiguration="2ServiceBinding" contract="abc.secContractName" behaviorConfiguration="SecServiceBehavior" name="SecService" /> 
    <endpoint address="https://....................../ThirdService.svc" binding="wsHttpBinding" bindingConfiguration="3ServiceBinding" contract="abc.3rdContractName" behaviorConfiguration="ThirdServiceBehavior" name="ThirdService" /> 
      </client> 
    </system.serviceModel> 

này sẽ làm việc clientSection.Endpoints[0];, nhưng tôi đang tìm kiếm một cách để lấy theo tên.

I.e. một cái gì đó như clientSection.Endpoints["SecService"], nhưng nó không hoạt động.

+0

Và câu hỏi của bạn là ....? Bạn có gặp lỗi không? Ko có kết quả? – Tim

+0

Bạn chỉ nhận được nó bằng cách sử dụng ConfigurationManager? – Kiquenet

Trả lời

13

Tôi đoán bạn phải thực sự lặp thông qua các thiết bị đầu cuối:

string address; 
for (int i = 0; i < clientSection.Endpoints.Count; i++) 
{ 
    if (clientSection.Endpoints[i].Name == "SecService") 
     address = clientSection.Endpoints[i].Address.ToString(); 
} 

Bạn có thể lấy điểm cuối bởi then chốt. Sử dụng dấu ngoặc vuông có tên.

+0

Giải pháp tốt. Tôi chỉ sử dụng nó. Cảm ơn –

4

Vâng, mỗi thiết bị đầu cuối client-side có tên - chỉ cần nhanh chóng proxy client của bạn sử dụng rằng tên:

ThirdServiceClient client = new ThirdServiceClient("ThirdService"); 

Việc làm này sẽ đọc các thông tin chính xác từ các tập tin cấu hình tự động.

+1

Có thể Op không có proxy của khách hàng? – McGarnagle

+0

Nếu bạn không muốn sử dụng lớp ThirdServiceClient? Chỉ sử dụng ConfigurationManager? – Kiquenet

10

Đây là cách tôi đã làm nó sử dụng LINQ và C# 6.

tiên nhận được phần khách hàng:

var client = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; 

Sau đó lấy điểm cuối đó là bằng endpointName:

var qasEndpoint = client.Endpoints.Cast<ChannelEndpointElement>() 
    .SingleOrDefault(endpoint => endpoint.Name == endpointName); 

Sau đó, lấy url từ điểm cuối:

var endpointUrl = qasEndpoint?.Address.AbsoluteUri; 

Bạn cũng có thể lấy tên điểm cuối từ giao diện điểm cuối bằng cách sử dụng:

var endpointName = typeof (EndpointInterface).ToString(); 
+0

Cảm ơn người đàn ông, ít mã !!!, để có được 'EndPoint' theo tên (không phải là chỉ số chính) mã thứ hai của bạn là một giải pháp tốt. – Aria

+0

truyền sử dụng 'as' thay vì ngăn chặn ngoại lệ. tốt đẹp. – GibralterTop

Các vấn đề liên quan