2012-02-08 7 views
0

Windows Azure上のASP.NETアプリケーションでRESTサービスをホストしています。完璧に動作していますが、数日前にInter-Role通信サービスが必要でした。私はサービスを構築しましたが、ホスティングに問題があります。WCF RESTとNET.TCPエンドポイントをAzureとASP.NETで一緒にホスティング

このコードは自分のローカルAzure開発サーバーでうまく動作しますが、Azure Cloudでは動作しませんでした。

正しくホストするにはどうすればよいですか?

のGlobal.asax:

public class Global : HttpApplication 
{ 
    void Application_Start(object sender, EventArgs e) 
    { 
     StartNotificationServiceHost(); 
     RegisterRoutes(); 
    } 

    public ServiceHost ServiceHost { get; private set; } 
    private void RegisterRoutes() 
    { 
     var hostfactory = new WebServiceHostFactory(); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Companies",hostfactory, typeof(Companies))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Public", hostfactory, typeof(Public))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Users", hostfactory, typeof(Users))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Contacts", hostfactory, typeof(Contacts))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Projects", hostfactory, typeof(Projects))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Instances", hostfactory, typeof(Instances))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Activity", hostfactory, typeof(Activity))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Search", hostfactory, typeof(Search))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Tasks", hostfactory, typeof(Tasks))); 
     RouteTable.Routes.Add(new ServiceRoute("REST/Documents", hostfactory, typeof(Documents))); 
    } 
    private void StartNotificationServiceHost() 
    { 
     Trace.WriteLine("Starting Service Host", "Information"); 
     NotificationServiceHost serviceHostBase = new NotificationServiceHost(); 
     serviceHostBase.RecycleNotificationRecieved += new RecycleNotificationRecievedEventHandler(ServiceHost_RecycleNotificationRecieved); 
     serviceHostBase.CheckInstanceStatusRecieved += new CheckInstanceStatusRecievedEventHandler(serviceHostBase_CheckInstanceStatusRecieved); 
     ServiceHost = new ServiceHost(serviceHostBase); 
     this.ServiceHost.Faulted += (sender, e) => 
     { 
      Trace.TraceError("Service Host fault occured"); 
      this.ServiceHost.Abort(); 
      Thread.Sleep(500); 
      this.StartNotificationServiceHost(); 

     }; 
     NetTcpBinding binding = new NetTcpBinding(SecurityMode.None); 
     RoleInstanceEndpoint notificationServiceHostEndPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["NotificationServiceEndPoint"]; 
     this.ServiceHost.AddServiceEndpoint(
      typeof(INotifyService), 
      binding, 
      String.Format("net.tcp://{0}/NotifyService", notificationServiceHostEndPoint.IPEndpoint) 
      ); 
     try 
     { 
      this.ServiceHost.Open(); 
      Trace.TraceInformation("Service Host Opened"); 
     } 
     catch (TimeoutException timeoutException) 
     { 
      Trace.TraceError("Service Host open failure, Time Out: " + timeoutException.Message); 
     } 
     catch (CommunicationException communicationException) 
     { 
      Trace.TraceError("Service Host open failure, Communication Error: " + communicationException.Message); 
     } 
     Trace.WriteLine("Service Host Started", "Information"); 
    } 
     InstanceItem serviceHostBase_CheckInstanceStatusRecieved(object sender, int e) 
     { 
... 
     } 
     void ServiceHost_RecycleNotificationRecieved(object sender, NotificationMessage e) 
     { 
... 

     } 
} 

のWeb.config/ServiceModelセクション:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- This behavior enables API Key Verification --> 
      <serviceAuthorization serviceAuthorizationManagerType="OfisimCRM.Webservice.APIKeyAuthorization, OfisimCRM.Webservice" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
      <serviceValidator /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <extensions> 
     <behaviorExtensions> 
     <add name="serviceValidator" type="OfisimCRM.Webservice.WebHttpWithValidationBehaviorExtension, OfisimCRM.Webservice, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
     </behaviorExtensions> 
    </extensions> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint crossDomainScriptAccessEnabled="true" faultExceptionEnabled="true" hostNameComparisonMode="WeakWildcard" name="" defaultOutgoingResponseFormat="Json" helpEnabled="true" automaticFormatSelectionEnabled="true" maxBufferSize="65536" maxReceivedMessageSize="104857600" transferMode="Streamed" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

サービス定義/ WebRole:

<WebRole name="OfisimCRM.WebClient" vmsize="Small"> 
    <Sites> 
     <Site name="Web"> 
     <Bindings> 
      <Binding name="Endpoint1" endpointName="Endpoint1" /> 
     </Bindings> 
     </Site> 
    </Sites> 
    <Endpoints> 
     <InputEndpoint name="Endpoint1" protocol="http" port="80" /> 
     <InternalEndpoint name="NotificationServiceEndPoint" protocol="tcp" /> 
    </Endpoints> 
    <Imports> 
     <Import moduleName="Diagnostics" /> 
     <Import moduleName="RemoteAccess" /> 
    </Imports> 
    <ConfigurationSettings> 
    </ConfigurationSettings> 
    </WebRole> 

エラー:

The server encountered an error processing the request. The exception message is 'Could not connect to net.tcp://x.x.x.29:8000/NotifyService. The connection attempt lasted for a time span of 00:00:21.0918600. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond x.x.x.29:8000. '. See server logs for more details. 

答えて

6

ロールインスタンスのアクセスは、ファイアウォールによって制限されています。通常、インスタンスはロードバランサからの接続のみを受け入れます。以下のようにサービス定義ファイル(* .csdef)に設定を追加するため、接続を受け入れる必要があります。また

 
<?xml version="1.0" encoding="utf-8" ?> 
    <ServiceDefinition name="RoleCommunication" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> 
: 
    <NetworkTrafficRules> 
    <OnlyAllowTrafficTo> 
     <Destinations> 
     <RoleEndpoint endpointName="YourEndPointName" roleName="YourRole" /> 
     <Destinations> 
     <WhenSource matches="AnyRole"> 
     <FromRole roleName="YourRole" /> 
     </WhenSource> 
    </OnlyAllowTrafficTo> 
    </NetworkTrafficRules> 
</ServiceDefinition > 

は、ルールにexpresilyスタートアップタスクでnetshコマンドを使用して接続を追加することができます。

EX) のnetshをadvfirewallは 'InternalEndpoint' ダイナミックポート

====

ているTCP =次に、localport = 80プロトコルを許可=アクションで= = "YourEndPoint" dirのファイアウォールルールの名前を追加しますか?可能であれば、.csdefと起動タスクに固定ポートを設定できますか?

 
    <Endpoints> 
     <InputEndpoint name="Endpoint1" protocol="http" port="80" /> 
     <InternalEndpoint name="NotificationServiceEndPoint" protocol="tcp" /> 
    </Endpoints> 

     <Startup> 
      <Task commandLine="configfirewall.cmd" executionContext="elevated" taskType="simple"> 
       <Environment> 
        <Variable name="NotificationServiceEndPointPort" value="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='NotificationServiceEndPoint']/@port" /> 
       </Environment> 
      </Task> 
     </Startup> 

とあなたは、環境変数を使用して次のように

EX)

 
<InternalEndpoint name="NotificationServiceEndPoint" protocol="tcp" port="8000"/> 

 
netsh advfirewall firewall add rule name="NotificationServiceEndPoint" dir=in action=allow localport=8000 protocol=tcp 

====

あるいは、環境変数が利用可能ですスタートアップ時

 
netsh advfirewall firewall add rule name="ServiceEndPoint" dir=in action=allow localport=%NotificationServiceEndPointPort% protocol=tcp 
+0

いいえ、動作しませんでした。質問に私のWebロール定義を追加しました。 – iboware

+0

私は以前の答えを編集しました。 –

+0

私はあなたの提案をすべて試みました。私は簡単にエラーを見るためにすべてのキャッチブロックを削除しました。それでも私は同じエラーが発生しています。私は私の質問にエラーメッセージを入れました。 – iboware