سؤال

لا أحد يعرف من مثال جيد على كيفية فضح خدمة WCF برمجيا دون استخدام تكوين الملف ؟ أنا أعرف كائن خدمة النموذج هو أكثر ثراء الآن مع صندوق رأس المال العامل ، لذلك أنا أعلم أنه من الممكن.أنا فقط لم أر مثال عن كيفية القيام بذلك.على العكس من ذلك ، أود أن أرى كيف تستهلك دون تكوين ملف به كذلك.

قبل أن يسأل أي شخص لدي محددة جدا تحتاج إلى القيام بذلك من دون ملفات التكوين.أنا عادة لا ننصح مثل هذه الممارسة ، ولكن كما قلت هناك محدد جدا تحتاج في هذه الحالة.

هل كانت مفيدة؟

المحلول

تستهلك خدمة الإنترنت بدون ملف التكوين هو بسيط جدا ، لقد اكتشفت.لكم ببساطة الحاجة إلى إنشاء كائن توثيق ومعالجة موضوع تمر بها إما إلى منشئ العميل الوكيل أو عامة ChannelFactory سبيل المثال.يمكنك أن تبحث في التطبيق الافتراضي.config لمعرفة ما هي إعدادات لاستخدامها ، ثم خلق ثابت مساعد الأسلوب في مكان ما أن instantiates البروكسي الخاص بك:

internal static MyServiceSoapClient CreateWebServiceInstance() {
    BasicHttpBinding binding = new BasicHttpBinding();
    // I think most (or all) of these are defaults--I just copied them from app.config:
    binding.SendTimeout = TimeSpan.FromMinutes( 1 );
    binding.OpenTimeout = TimeSpan.FromMinutes( 1 );
    binding.CloseTimeout = TimeSpan.FromMinutes( 1 );
    binding.ReceiveTimeout = TimeSpan.FromMinutes( 10 );
    binding.AllowCookies = false;
    binding.BypassProxyOnLocal = false;
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.MessageEncoding = WSMessageEncoding.Text;
    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.TransferMode = TransferMode.Buffered;
    binding.UseDefaultWebProxy = true;
    return new MyServiceSoapClient( binding, new EndpointAddress( "http://www.mysite.com/MyService.asmx" ) );
}

نصائح أخرى

إذا كنت ترغب في التخلص من استخدام النظام.ServiceModel القسم في شبكة الإنترنت.التكوين في IIS استضافة, لقد وضعت مثال عن كيفية القيام بذلك هنا (http://bejabbers2.blogspot.com/2010/02/wcf-zero-config-in-net-35-part-ii.html).تبين لي كيفية تخصيص ServiceHost لإنشاء كل البيانات wshttpbinding النهاية.أفعل ذلك في الغرض العام الطريقة التي لا تتطلب ترميز إضافية.بالنسبة لأولئك الذين ليسوا على الفور الترقية إلى .NET 4.0 وهذا يمكن أن تكون مريحة جدا.

هنا هذا هو العامل التعليمات البرمجية.أعتقد أنها سوف تساعدك كثيرا.كنت ابحث و لم يجد قانون كامل هذا هو السبب في أنني حاولت وضع كاملة و تعمل التعليمات البرمجية.حظا سعيدا.

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;
    }
}

فإنه ليس من السهل على الملقم الجانب..

على جانب العميل, يمكنك استخدام ChannelFactory

كل WCF يمكن أن يتم تكوين برمجيا.لذلك فمن الممكن لخلق كلا ملقمات وعملاء دون config.

أوصي كتاب "البرمجة خدمات WCF" قبل Juval لوي الذي يحتوي على العديد من الأمثلة على تكوين البرامج.

فإنه من السهل جدا القيام به على كل من العميل والخادم الجانب.Juval لوي كتاب له أمثلة ممتازة.

ما تعليقك حول ملفات التكوين أود أن أقول أن تكوين الملفات رجل فقير الثاني إلى فعل ذلك في التعليمات البرمجية.تكوين ملفات كبيرة عند السيطرة على كل عميل أن الاتصال إلى الملقم الخاص بك وتأكد من أنهم تحديث و أن المستخدمين لا يمكن العثور عليها تغيير أي شيء.أجد WCF ملف تكوين نموذج الحد ، أقل ما يقال الصعب تصميم و صيانة كابوس.جميع في كل شيء ، أعتقد أنه كان فقيرا جدا المقرر من قبل مايكروسوفت لجعل ملفات التكوين الافتراضي وسيلة للقيام بهذه الأمور.

تحرير:واحدة من الأشياء التي لا يمكنك أن تفعل مع ملف التكوين هو خلق خدمات غير افتراضي المنشئات.وهذا يؤدي إلى ثابت/المتغيرات العالمية ووحدانية وأنواع أخرى من غير معنى في صندوق رأس المال العامل.

لقد وجدت بلوق وظيفة في الرابط أدناه حول هذا الموضوع مثير جدا للاهتمام.

فكرة واحدة أحب هو أن تكون قادرة على مجرد تمر في ملزمة أو السلوك أو معالجة XML القسم من التكوين المناسب WCF موضوع وليكن التعامل مع تعيين خصائص - حاليا لا يمكنك أن تفعل هذا.

مثل الآخرين على شبكة الإنترنت أواجه قضايا حول الحاجة إلى بلدي WCF تنفيذ لاستخدام مختلف ملف التكوين من استضافة التطبيق (وهو .صافي خدمة Windows 2.0).

http://salvoz.com/blog/2007/12/09/programmatically-setting-wcf-configuration/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top