2012-08-22 4 views
2

저는 Azure를 사용하는 프로젝트에서 작업 중입니다. 작업자 역할 내에서 웹 서비스를 사용하여 정보를 제출하고 싶습니다. 내 질문은 : webservice 그것에 서비스 참조를 추가하지 않고? 또는 하늘에 프로젝트를 게시 할 때 서비스 참조를 변경할 필요가없는 특정 방식으로 추가 할 수 있습니까?서비스 참조를 추가하지 않고 동일한 솔루션 내에서 WCF 참조를 추가하십시오.

+2

당신은 구성 할 수 원하는 웹 서비스의 끝점? 그렇다면 app.config 파일을 사용하여 확인하십시오. – Slugart

답변

3

채널 팩토리를 사용하여 직접 연결할 수 있습니다. 여기에 같은 IMyService 같은 T는 귀하의 서비스 계약 당신이 오버라이드 (override) 할 샘플 기본 저장소 클래스,,,, 그런 다음 게시 및 에뮬레이션 환경에서 모두 작동 엔드 포인트를 발견하고

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using Microsoft.WindowsAzure.ServiceRuntime; 

namespace pageengine.clients 
{ 
    public class RepositoryBase<T> : IDisposable 
    { 

     #region Channel 
     protected String roleName; 
     protected String serviceName; 
     protected String endpointName; 
     protected String protocol = @"http"; 

     protected EndpointAddress _endpointAddress; 
     protected BasicHttpBinding httpBinding; 
     protected NetTcpBinding tcpBinding; 

     protected IChannelFactory channelFactory; 
     protected T client; 

     protected virtual AddressHeader[] addressHeaders 
     { 
      get 
      { 
       return null; 
      } 
     } 

     protected virtual EndpointAddress endpointAddress 
     { 
      get 
      { 
       if (_endpointAddress == null) 
       { 
        var endpoints = RoleEnvironment.Roles[roleName].Instances.Select(i => i.InstanceEndpoints[endpointName]).ToArray(); 
        var endpointIP = endpoints.FirstOrDefault().IPEndpoint; 
        if(addressHeaders != null) 
        { 
         _endpointAddress = new EndpointAddress(new Uri(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName)), addressHeaders); 
        } 
        else 
        { 
         _endpointAddress = new EndpointAddress(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName)); 
        } 

       } 
       return _endpointAddress; 
      } 
     } 

     protected virtual Binding binding 
     { 
      get 
      { 
       switch (protocol) 
       { 
        case "tcp.ip": 
         if (tcpBinding == null) tcpBinding = new NetTcpBinding(); 
         return tcpBinding; 
        default: 
         //http 
         if (httpBinding == null) httpBinding = new BasicHttpBinding(); 
         return httpBinding; 
       } 
      } 
     } 

     public virtual T Client 
     { 
      get 
      { 
       if (this.client == null) 
       { 
        this.channelFactory = new ChannelFactory<T>(binding, endpointAddress); 
        this.client = ((ChannelFactory<T>)channelFactory).CreateChannel(); 
        ((IContextChannel)client).OperationTimeout = TimeSpan.FromMinutes(2); 
        var scope = new OperationContextScope(((IContextChannel)client)); 
        addCustomMessageHeaders(scope); 
       } 
       return this.client; 
      } 
     } 

     protected virtual void addCustomMessageHeaders(OperationContextScope operationContextScope) 
     { 
      // Overidden 
     } 
     #endregion 

     #region CTOR 
     public RepositoryBase() 
     { 

     } 
     #endregion 

     #region IDisposable Members 

     public virtual void Dispose() 
     { 
      if (client != null) 
      { 
       try 
       { 
        ((ICommunicationObject)client).Close(); 
       } 
       catch 
       { 
        try 
        { 
         ((ICommunicationObject)client).Abort(); 
        } 
        catch { } // Die quietly. 
       } 
      } 
      if (channelFactory != null) 
      { 
       try 
       { 
        channelFactory.Close(); 
       } 
       catch 
       { 
        try 
        { 
         channelFactory.Abort(); 
        } 
        catch { } // Die quietly. 
       } 
       channelFactory = null; 
      } 
      _endpointAddress = null; 
      httpBinding = null; 
      tcpBinding = null; 
     } 

     #endregion 
    } 
} 

입니다. 이 기지를 확장하는 클래스는 다음과 같습니다

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

using pageengine.services.entities.account; 
using pageengine.services; 

namespace pageengine.clients.accounts 
{ 
    public class AccountsRepository : RepositoryBase<IAccounts>, IDisposable 
    { 
     #region CTOR 
     public AccountsRepository() 
     { 
      this.roleName = "EntitiesRole";  // Name of the role my service is on 
      this.endpointName = "HttpInternal"; // Name of the endpoint configured on that role. Can be internal or input, tcp or http. 
      this.serviceName = "Accounts.svc"; // Name of my service. 
     } 
     #endregion 
    } 
} 

통화 서비스에 (AN MVC의 컨트롤러 액션에서이 경우)되는 다음의 형태 :

public ActionResult ListAccounts() 
    { 
     using (var accountsRepository = new AccountsRepository()) 
     { 
      return View("ListAccounts", accountsRepository.Client.ListAccounts()); 
     } 
    } 
관련 문제