2011-04-05 6 views
0

내 특정 상황과 관련된 이유로 App.Config 파일에서 가능한 한 많이 제거하려고합니다. 내가 코드로 옮기려고하는 항목 중 하나는 웹 서비스와 관련된 정보입니다. 나는의 app.config의 정보를 촬영하고은 BasicHttpBinding 클래스를 만들었습니다 어떻게런타임에 수동으로 바인딩 추가

Uri baseAddress = new Uri("http://localservice/dataservice.asmx"); 

: 그 후

System.ServiceModel.BasicHttpBinding dss = new System.ServiceModel.BasicHttpBinding(); 
     dss.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.None; 
     dss.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None; 
     dss.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None; 
     dss.Security.Transport.Realm = ""; 

     dss.Security.Message.ClientCredentialType = System.ServiceModel.BasicHttpMessageCredentialType.UserName; 

     dss.Name = "DataServiceSoap"; 
     dss.CloseTimeout = System.TimeSpan.Parse("00:01:00"); 
     dss.OpenTimeout = System.TimeSpan.Parse("00:01:00"); 
     dss.ReceiveTimeout = System.TimeSpan.Parse("00:10:00"); 
     dss.SendTimeout = System.TimeSpan.Parse("00:10:00"); 
     dss.AllowCookies = false; 
     dss.BypassProxyOnLocal = false; 
     dss.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard; 
     dss.MaxBufferSize = 655360; 
     dss.MaxBufferPoolSize = 524288; 
     dss.MaxReceivedMessageSize = 655360; 
     dss.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text; 
     dss.TextEncoding = new System.Text.UTF8Encoding(); 
     dss.TransferMode = System.ServiceModel.TransferMode.Buffered; 
     dss.UseDefaultWebProxy = true; 
     dss.ReaderQuotas.MaxDepth = 32; 
     dss.ReaderQuotas.MaxStringContentLength = 8192; 
     dss.ReaderQuotas.MaxArrayLength = 16384; 
     dss.ReaderQuotas.MaxBytesPerRead = 4096; 
     dss.ReaderQuotas.MaxNameTableCharCount = 16384; 

를, I는 웹 서비스의 주소를 가리 키도록 URI를 생성 결국 클라이언트 엔드 포인트 주소와 바인딩을 추가합니까? 채널을 열어야합니까, 아니면이를 처리하는 더 쉬운 클래스가 있습니까?

답변

3

다음은 ChannelFactory를 사용하여 프로그래밍 방식으로 쉽게 할 수있는 방법입니다.

 BasicHttpBinding binding = new BasicHttpBinding(); 
     EndpointAddress address = new EndpointAddress("Your uri here"); 

     ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, address); 
     IContract channel = factory.CreateChannel(); 
     channel.YourMethod(); 
     ((ICommunicationObject)channel).Close(); 
관련 문제