Windows服务问题

时间:2017-08-23 09:05:39

标签: c# windows wcf service

我花了很多时间研究我的问题,但没有成功。 关键是有一个服务必须打开端口(使用证书)。 如果我以管理员身份运行文件Host.exe,一切正常。 我想把它变成一种服务。创建它之后,尝试运行,几秒钟之后就会出现一个问题:

    Exclusion Information: System.InvalidOperationException
   in System.ServiceModel.Security.SecurityUtils.GetCertificateFromStoreCore(System.Security.Cryptography.X509Certificates.StoreName, System.Security.Cryptography.X509Certificates.StoreLocation, System.Security.Cryptography.X509Certificates.X509FindType, System.Object, System.ServiceModel.EndpointAddress, Boolean)
   in System.ServiceModel.Security.SecurityUtils.GetCertificateFromStore(System.Security.Cryptography.X509Certificates.StoreName, System.Security.Cryptography.X509Certificates.StoreLocation, System.Security.Cryptography.X509Certificates.X509FindType, System.Object, System.ServiceModel.EndpointAddress)
   in System.ServiceModel.Security.X509CertificateRecipientServiceCredential.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation, System.Security.Cryptography.X509Certificates.StoreName, System.Security.Cryptography.X509Certificates.X509FindType, System.Object)
   in System.ServiceModel.Configuration.X509RecipientCertificateServiceElement.ApplyConfiguration(System.ServiceModel.Security.X509CertificateRecipientServiceCredential)
   in System.ServiceModel.Configuration.ServiceCredentialsElement.ApplyConfiguration(System.ServiceModel.Description.ServiceCredentials)
   in System.ServiceModel.Configuration.ServiceCredentialsElement.CreateBehavior()
   in System.ServiceModel.Description.ConfigLoader.LoadBehaviors[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1<System.ServiceModel.Configuration.BehaviorExtensionElement>, System.Collections.Generic.KeyedByTypeCollection`1<System.__Canon>, Boolean)
   in System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(System.ServiceModel.ServiceHostBase, System.ServiceModel.Description.ServiceDescription, System.ServiceModel.Configuration.ServiceElement, System.Action`1<System.Uri>, Boolean)
   in System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(System.ServiceModel.Description.ConfigLoader, System.ServiceModel.Description.ServiceDescription, System.ServiceModel.Configuration.ServiceElement)
   in System.ServiceModel.ServiceHostBase.ApplyConfiguration()
   in System.ServiceModel.ServiceHost.ApplyConfiguration()
   in System.ServiceModel.ServiceHostBase.InitializeDescription(System.ServiceModel.UriSchemeKeyedCollection)
   in System.ServiceModel.ServiceHost.InitializeDescription(System.Type, System.ServiceModel.UriSchemeKeyedCollection)
   in System.ServiceModel.ServiceHost..ctor(System.Type, System.Uri[])
   in Host.PasportHost.OpenPort()
   in System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   in System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   in System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   in System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   in System.Threading.ThreadHelper.ThreadStart()

你有什么想法来解决这个问题吗?

非常感谢。

static void Main(string[] args)
        {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new PasportHost()
                }; ServiceBase.Run(ServicesToRun);

         }

public partial class PasportHost : ServiceBase
    {
        private readonly Thread myThread;

        public PasportHost()
        {
            InitializeComponent();
            myThread = new Thread(OpenPort);

            this.CanStop = true;
        }

        private void OpenPort()
        {
            Type Service = typeof(Services);
            ServiceHost host = new ServiceHost(Service);
            host.Open();
        }

        protected override void OnStart(string[] args)
        {
            Start();
        }

        public void Start()
        {
            myThread.Start();
        }

        protected override void OnStop()
        {
            ClosePort();
        }

        private void ClosePort()
        {
            Type Service = typeof(Services);
            ServiceHost host = new ServiceHost(Service);
            host.Close();
        }
    }

0 个答案:

没有答案