2011-06-13 17 views
13

मैं सिर्फ एक महीने पहले डब्ल्यूसीएफ पर काम शुरू करता हूं। कृपया मुझे माफ़ कर दो अगर मैं पहले से ही कुछ जवाब मांगता हूं। मैं पहले खोज करने की कोशिश करता हूं लेकिन कुछ भी नहीं मिला।कॉन्फ़िगरेशन फ़ाइल में सेटिंग्स के बिना डब्ल्यूसीएफ क्लाइंट कैसे बनाएं?

मैंने इस आलेख को पढ़ा, डब्ल्यूसीएफ फ़ाइल स्थानांतरण: स्ट्रीमिंग & आईआईएस में होस्ट किए गए चंकिंग चैनल। यह बहुत अच्छा काम करता है। अब मैं अपने आवेदन का हिस्सा बनने के लिए क्लाइंट साइड कोड को एकीकृत करना चाहता हूं, जो ऑटोकैड के अंदर चल रहा एक डीएल है। अगर मैं कॉन्फ़िगरेशन फ़ाइल के साथ काम करना चाहता हूं, तो मुझे acad.exe.config को बदलना होगा जो मुझे नहीं लगता कि यह एक अच्छा विचार है। इसलिए मुझे लगता है कि यदि यह संभव है, तो मैं कोड में कॉन्फ़िगरेशन फ़ाइल में सभी कोड को स्थानांतरित करना चाहता हूं।

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" 
       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
       allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
       messageEncoding="Mtom" textEncoding="utf-8" transferMode="Buffered" 
       useDefaultWebProxy="true"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       <security mode="None"> 
        <transport clientCredentialType="None" proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="UserName" algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://10.1.13.15:88/WCFStreamUpload/service.svc/ep1" 
      binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpBinding_IService" 
      contract="MGFileServerClient.IService" 
      name="BasicHttpBinding_IService" /> 
    </client> 
</system.serviceModel> 

आप कृपया मेरी मदद कर सके यह परिवर्तन करने के:

यहाँ कॉन्फ़िग फ़ाइल है?

+0

क्योंकि acad.exe.config सी: \ प्रोग्राम फ़ाइलें \ के अंतर्गत है और हमारी कंपनी के पास इस फ़ोल्डर तक पहुंच सीमित करने की नीति है। – weslleywang

उत्तर

23

आप कोड में से सभी सेटिंग कर सकते हैं, यह मानते हुए कि आपको भविष्य में इसे बदलने की लचीलापन की आवश्यकता नहीं है।

आप MSDN पर एंडपॉइंट सेट करने के बारे में पढ़ सकते हैं। हालांकि यह सर्वर पर लागू होता है एंडपॉइंट और बाइंडिंग की कॉन्फ़िगरेशन क्लाइंट पर भी लागू होती है, यह सिर्फ कि आप कक्षाओं का अलग-अलग उपयोग करते हैं।

मूल रूप से आप की तरह कुछ करना चाहता हूँ:

// Specify a base address for the service 
EndpointAddress endpointAdress = new EndpointAddress("http://10.1.13.15:88/WCFStreamUpload/service.svc/ep1"); 
// Create the binding to be used by the service - you will probably want to configure this a bit more 
BasicHttpBinding binding1 = new BasicHttpBinding(); 
///create the client proxy using the specific endpoint and binding you have created 
YourServiceClient proxy = new YourServiceClient(binding1, endpointAddress); 

जाहिर है आप शायद सुरक्षा के साथ बंधन कॉन्फ़िगर करने के लिए चाहता हूँ, टाइमआउट आदि के ऊपर अपने config (you can read about the BasicHttpBinding on MSDN) के रूप में ही है, लेकिन यह आप के लिए जा रहा मिलना चाहिए सही दिशा में।

2

क्या आप अपनी कस्टम कॉन्फ़िगरेशन को बनाए रखना चाहते हैं और इसे अपने एप्लिकेशन में संदर्भित करना चाहते हैं? आप इस आलेख को आजमा सकते हैं: Reading WCF Configuration from a Custom Location (डब्ल्यूसीएफ के संबंध में)

अन्यथा, आप ConfigurationManager.OpenExeConfiguration का उपयोग कर सकते हैं।

3

यह पूरी तरह से कोड आधारित कॉन्फ़िगरेशन और कार्य कोड है। आपको क्लाइंट के लिए किसी कॉन्फ़िगरेशन फ़ाइल की आवश्यकता नहीं है। लेकिन कम से कम आपको वहां एक कॉन्फ़िगरेशन फ़ाइल की आवश्यकता है (स्वचालित रूप से जेनरेट हो सकता है, आपको इसके बारे में सोचना नहीं है)। कोड में सभी विन्यास सेटिंग यहां की जाती है।

public class ValidatorClass 
{ 
    WSHttpBinding BindingConfig; 
    EndpointIdentity DNSIdentity; 
    Uri URI; 
    ContractDescription ConfDescription; 

    public ValidatorClass() 
    { 
     // In constructor initializing configuration elements by code 
     BindingConfig = ValidatorClass.ConfigBinding(); 
     DNSIdentity = ValidatorClass.ConfigEndPoint(); 
     URI = ValidatorClass.ConfigURI(); 
     ConfDescription = ValidatorClass.ConfigContractDescription(); 
    } 


    public void MainOperation() 
    { 
     var Address = new EndpointAddress(URI, DNSIdentity); 
     var Client = new EvalServiceClient(BindingConfig, Address); 
     Client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; 
     Client.Endpoint.Contract = ConfDescription; 
     Client.ClientCredentials.UserName.UserName = "companyUserName"; 
     Client.ClientCredentials.UserName.Password = "companyPassword"; 
     Client.Open(); 

     string CatchData = Client.CallServiceMethod(); 

     Client.Close(); 
    } 



    public static WSHttpBinding ConfigBinding() 
    { 
     // ----- Programmatic definition of the SomeService Binding ----- 
     var wsHttpBinding = new WSHttpBinding(); 

     wsHttpBinding.Name = "BindingName"; 
     wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10); 
     wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.BypassProxyOnLocal = false; 
     wsHttpBinding.TransactionFlow = false; 
     wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
     wsHttpBinding.MaxBufferPoolSize = 524288; 
     wsHttpBinding.MaxReceivedMessageSize = 65536; 
     wsHttpBinding.MessageEncoding = WSMessageEncoding.Text; 
     wsHttpBinding.TextEncoding = Encoding.UTF8; 
     wsHttpBinding.UseDefaultWebProxy = true; 
     wsHttpBinding.AllowCookies = false; 

     wsHttpBinding.ReaderQuotas.MaxDepth = 32; 
     wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384; 
     wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192; 
     wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096; 
     wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384; 

     wsHttpBinding.ReliableSession.Ordered = true; 
     wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10); 
     wsHttpBinding.ReliableSession.Enabled = false; 

     wsHttpBinding.Security.Mode = SecurityMode.Message; 
     wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 
     wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None; 
     wsHttpBinding.Security.Transport.Realm = ""; 

     wsHttpBinding.Security.Message.NegotiateServiceCredential = true; 
     wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
     wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256; 
     // ----------- End Programmatic definition of the SomeServiceServiceBinding -------------- 

     return wsHttpBinding; 

    } 

    public static Uri ConfigURI() 
    { 
     // ----- Programmatic definition of the Service URI configuration ----- 
     Uri URI = new Uri("http://localhost:8732/Design_Time_Addresses/TestWcfServiceLibrary/EvalService/"); 

     return URI; 
    } 

    public static EndpointIdentity ConfigEndPoint() 
    { 
     // ----- Programmatic definition of the Service EndPointIdentitiy configuration ----- 
     EndpointIdentity DNSIdentity = EndpointIdentity.CreateDnsIdentity("tempCert"); 

     return DNSIdentity; 
    } 


    public static ContractDescription ConfigContractDescription() 
    { 
     // ----- Programmatic definition of the Service ContractDescription Binding ----- 
     ContractDescription Contract = ContractDescription.GetContract(typeof(IEvalService), typeof(EvalServiceClient)); 

     return Contract; 
    } 
}