C# 不带配置文件的主机WCF服务
我想托管WCF服务,但不想使用C# 不带配置文件的主机WCF服务,c#,.net,wcf,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
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服务项目,我有以下主要问题:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
服务代码应该放在哪里?您可以将第一个代码块中的代码放在
Service1
的OnStart
功能中
private _myServiceHost;
protected override void OnStart(string[] args)
{
if(_myServiceHost != null)
{
//Close the connection if the service was already opened.
_myServiceHost.Close();
}
// 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.
_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();
}
//Adding a close on OnStop gives you a more graceful shutdown of your service, letting clients finish the work they are currently on
protected override void OnStop()
{
if(_myServiceHost != null)
{
_myServiceHost.Close();
_myServiceHost = null;
}
}
您可以在下面找到一个工作示例:
[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);
我必须检查一下,但我非常确定在VisualStudio中有一个Windows服务项目模板。使用它。请查看我的更新。我想更多的问题是他不知道将
StartService(…
调用放在哪里,而不是在调用中放置什么。是的,你是对的@ScottChamberlain我没有注意。但是完整的代码可能对某人有用:)我需要将它放在哪里,这是安全的?(我宁愿不穿)