2012-04-19 26 views
7

Tôi có tiện ích mở rộng hành vi WCF mà tôi muốn thêm vào máy khách WCF. Tuy nhiên, khách hàng được xây dựng theo chương trình. Địa chỉ điểm cuối có thể thay đổi, nhưng tôi biết loại. Tôi có thể thêm các hành vi lập trình hoặc trong tập tin cấu hình (ưa thích), nhưng tôi cần phải vượt qua một số cấu hình trong tập tin cấu hình chỉ.Tuyên bố cấu hình tiện ích mở rộng hành vi WCF trên điểm cuối được lập trình xây dựng

Tôi không muốn điều này trong các hành vi phổ biến (machine.config).

tôi có thể thêm các hành vi lập trình

endpoint.Behaviors.Add(new MyCustomBehavior()) 

Nhưng tôi thà làm điều đó trong cấu hình, vì vậy tôi có thể cấu hình các phần mở rộng đó là tốt. Có thể tuyên bố thêm và cấu hình tiện ích mở rộng hành vi điểm cuối đến điểm cuối được lập trình được xây dựng chỉ biết loại hoặc giao diện trong khi rời khỏi điểm cuối ứng dụng khách được lập trình không? Không.

<system.serviceModel> 
    <client> 
    <!-- Created programmatically --> 
    </client> 
<extensions> 
    <behaviorExtensions> 
    <add name="MyCustomBehavior" type="namespace.CustomBehaviors", MyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
    </behaviorExtensions> 
</extensions> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="MyCustomBehavior"> 
     <MyCustomBehavior MyImportantBehaviorParam1="foo" /> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 

Tất nhiên tôi có thể đặt cấu hình ở phần khác và có hành vi đọc ở đó, nhưng tôi muốn sử dụng tiện nghi WCF nếu có thể.

Trả lời

10

Để thực hiện điều đó, bạn cần tạo tiện ích mở rộng cấu hình hành vi cho điểm cuối của mình. Để biết thêm thông tin về cách thực hiện điều đó, hãy kiểm tra http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/28/wcf-extensibility-behavior-configuration-extensions.aspx.

Cập nhật: Tôi thấy sự cố của bạn ngay bây giờ. Không có cách nào trực tiếp để thêm vào điểm cuối được tạo thông qua mã một hành vi được khai báo trong cấu hình. Tuy nhiên, bạn vẫn có thể sử dụng một số phản ánh để truy cập phương thức CreateBehavior của phần mở rộng cấu hình hành vi (phương thức được bảo vệ) để thực sự tạo ra hành vi điểm cuối để thêm nó vào điểm cuối được tạo thông qua mã. Mã dưới đây cho thấy cách này có thể được thực hiện.

public class StackOverflow_10232385 
{ 
    public class MyCustomBehavior : IEndpointBehavior 
    { 
     public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 
      Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name); 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
      Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name); 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
      Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name); 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
      Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name); 
     } 
    } 

    public class MyCustomBehaviorExtension : BehaviorExtensionElement 
    { 
     public override Type BehaviorType 
     { 
      get { return typeof(MyCustomBehavior); } 
     } 

     protected override object CreateBehavior() 
     { 
      return new MyCustomBehavior(); 
     } 
    } 

    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     string Echo(string text); 
    } 
    public class Service : ITest 
    { 
     public string Echo(string text) 
     { 
      return text; 
     } 
    } 

    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 

     var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     ServiceModelSectionGroup smsg = configuration.GetSectionGroup("system.serviceModel") as ServiceModelSectionGroup; 
     EndpointBehaviorElement endpointBehaviorElement = smsg.Behaviors.EndpointBehaviors["MyCustomBehavior_10232385"]; 
     foreach (BehaviorExtensionElement behaviorElement in endpointBehaviorElement) 
     { 
      MethodInfo createBehaviorMethod = behaviorElement.GetType().GetMethod("CreateBehavior", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Type.EmptyTypes, null); 
      IEndpointBehavior behavior = createBehaviorMethod.Invoke(behaviorElement, new object[0]) as IEndpointBehavior; 
      endpoint.Behaviors.Add(behavior); 
     } 

     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); 
     ITest proxy = factory.CreateChannel(); 
     Console.WriteLine(proxy.Echo("Hello")); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 

Và cấu hình cho mã này:

<system.serviceModel> 
    <extensions> 
     <behaviorExtensions> 
      <add name="myCustomBehavior_10232385" type="QuickCode1.StackOverflow_10232385+MyCustomBehaviorExtension, QuickCode1"/> 
     </behaviorExtensions> 
    </extensions> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="MyCustomBehavior_10232385"> 
       <myCustomBehavior_10232385/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 
+0

hành vi của tôi đã Thực hiện hành vi mở rộng, lưu ý các tham số trong ... Xin lỗi nếu câu hỏi của tôi là không rõ ràng, nhưng trong tất cả các ví dụ tôi có thể tìm thấy, behaviorConfiguration được thêm vào một điểm cuối được xây dựng (dịch vụ hoặc máy khách) được khai báo. Tôi cần biết cách thêm nó vào điểm cuối với một hợp đồng đã cho được tạo lập trình. – DanO

+0

Hiểu rồi, tôi không hiểu nó trước đây. Tôi đã cập nhật câu trả lời với điều này và bạn có thể tìm thấy mã đầy đủ tại https://github.com/carlosfigueira/WCFQuickSamples/tree/master/WCFForums/QuickCode1. – carlosfigueira

+0

Cảm ơn bạn đã trải qua những nỗ lực để cung cấp giải pháp đó! Tôi đã từ bỏ làm những gì tôi muốn ủng hộ những gì đã được dễ dàng hơn, nhưng nếu tôi xem lại dự án đó mã của bạn sẽ rất hữu ích! – DanO

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