Windows服务中托管的netTcpRelayBinding WCF服务 - 标准实践

时间:2013-02-07 14:57:16

标签: wcf azure windows-services azureservicebus

我正在编写Windows服务中托管的netTcpRelayBinding WCF服务。由于缺乏Windows服务开发经验,我觉得我的代码很幼稚。这是从Windows-Azure调用的。你能帮我看一下代码,以及我如何即兴发布: - 我们正在使用NLOG进行日志记录。

我的服务抛出异常(在问题末尾提到)。

我应该怎样: -  1.定义Windows服务依赖项(应该在机器获取之前启动)     连接到互联网)。 2.它应该如何更具容错性,如何在一段时间间隔后启动。

namespace XYC.Service.WinServiceHost
    {
        public partial class XYCService : ServiceBase
        {
            private ServiceHost serviceHost = null;
            private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
            public One23InsightService()
            {
                // Name the Windows Service
                ServiceName = "XYCService ";
                const string logName = "Application";
                InitializeComponent();
                if (!System.Diagnostics.EventLog.SourceExists(ServiceName))
                {
                    System.Diagnostics.EventLog.CreateEventSource(ServiceName, logName);
                }
                EnLog.Source = ServiceName;
                EnLog.Log = logName;
            }

            protected override void OnStart(string[] args)
            {
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }
                EnLog.WriteEntry(ServiceName + " Inside OnStart...");
                try
                {
                   serviceHost = new ServiceHost(typeof(InsightSrcDataService));

                    serviceHost.Faulted += HostFaulted;
                    serviceHost.UnknownMessageReceived += HostUnknownMessageReceived;

                    serviceHost.Open();
                    LogServiceInfo(serviceHost);
                    if (serviceHost.State != CommunicationState.Faulted)
                    {
                        EnLog.WriteEntry(ServiceName + " started successfully.");
                        EnLog.WriteEntry(ServiceName + " Automapping of entities are done successfully.");
                    }
                }
                catch (Exception ex)
                {
                    string msg = ServiceName + " failed to start. Exception Message:-" + ex.Message + (ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                    EnLog.WriteEntry(msg);
                    Logger.LogException(NLog.LogLevel.Error, msg, ex);
                }
            }

            private void LogServiceInfo(ServiceHost host)
            {
                EnLog.WriteEntry(host.Description.ServiceType + "is up and running with these endpoints :");
                foreach (ServiceEndpoint se in host.Description.Endpoints)
                {
                    EnLog.WriteEntry(se.Address.ToString());
                }
            }

            private void HostUnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
            {
                EnLog.WriteEntry(ServiceName + " Inside UnknownMessageReceived.");
            }

            private void HostFaulted(object sender, EventArgs e)
            {
                EnLog.WriteEntry(ServiceName + " - Host Faulted.");
            }

            protected override void OnStop()
            {
                if (serviceHost == null) return;
                serviceHost.Close();
                serviceHost = null;
            }
        }
    }

Configuration

 <system.serviceModel>
    <services>
      <service name="XYC.Service.WCFNetTCPContract.DataService">
        <endpoint
          name="SrcWeb"
          address="sb://data.servicebus.windows.net/App/long-guid"
                  binding="netTcpRelayBinding"
                  contract="XYC.Service.Interface.IService"
                  bindingConfiguration="Hybrid"
                  behaviorConfiguration="sbTokenProvider" />

        <endpoint
          name="AdminServiceEndpoint"
          address="sb://serviceaddress.servicebus.windows.net/Admin/long-guid"
                  binding="netTcpRelayBinding"
                  contract="One234C.Service.AdminInterface.IOne23AdminService"
                  bindingConfiguration="Hybrid"
                  behaviorConfiguration="sbTokenProvider" />
      </service>
    </services>
    <bindings>
      <netTcpRelayBinding>
        <binding name="Hybrid" connectionMode="Hybrid" maxReceivedMessageSize="500000">
          <security mode="None" />
        </binding>
      </netTcpRelayBinding>
    </bindings>
    <behaviors>

      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="False" httpsGetEnabled="False" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="sbTokenProvider">
          <transportClientEndpointBehavior>
            <tokenProvider>
              <sharedSecret issuerName="owner" issuerSecret="xyc" />
            </tokenProvider>
          </transportClientEndpointBehavior>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <extensions>   
    </extensions>
  </system.serviceModel>

这有时会抛出以下异常(我猜它缺乏Windows服务依赖性,我希望在机器连接到Internet并启动sql server服务后启动此服务。)

  1. System.TimeoutException:打开操作未在其中完成 分配超时00:00:00。分配给此的时间 操作可能是较长超时的一部分。 ---&GT; System.TimeoutException:在00:00:00之后打开超时 建立一个运输会议 的net.tcp://xxx.servicebus.windows.net:9351 / src目录/ GUID /。时间 分配给这个操作可能是一个更长的一部分 超时。 ---&GT; System.TimeoutException:连接到via net.tcp://xxx.servicebus.windows.net:9351 / Src / guid / timed out after 00:00:00。连接尝试为0,即1 地址()。检查频道的RemoteAddress并验证 该端点的DNS记录对应于有效的IP 地址。分配给此操作的时间可能是a 超时超时的一部分。在 System.ServiceModel.Channels.SocketConnectionInitiator.CreateTimeoutException(URI uri,TimeSpan超时,IPAddress []地址,Int32
  2. 中的invalidAddressCount,SocketException innerException)
  3. System.ArgumentException:issuerSecret无效。参数名称: 发行人秘密 Microsoft.ServiceBus.SharedSecretTokenProvider.DecodeSecret(字符串 issuerSecret)at Microsoft.ServiceBus.SharedSecretTokenProvider..ctor(字符串 issuerName,String issuerSecret)at Microsoft.ServiceBus.Configuration.TokenProviderElement.CreateTokenProvider() 在 Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement.CreateBehavior() 在 System.ServiceModel.Description.ConfigLoader.LoadBehaviors [T](ServiceModelExtensionCollectionElement 1 behaviorElement, KeyedByTypeCollection 1行为,布尔值 commonBehaviors) System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host,ServiceDescription描述,ServiceElement serviceElement, Action`1 addBaseAddress,Boolean skipHost)at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader,ServiceDescription描述,ServiceElement serviceSection)at System.ServiceModel.ServiceHost.ApplyConfiguration()at System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses)在System.ServiceModel.ServiceHost..ctor(Type serviceType,Uri [] baseAddresses)at One234C.Service.WinServiceHost.One23InsightService.OnStart(字符串[] args)issuerSecret无效。

2 个答案:

答案 0 :(得分:0)

第二个错误意味着您使用的命名空间的颁发者密钥不正确。您可能需要检查以确保使用正确的密钥。

答案 1 :(得分:0)

看起来像是NetTCPRelaybinding实现中的一个错误。我也有同样的问题。我转发Servicebus的部分,我可以看到正在显示的服务但是relaytype是None。