WCF: How can I programmatically recreate these App.config values?

后端 未结 2 1854
一个人的身影
一个人的身影 2020-12-05 16:16

I have a WCF service that works ok if I create the service without specifying any binding or endpoint (it reads it from the generated values in the App.config when I registe

2条回答
  •  醉酒成梦
    2020-12-05 16:46

    Most of the values in the App config are also properties in the binding and can be recreated programatically. Personally, I use a method such as the one below to create the binding

    
     public static BasicHttpBinding CreateBasicHttpBinding()
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                binding.AllowCookies = false;
                binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
                binding.OpenTimeout = new TimeSpan(0, 1, 0);
                binding.SendTimeout = new TimeSpan(0, 1, 0);
                // add more based on config file ...
                //buffer size
                binding.MaxBufferSize = 65536;
                binding.MaxBufferPoolSize = 534288;
                binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    
                //quotas
                binding.ReaderQuotas.MaxDepth = 32;
                binding.ReaderQuotas.MaxStringContentLength = 8192;
                // add more based on config file ...
    
                return binding;
            }
    
    

    And I use something like this for creating my Endpoint address

    
    public static EndpointAddress CreateEndPoint()
            {
                return new EndpointAddress(Configuration.GetServiceUri());
            }
    
    

    The serviceUri will be the service URL such as http://www.myuri.com/Services/Services.svc/basic

    Finally to create the service client

    
     Binding httpBinding = CreateBasicHttpBinding();
     EndpointAddress address = CreateEndPoint();
     var serviceClient = new MyServiceClient(httpBinding, address);
    
    

提交回复
热议问题