2010-06-25 18 views
5

मेरे पास हैंडहेल्ड हैं जिन्हें मूल एचटीटीपी बाइंडिंग के माध्यम से संवाद करने की आवश्यकता है। मेरे पास एक अनुबंध है और सब कुछ विज्ञापित के रूप में काम करता है।एक ही अनुबंध, एक ही बाध्यकारी, एक ही पता, लेकिन विभिन्न बंदरगाहों के लिए संभव है?

मुझे परीक्षण पर्यावरण, प्रशिक्षण और पाठ्यक्रम के उत्पादन में आसानी से समर्थन करने के लिए इसे विस्तारित करने की आवश्यकता है। मैंने बंदरगाह मार्ग लिया, सोच रहा था कि मैं बंदरगाह मतभेदों के साथ अलग-अलग अंतराल का पर्दाफाश कर सकता हूं, और बंदरगाह पर आधारित, यह तय कर सकता हूं कि मैं किस डेटाबेस से जानकारी चाहता हूं।

मुझे यह काम नहीं लगता है, और अब तक कोई जानकारी नहीं मिली है जो इंगित करती है कि यह किया जा सकता है। चूंकि बंदरगाह वैकल्पिक है, यह नहीं हो सकता है।

किसी ने ऐसा कुछ किया है?

उत्तर

6

हालांकि आप पोर्ट के साथ जो भी चाहते हैं वह नहीं कर सकते हैं, आप इसे एक अलग पथ से पूरा कर सकते हैं। जैसे कि आपके मूल पते पर "/ prod" या "/ test" जोड़ना। मैंने एक उदाहरण शामिल किया है जो इसे दिखाता है।

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ServiceModel; 
using System.ServiceModel.Description; 

namespace WCFTest 
{ 
    class Program 
    { 
     static void Main() 
     { 
      List<Uri> baseAddresses = new List<Uri> { new Uri("http://localhost:1000/Prod"), new Uri("http://localhost:1000/Test") }; 
      ServiceHost wcfHost = new ServiceHost(typeof(SimpleWCF), new Uri[] {new Uri("http://localhost:1000")}); 

      foreach (ServiceEndpoint endpoint in SimpleWCF.CreateEndpoints(baseAddresses.ToArray())) 
      { 
       wcfHost.AddServiceEndpoint(endpoint); 
      } 

      ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior(); 
      metadataBehavior.HttpGetEnabled = true; 
      wcfHost.Description.Behaviors.Add(metadataBehavior); 

      wcfHost.Open(); 
      Console.ReadLine(); 
      wcfHost.Close(); 
     } 
    } 

    [ServiceContract] 
    public interface ISimpleWCF 
    { 
     [OperationContract] 
     string TestMethod(); 
    } 

    public class SimpleWCF : ISimpleWCF 
    { 
     /// <summary> 
     /// Thread Synchronization Object. 
     /// </summary> 
     private static readonly object _syncRoot = new object(); 

     /// <summary> 
     /// Static Instance of Class. 
     /// </summary> 
     private static volatile SimpleWCF _current; 

     /// <summary> 
     /// Initializes a new instance of the <see cref="WebDataExchange"/> class. 
     /// </summary> 
     public SimpleWCF() 
     { 
      this.Contract = ContractDescription.GetContract(typeof(ISimpleWCF), GetType()); 
     } 

     /// <summary> 
     /// Gets or sets the contract. 
     /// </summary> 
     /// <value>The contract.</value> 
     private ContractDescription Contract { get; set; } 

     /// <summary> 
     /// Gets the current instance of the SimpleWCF Object. 
     /// </summary> 
     /// <value>The current SimpleWCF Object.</value> 
     public static SimpleWCF Current 
     { 
      get 
      { 
       if (_current != null) 
       { 
        return _current; 
       } 

       lock (_syncRoot) 
       { 
        if (_current == null) 
         _current = new SimpleWCF(); 

       } 

       return _current; 
      } 
     } 

     /// <summary> 
     /// Creates an Enpoint Collection. 
     /// </summary> 
     /// <param name="addresses">The addresses.</param> 
     /// <returns>A Collection of ServiceEndpoints.</returns> 
     public static Collection<ServiceEndpoint> CreateEndpoints(Uri[] addresses) 
     { 
      Collection<ServiceEndpoint> endpointCollection = new Collection<ServiceEndpoint>(); 

      foreach (Uri uriAddress in addresses) 
      { 
       EndpointAddress address = new EndpointAddress(uriAddress); 

       BasicHttpSecurityMode securityMode = address.Uri.Scheme == Uri.UriSchemeHttps ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None; 
       BasicHttpBinding endpointBinding = new BasicHttpBinding(securityMode); 

       ServiceEndpoint endpoint = new ServiceEndpoint(Current.Contract, endpointBinding, address); 
       endpoint.ListenUriMode = ListenUriMode.Explicit; 
       endpointCollection.Add(endpoint); 
      } 

      return endpointCollection; 
     } 

     #region ISimpleWCF Members 

     string ISimpleWCF.TestMethod() 
     { 
      if (OperationContext.Current.Channel.LocalAddress.Uri.AbsoluteUri.EndsWith("Prod")) 
       return "Hello Prod!"; 
      else return "Hello Test!"; 
     } 

     #endregion 
    } 

} 
संबंधित मुद्दे