本文整理汇总了C#中System.ServiceModel.ServiceHost.Close方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceHost.Close方法的具体用法?C# ServiceHost.Close怎么用?C# ServiceHost.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceModel.ServiceHost
的用法示例。
在下文中一共展示了ServiceHost.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldSetShieldingWithNonIncludeExceptionDetailInFaults
public void ShouldSetShieldingWithNonIncludeExceptionDetailInFaults()
{
// create a mock service and its endpoint.
Uri serviceUri = new Uri("http://tests:30003");
ServiceHost host = new ServiceHost(typeof(MockService), serviceUri);
host.AddServiceEndpoint(typeof(IMockService), new WSHttpBinding(), serviceUri);
host.Open();
try
{
// check that we have no ErrorHandler loaded into each channel that
// has IncludeExceptionDetailInFaults turned off.
foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
{
Assert.AreEqual(0, dispatcher.ErrorHandlers.Count);
Assert.IsFalse(dispatcher.IncludeExceptionDetailInFaults);
}
ExceptionShieldingBehavior behavior = new ExceptionShieldingBehavior();
behavior.ApplyDispatchBehavior(null, host);
// check that the ExceptionShieldingErrorHandler was assigned to each channel
foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
{
Assert.AreEqual(1, dispatcher.ErrorHandlers.Count);
Assert.IsTrue(dispatcher.ErrorHandlers[0].GetType().IsAssignableFrom(typeof(ExceptionShieldingErrorHandler)));
}
}
finally
{
if (host.State == CommunicationState.Opened)
{
host.Close();
}
}
}
示例2: StartServer
/// <summary>
/// Start the server
/// </summary>
/// <param name="address">the address that the server binds to</param>
static void StartServer(string address)
{
ServiceHost host = null;
try
{
NetTcpBinding tcpBinding = new NetTcpBinding();
//set the maximum message size
tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue;
tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue;
//start the service
host = new ServiceHost(typeof(ATCMasterControllerImpl));
host.AddServiceEndpoint(typeof(IATCMasterController), tcpBinding, "net.tcp://" + address);
host.Open();
System.Console.WriteLine("Press Enter to exit");
//keep server running until enter is pressed
System.Console.ReadLine();
//exit
host.Close();
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
if (host != null)
host.Close();
StartServer(address);
}
}
示例3: Main
static void Main(string[] args)
{
ServiceHost condititionHost=null;
ServiceHost moldpartInfoHost=null;
ServiceHost storageHost=null;
ServiceHost smartDeviceHost = null;
ServiceHost restHost = null;
try
{
condititionHost = new ServiceHost(typeof(ConditionService));
moldpartInfoHost = new ServiceHost(typeof(MoldPartInfoService));
storageHost = new ServiceHost(typeof(StorageManageService));
smartDeviceHost = new ServiceHost(typeof(SmartDeviceApi));
restHost = new ServiceHost(typeof(RestService));
condititionHost.Open();
moldpartInfoHost.Open();
storageHost.Open();
smartDeviceHost.Open();
restHost.Open();
Console.WriteLine("服务已经启动,请保持其运行...");
Console.WriteLine("退出请按 Q ,并回车");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (condititionHost != null && condititionHost.State == CommunicationState.Opened)
condititionHost.Close();
if (moldpartInfoHost != null && moldpartInfoHost.State == CommunicationState.Opened)
moldpartInfoHost.Close();
if (storageHost != null && storageHost.State == CommunicationState.Opened)
storageHost.Close();
if (smartDeviceHost != null && smartDeviceHost.State == CommunicationState.Opened)
smartDeviceHost.Close();
if (restHost != null && restHost.State == CommunicationState.Opened)
restHost.Close();
}
string quit=Console.ReadLine();
while (true)
{
if (quit.Equals("Q"))
{
if (condititionHost != null && condititionHost.State == CommunicationState.Opened)
condititionHost.Close();
if (moldpartInfoHost != null && moldpartInfoHost.State == CommunicationState.Opened)
moldpartInfoHost.Close();
if (storageHost != null && storageHost.State == CommunicationState.Opened)
storageHost.Close();
if (smartDeviceHost != null && smartDeviceHost.State == CommunicationState.Opened)
smartDeviceHost.Close();
if (restHost != null && restHost.State == CommunicationState.Opened)
restHost.Close();
break;
}
quit = Console.ReadLine();
}
}
示例4: RunServer
private void RunServer()
{
try
{
using (ServiceHost host = new ServiceHost(typeof(WcfService)))
{
try
{
ServiceDescription serviceDesciption = host.Description;
foreach (ServiceEndpoint endpoint in serviceDesciption.Endpoints)
{
string properties = "";
ConsoleColor oldColour = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
properties = "Endpoint - address: " + endpoint.Address + "\n";
properties += " - binding name:\t\t" + endpoint.Binding.Name + "\n";
properties += " - contract name:\t\t" + endpoint.Contract.Name + "\n";
Console.WriteLine("\n" + properties);
Console.WriteLine();
Console.ForegroundColor = oldColour;
}
host.Open();
Console.WriteLine("Server is running at " + DateTime.Now.ToString());
Console.ReadLine();
host.Close();
Console.WriteLine("Host is closed. Server is shutting down.");
}
catch (Exception e)
{
Console.WriteLine("Fatal error: \n" + e.Message);
try
{
if (host != null)
host.Close();
}
catch
{
Console.WriteLine("Fatal error while closing host: \n" + e.Message);
}
Console.WriteLine("Press Enter to close application");
Console.ReadLine();
}
}
}
catch (Exception e)
{
Console.WriteLine("Fatal error while creating new ServiceHost: \n" + e.Message);
Console.WriteLine("Press Enter to close application");
Console.ReadLine();
}
}
示例5: Main
static void Main(string[] args)
{
//BaseAddress
Uri baseAddress = new Uri("http://localhost:8080/SelfService.HarryPotter");
//ServiceHost
ServiceHost selfServiceHost = new ServiceHost(typeof(HarryPotterBooksCheckerService), baseAddress);
try
{
//ABC-Endpoint
selfServiceHost.AddServiceEndpoint(typeof(IHarryPotterBooks), new WSHttpBinding(), "HarryPotterCheckerService");
ServiceMetadataBehavior smBehavior = new ServiceMetadataBehavior {HttpGetEnabled = true};
selfServiceHost.Description.Behaviors.Add(smBehavior);
selfServiceHost.Open();
Console.WriteLine("Tjänsten är öppen");
Console.ReadKey();
}
catch (CommunicationException ex)
{
selfServiceHost.Close();
Console.WriteLine($"Ett kommunikationsfel har uppstått.: {ex.Message}");
Console.ReadKey();
throw;
}
}
示例6: Main
static void Main(string[] args)
{
//NetTcpBinding bind = new NetTcpBinding();
//Uri uri = new Uri(ConfigurationManager.AppSettings["baseAddress"]);
//ServiceHost host = new ServiceHost(typeof(QQServer.ChatService), uri);
//if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
//{
// BindingElement metaElement = new TcpTransportBindingElement();
// CustomBinding metaBind = new CustomBinding(metaElement);
// host.Description.Behaviors.Add(new System.ServiceModel.Description.ServiceMetadataBehavior());
// host.AddServiceEndpoint(typeof(System.ServiceModel.Description.IMetadataExchange), metaBind, "MEX");
//}
//host.Open();
//Console.WriteLine("Chat service start to listen: endpoint {0}", uri.ToString());
//Console.WriteLine("Press Enter to stop listen...");
//Console.ReadLine();
//host.Abort();
//host.Close();
using(ServiceHost host = new ServiceHost(typeof(QQServer.ChatService)))
{
host.Open();
Console.WriteLine("Chat service start to listen: endpoint {0}", @"http://localhost:8732/QQServer/ChatService/");
Console.WriteLine("Press Enter to stop listen...");
Console.ReadLine();
host.Close();
}
}
示例7: Main
static void Main()
{
if(MessageQueue.Exists(@".\private$\MyPersistentSubscriberQueue1") == false)
{
MessageQueue.Create(@".\private$\MyPersistentSubscriberQueue1",true);
}
if(MessageQueue.Exists(@".\private$\MyPersistentSubscriberQueue2") == false)
{
MessageQueue.Create(@".\private$\MyPersistentSubscriberQueue2",true);
}
if(MessageQueue.Exists(@".\private$\MyPersistentSubscriberQueue3") == false)
{
MessageQueue.Create(@".\private$\MyPersistentSubscriberQueue3",true);
}
ServiceHost host1 = new ServiceHost(typeof(MyPersistentSubscriber1),new Uri("http://localhost:7000/"));
host1.Open();
ServiceHost host2 = new ServiceHost(typeof(MyPersistentSubscriber2),new Uri("http://localhost:8000/"));
host2.Open();
ServiceHost host3 = new ServiceHost(typeof(MyPersistentSubscriber3),new Uri("http://localhost:9000/"));
host3.Open();
Application.Run(new HostForm());
host1.Close();
host2.Close();
host3.Close();
}
示例8: Main
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/GlydeServiceModelExamples/Service");
ServiceHost selfHost = new ServiceHost(typeof(StringManipulatorService), baseAddress);
try
{
selfHost.AddServiceEndpoint(
typeof(IStringManipulator),
new WSHttpBinding(),
"StringManipulatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
示例9: Main
static void Main(string[] args)
{
// Create binding for the service endpoint.
CustomBinding amqpBinding = new CustomBinding();
amqpBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
amqpBinding.Elements.Add(new AmqpTransportBindingElement());
// Create ServiceHost.
ServiceHost serviceHost = new ServiceHost(typeof(HelloService), new Uri[] { new Uri("http://localhost:8080/HelloService2") });
// Add behavior for our MEX endpoint.
ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
mexBehavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(mexBehavior);
// Add MEX endpoint.
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");
// Add AMQP endpoint.
Uri amqpUri = new Uri("amqp:news");
serviceHost.AddServiceEndpoint(typeof(IHelloService), amqpBinding, amqpUri.ToString());
serviceHost.Open();
Console.WriteLine();
Console.WriteLine("The consumer is now listening on the queue \"news\".");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
if (serviceHost.State != CommunicationState.Faulted)
{
serviceHost.Close();
}
}
示例10: Main
static void Main(string[] args)
{
Uri address = new Uri("http://localhost:8080/Lab2.Service.Age");
ServiceHost serviceHost = new ServiceHost(typeof(Days), address);
try
{
serviceHost.AddServiceEndpoint(typeof(IDays),
new WSHttpBinding(),
"Days");
ServiceMetadataBehavior smBehavior = new ServiceMetadataBehavior();
smBehavior.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smBehavior);
serviceHost.Open();
Console.WriteLine("Tjänsten är öppen!");
Console.WriteLine("Tryck enter för att avsluta");
Console.ReadLine();
}
catch (CommunicationException ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
throw;
}
finally
{
serviceHost.Close();
}
}
示例11: Main
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ServiceA));
host.Open();
ServiceEndpointCollection endpoints = host.Description.Endpoints;
foreach (ServiceEndpoint item in endpoints)
{
Console.WriteLine(item.Address);
}
ServiceHost host2 = new ServiceHost(typeof(ServiceA2));
host2.Open();
endpoints = host2.Description.Endpoints;
foreach (ServiceEndpoint item in endpoints)
{
Console.WriteLine(item.Address);
}
Console.WriteLine();
Console.WriteLine("Please press Enter to terminate the Host");
Console.ReadLine();
host.Close();
}
示例12: Main
static void Main(string[] args)
{
// Create service host
ServiceHost sh = new ServiceHost(typeof(CalculatorService));
// Setting the certificateValidationMode to PeerOrChainTrust means that if the certificate
// is in the user's Trusted People store, then it will be trusted without performing a
// validation of the certificate's issuer chain. This setting is used here for convenience so that the
// sample can be run without having to have certificates issued by a certificate authority (CA).
// This setting is less secure than the default, ChainTrust. The security implications of this
// setting should be carefully considered before using PeerOrChainTrust in production code.
sh.Credentials.IssuedTokenAuthentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust;
sh.Open();
try
{
foreach (ChannelDispatcher cd in sh.ChannelDispatchers)
foreach (EndpointDispatcher ed in cd.Endpoints)
Console.WriteLine("Service listening at {0}", ed.EndpointAddress.Uri);
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
}
finally
{
sh.Close();
}
}
示例13: Main
static void Main(string[] args)
{
using (var host = new ServiceHost(typeof(RExService.RExService) ))//, new Uri("http://localhost:5555")))
{
try
{
/*
host.AddServiceEndpoint(typeof(IRExService), new WSHttpBinding(), "");
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
*/
host.Open();
Console.WriteLine("Service started at {0}", host.BaseAddresses.First());
Console.WriteLine("Press <ENTER> to terminate...");
Console.WriteLine();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine();
}
finally
{
host.Close();
}
}
}
示例14: Main
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
示例15: RunForm
public void RunForm()
{
ServiceHost host = new ServiceHost(typeof(Service.StationService));
host.Open();
Application.Run(new FormHidden());
host.Close();
}