2012-02-08 65 views
0

我通过在Windows Azure上的ASP.NET应用程序托管我的REST服务。它的工作非常完美,但几天前我需要一个角色间通信服务,我建立了我的服务,但我在托管服务时遇到了问题。托管WCF REST和的net.tcp在Azure上一起端点与ASP.NET

这段代码在我的Global.asax工作在我的本地Azure的开发服务器伟大的,但它并没有在碧霞工作。

什么是正确收留他们的最好方法?

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 > 

或者,您可以在启动任务中使用netsh命令显式添加规则连接。

前) 的netsh advfirewall防火墙添加规则名称= “YourEndPoint” DIR =行动=允许将localPort = 80协议= TCP

====

是 'InternalEndpoint' 动态端口?如果可能,您可以在.csdef和启动任务中设置固定端口。

前)

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

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

====

或者,环境变量可用如下:

 
    <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> 

,并使用环境变量在启动任务

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

没有,也没有工作。我在问题中添加了我的webrole定义。 – iboware 2012-02-08 05:05:40

+0

我编辑了一个较早的答案。 – 2012-02-08 06:16:18

+0

我尝试了所有的建议。我删除了所有尝试catch块来轻松查看错误。仍然我收到了同样的错误。我把错误信息放在我的问题上。 – iboware 2012-02-09 00:02:54