我正在编写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
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无效。答案 0 :(得分:0)
第二个错误意味着您使用的命名空间的颁发者密钥不正确。您可能需要检查以确保使用正确的密钥。
答案 1 :(得分:0)
看起来像是NetTCPRelaybinding实现中的一个错误。我也有同样的问题。我转发Servicebus的部分,我可以看到正在显示的服务但是relaytype是None。