2015-02-06 9 views
1

기본적으로 호스트 된 서비스를 사용하여 서버와 데이터를 송수신하는 파일 서버/클라이언트 도구를 작성하는 중입니다. 이 솔루션은 많은 사람들이 사용할 것이므로, 설치를 위해 App.Config 파일을 편집하고 편집하는 것은 바람직하지 않습니다. 내가 뭘하고 싶은지는 런타임에이를 변경하여 사용자가 사용할 설정을 완벽하게 제어 할 수있게하는 것입니다.런타임시 ServiceHost EndPoint 주소 변경 C#

<system.serviceModel> 
     <services> 
      <service name="FI.ProBooks.FileSystem.FileRepositoryService"> 
       <endpoint name="" binding="netTcpBinding" 
        address="net.tcp://localhost:5000" 
        contract="FI.ProBooks.FileSystem.IFileRepositoryService" 
        bindingConfiguration="customTcpBinding" /> 
      </service> 
     </services> 
     <bindings> 
      <netTcpBinding> 
       <binding name="customTcpBinding" transferMode="Streamed" maxReceivedMessageSize="20480000" /> 
      </netTcpBinding> 
     </bindings> 
    </system.serviceModel> 

내가 뭘하고 싶은 것은 주소 만 변경하는 것입니다 (이 예에서는, net.tcp : // localhost를 : 5000) 응용 프로그램이 실행 그래서,이 내 app.config 파일입니다. 따라서 현재 값을 읽고이를 사용자에게 표시 한 다음 입력을 가져 와서 해당 필드에 다시 저장할 수 있어야합니다.

답변

0

구성 이름과 엔드 포인트를 제공하는 서비스 인스턴스를 생성 할 수 있습니다. 그래서 당신은 사용할 수 있습니다;

EndpointAddress endpoint = new EndpointAddress(serviceUri); 
var client= new MyServiceClient(endpointConfigurationName,endpoint) 

보기 msdn 문서를 참조하십시오.

+0

문제는 ServiceHost를하지 클라이언트에 관한 한 –

0

아래의 테스트가 도움이 될 수 있습니다. 기본적으로 단계는 다음과 같습니다.

  • .config 파일에서 구성을 읽는 인스턴스를 인스턴스화합니다.
  • 이전 인스턴스와 동일한 구성을 사용하여 EndpointAddress의 새 인스턴스를 만들고 uri를 변경하고 ServiceEndpointAddress 속성에 할당합니다.

    [TestMethod] 
    public void ChangeEndpointAddressAtRuntime() 
    { 
        var host = new ServiceHost(typeof(FileRepositoryService)); 
    
        var serviceEndpoint = host.Description.Endpoints.First(e => e.Contract.ContractType == typeof (IFileRepositoryService)); 
        var oldAddress = serviceEndpoint.Address; 
        Console.WriteLine("Curent Address: {0}", oldAddress.Uri); 
    
        var newAddress = "net.tcp://localhost:5001"; 
        Console.WriteLine("New Address: {0}", newAddress); 
        serviceEndpoint.Address = new EndpointAddress(new Uri(newAddress), oldAddress.Identity, oldAddress.Headers); 
        Task.Factory.StartNew(() => host.Open()); 
    
        var channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), new EndpointAddress(newAddress)); 
        var channel = channelFactory.CreateChannel(); 
    
        channel.Method(); 
    
        (channel as ICommunicationObject).Close(); 
    
    
        channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), oldAddress); 
        channel = channelFactory.CreateChannel(); 
    
        bool failedWithOldAddress = false; 
        try 
        { 
         channel.Method(); 
        } 
        catch (Exception e) 
        { 
         failedWithOldAddress = true; 
        } 
    
        (channel as ICommunicationObject).Close(); 
    
        Assert.IsTrue(failedWithOldAddress); 
    }