没有配置文件的主机WCF服务

时间:2014-08-22 15:48:02

标签: c# .net wcf

我想托管一个WCF服务,但我不想使用app.config文件,但类似于此:

// 2nd Procedure:
// Use the binding in a service
// Create the Type instances for later use and the URI for 
// the base address.
Type contractType = typeof(ICalculator);
Type serviceType = typeof(Calculator);
Uri baseAddress = new Uri("http://localhost:8036/SecuritySamples/");

// Create the ServiceHost and add an endpoint, then start
// the service.
ServiceHost myServiceHost = new ServiceHost(serviceType, baseAddress);
myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator");

//enable metadata
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
myServiceHost.Description.Behaviors.Add(smb);

myServiceHost.Open();

之后我想添加一个Windows服务项目并托管我的服务。

我应该使用什么项目?我不想要控制台或winforms,我只想要Windows服务

我检查了Windows Service project,我有这个主要内容:

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

我应该把服务代码放在哪里?

2 个答案:

答案 0 :(得分:0)

您可以将第一个代码块中的代码放在OnStart的{​​{1}}函数中。

Service1

答案 1 :(得分:0)

您可以在下面找到一个有效的例子:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = true)]
    public class SMTX_Service : ISMTX_Service
    {
    }
 private void StartService(Uri baseUri, string EndPointName, bool EnableServiceDiscovery)
        {
                var serviceUri = new Uri(baseUri, EndPointName);
                Host = new ServiceHost(typeof(WCF_TEST.SMTX_Service), baseUri);
                Host.Faulted += h_ServiceFaulted;
                Host.Opened += h_ServiceOpened;
                Host.Closed += h_ServiceClosed;
                WSHttpBinding wsHttpBinding = new WSHttpBinding();
                wsHttpBinding.Security.Mode = SecurityMode.Message;
                wsHttpBinding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.TripleDesSha256Rsa15;
                wsHttpBinding.Security.Message.EstablishSecurityContext = true;
                wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
                wsHttpBinding.Security.Message.NegotiateServiceCredential = true;
                wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
                wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                wsHttpBinding.Security.Transport.Realm = "";
                wsHttpBinding.MaxBufferPoolSize = 524288;
                wsHttpBinding.MaxReceivedMessageSize = 4194304;
                ServiceEndpoint SEP = Host.AddServiceEndpoint(typeof(WCF_TEST.ISMTX_Service), wsHttpBinding, EndPointName);
                SEP.Name = EndPointName;
                SEP.Address = new EndpointAddress(serviceUri);
                SEP.Binding = wsHttpBinding;
                ServiceMetadataBehavior SMB = new ServiceMetadataBehavior();
                SMB.HttpGetEnabled = EnableServiceDiscovery;
                Host.Description.Behaviors.Add(SMB);
                ServiceCredentials SC = new ServiceCredentials();
                SC.ServiceCertificate.Certificate = GetCertificate();
                SC.ClientCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
                SC.UseIdentityConfiguration = false;
                SC.ClientCertificate.Certificate = GetCertificate();
                SC.WindowsAuthentication.AllowAnonymousLogons = false;
                SC.WindowsAuthentication.IncludeWindowsGroups = true;
                SC.Peer.PeerAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
                Host.Description.Behaviors.Add(SC);
                Host.Open();
        }

以上示例使用来自证书的证书'存储用于身份验证函数GetCertificate不包括在内。根据需要更改消息算法和协商。 您可以按照以下方式开始服务:

StartService(new Uri("..."), "WCF_NAME", true);