我正在编写托管在 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 服务依赖性,我希望在机器连接到互联网和 sql 服务器服务启动后启动此服务)。
- System.TimeoutException:打开操作未在分配的 00:00:00 超时内完成。分配给此操作的时间可能是较长超时的一部分。---> System.TimeoutException:在 00:00:00 之后打开超时,同时建立到 net.tcp://xxx.servicebus.windows.net:9351/Src/guid/ 的传输会话。分配给此操作的时间可能是较长超时的一部分。---> System.TimeoutException:通过 net.tcp://xxx.servicebus.windows.net:9351/Src/guid/ 连接到 00:00:00 后超时。对 1 个可用地址中的 0 个进行了连接尝试 ()。检查您的通道的 RemoteAddress 并验证此端点的 DNS 记录是否对应于有效的 IP 地址。分配给此操作的时间可能是较长超时的一部分。在 System.ServiceModel.Channels。
- System.ArgumentException:issuerSecret 无效。参数名称:Microsoft.ServiceBus.SharedSecretTokenProvider.DecodeSecret(String issuerSecret) 在 Microsoft.ServiceBus.SharedSecretTokenProvider..ctor(String issuerName, String issuerSecret) 在 Microsoft.ServiceBus.Configuration.TokenProviderElement.CreateTokenProvider() 在 Microsoft.ServiceBus.Configuration 的 issuerSecret .TransportClientEndpointBehaviorElement.CreateBehavior() 在 System.ServiceModel.Description.ConfigLoader.LoadBehaviors[T](ServiceModelExtensionCollectionElement
1 behaviorElement, KeyedByTypeCollection
1 行为,布尔 commonBehaviors) 在 System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress, Boolean skipHost) 在 System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection) 在 System.ServiceModel.ServiceHost.ApplyConfiguration() 在 System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) 在 System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) 在 One234C.Service.WinServiceHost.One23InsightService .OnStart(String[] args) issuerSecret 无效。