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

后端 未结 2 1836
一个人的身影
一个人的身影 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);
    
    
    0 讨论(0)
  • 2020-12-05 16:53

    Well, the client endpoint in the config specifies this URL:

     <endpoint address="http://www.myuri.com/Services/Services.svc/basic"
    

    but in your code sample, you create:

     EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );
    

    The addresses must match - if the one in the config works, you'll need to change your code to:

     EndpointAddress endpointAddress = new EndpointAddress( "http://www.myuri.com/Services/Services.svc/basic" );
    

    Mind you - there are various little typos in your code sample (my.uri.com vs. www.myuri.com, /service.svc instead of /Services/Services.svc).

    Does it work with the corrected endpoint address?

    Marc

    0 讨论(0)
提交回复
热议问题