2009-01-30 7 views
8

मुझे लिखने वाले क्लाइंट एप्लिकेशन के लिए app.config को तैनात किए बिना मैंने डब्लूसीएफ सेवा से कनेक्ट करने की आवश्यकता है। हालांकि, मुझे कोड में क्लाइंट साइड से चीजों को सेट अप करने का तरीका जानने का प्रयास करने में बहुत मुश्किल समय हो रहा है। यह जहां तक ​​मैंने प्राप्त किया है ... क्या किसी के पास कोई विचार है जो मुझे काम करने के लिए करने की ज़रूरत है? मैं वास्तव में इसकी सराहना करता हूं।कोड में wsDualHttp बाइंडिंग का उपयोग कर डब्लूसीएफ क्लाइंट कैसे सेट अप करें?

इस कोड को मैं अब तक मिल गया है है:

String baseAddress = "http://localhost/CommService"; 

    WSDualHttpBinding binding = new WSDualHttpBinding(); 
    binding.Name = "WSDualHttpBinding_ICommService"; 
    binding.ClientBaseAddress = new Uri(baseAddress); 
    binding.ReliableSession.Ordered = true; 
    binding.ReliableSession.InactivityTimeout = new TimeSpan(0, 10, 0); 
    binding.ReceiveTimeout = new TimeSpan(0, 10, 0); 
    binding.SendTimeout = new TimeSpan(0, 0, 5); 

    InstanceContext context = new InstanceContext(this); 
    client = new CommServiceClient(context, "WSDualHttpBinding_ICommService"); 
    client.Endpoint.Binding = binding; 

और यह मेरे मुवक्किल को एप्लिकेशन की app.config है:

<system.serviceModel> 
    <bindings> 
     <wsDualHttpBinding> 
      <binding name="WSDualHttpBinding_ICommService" closeTimeout="00:01:00" 
       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:00:05" 
       bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
       maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
       messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       <reliableSession ordered="true" inactivityTimeout="00:10:00" /> 
       <security mode="Message"> 
        <message clientCredentialType="Windows" negotiateServiceCredential="true" 
         algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </wsDualHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost/CommService/" 
      binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_ICommService" 
      contract="Services.ICommService" name="WSDualHttpBinding_ICommService"> 
      <identity> 
       <dns value="localhost" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

उत्तर

9

आप आसानी से प्राप्त कर सकते हैं कि आप क्या चाहते। कोड नीचे देखें:

Uri baseAddress = new Uri("http://localhost/CommService"); 
WSDualHttpBinding wsd = new WSDualHttpBinding(); 
EndpointAddress ea = new EndpointAddress(baseAddress, EndpointIdentity.CreateDnsIdentity("localhost")); 
client = new CommServiceClient(new InstanceContext(this), wsd, ea); 

मुझे थोड़ी समझाता हूँ:

  • पहले हम डिफ़ॉल्ट सेटिंग्स (उन सटीक लोगों उत्पन्न app.config है कर रहे हैं) के साथ एक WSDualHttpBinding का एक उदाहरण बना सकते हैं। यदि आप किसी भी सेटिंग्स को संशोधित करना चाहते हैं, तो आप उन्हें उजागर गुणों को कम कर सकते हैं।
  • फिर हम वांछित यूआरएल और पहचान के साथ एक एंडपॉइंट एड्रेस बनाते हैं। इसे बाध्यकारी से जोड़ने की आवश्यकता नहीं है क्योंकि हम उन सभी को सेवा क्लाइंट कन्स्ट्रक्टर में लिंक करेंगे।
  • आखिरकार हम सेवा क्लाइंट बनाते हैं। एक नियंत्रक अधिभार में से एक हमें बाध्यकारी और एंडपॉइंट पता निर्दिष्ट करने की अनुमति देता है।
  • सामान्य रूप से app.config फ़ाइल में उपलब्ध प्रत्येक तत्व में .NET कोड में एक संबद्ध कक्षा है और प्रत्येक विशेषता या बाल तत्व में निर्दिष्ट वर्ग में संबंधित संपत्ति है।
+1

यह पूरी तरह से काम करता है, धन्यवाद! यह मुझे पागल कर रहा था, हाहा। –

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