protected override void OnStart(string[] args) { // 只是为了确保安全 if (myHost != null) { myHost.Close(); myHost = null; } //// 创建宿主 //myHost = new ServiceHost(typeof(MathService)); //// 代码中的ABC //Uri address = new Uri("http://localhost:8080/MathServiceLibrary"); //WSHttpBinding binding = new WSHttpBinding(); //Type contract = typeof(IBasicMath); //// 增加终结点 //myHost.AddServiceEndpoint(contract, binding, address); // 创建宿主,并为HTTP绑定URL myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8080/MathServiceLibrary")); // 选择默认的终结点 myHost.AddDefaultEndpoints(); // 打开宿主 myHost.Open(); }
public override void Run() { Trace.TraceInformation("Node started"); var address = string.Format("net.tcp://{0}", (RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["InternalService"] .IPEndpoint)); host = new ServiceHost(typeof(TenantService), new Uri(address)); host.AddDefaultEndpoints(); host.Open(); new Task(() => Parallel.For(0, TenantCount, i => { var tenant = new TenantEnvironment(i); TenantStatuses[tenant.TenantId] = tenant; while (true) { TenantStatuses[tenant.TenantId].TenantStatus = TenantStatus.Unknown; tenant.Run(); Trace.TraceInformation("Tenant {0} crashed", tenant.TenantNumber); Thread.Sleep(1000); } })).Start(); // This is a sample worker implementation. Replace with your logic. Trace.TraceInformation("WorkerRole1 entry point called", "Information"); var timer = new System.Timers.Timer(5000); timer.Elapsed += timer_Elapsed; timer.Start(); while (true) { Thread.Sleep(10000); Trace.TraceInformation("Working", "Information"); } }
static void Main(string[] args) { string Url = "http://localhost:8000/"; Binding binding = new BasicHttpBinding(); ServiceHost host = new ServiceHost(typeof(Service)); host.AddServiceEndpoint(typeof(MyServer.IService), binding, Url); host.AddDefaultEndpoints(); host.Open(); ChannelFactory <MyClient.IService> fac = new ChannelFactory <MyClient.IService>(binding); fac.Open(); MyClient.IService proxy = fac.CreateChannel(new EndpointAddress(Url)); string d = proxy.Method("String from client."); fac.Close(); host.Close(); Console.WriteLine("Result after calling \n " + d); Console.ReadLine(); Console.ReadLine(); }
protected override void OnStart(string[] args) { ////Just to be safe //myHost?.Close(); ////Create the service //myHost = new ServiceHost(typeof(MathService)); ////The ABCs in Code! //Uri address = new Uri("http://localhost:8080/MathServiceLibrary"); //WSHttpBinding binding = new WSHttpBinding(); //Type contract = typeof(IBasicMath); ////Add the endpoint //myHost.AddServiceEndpoint(contract, binding, address); ////Open the host //myHost.Open(); //Using the defaults myHost?.Close(); // Create the host and specify a URL for an HTTP binding. myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8080/MathServiceLibrary")); myHost.AddDefaultEndpoints(); // Open the host. myHost.Open(); }
static void Main(string[] args) { //create the base addresses for each protocol Uri http = new Uri("http://localhost:9999"); Uri tcp = new Uri("net.tcp://localhost:8888"); Uri[] baseaddress = { http, tcp }; ServiceHost svc = new ServiceHost(typeof(SimpleMath), baseaddress);//service host needs to know which methods are hosted try { svc.AddDefaultEndpoints(); //enable Metadata publishing ServiceMetadataBehavior metadata = new ServiceMetadataBehavior(); metadata.HttpGetEnabled = true; metadata.HttpGetUrl = new Uri("http://localhost:1111"); svc.Description.Behaviors.Add(metadata); //start connection svc.Open(); Console.WriteLine("Server Started....."); Console.ReadKey(); //shut down the server svc.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }
private static void Main(string[] args) { Console.WriteLine(@"Preparing the database..."); SqlServerShared.RecreateDatabase(); // Start self-hosted WCF server using (var host = new ServiceHost( new InfoCarrierService(), new Uri($"http://{WcfShared.BaseUrl}/{WcfShared.ServiceName}"))) { host.AddDefaultEndpoints(); foreach (var ep in host.Description.Endpoints) { ep.Binding = new BasicHttpBinding { MaxReceivedMessageSize = WcfShared.MaxReceivedMessageSize }; } host.Open(); foreach (Uri addr in host.BaseAddresses) { Console.WriteLine($@"The service is ready at {addr}"); } Console.WriteLine(@"Press any key to stop."); Console.ReadKey(); host.Close(); } }
protected override void OnStart(string[] args) { // to be safe if (myHost != null) { myHost.Close(); } // create host myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8087/MathServiceLibrary")); // the ABC's in code (address, binding, contract) //Uri address = new Uri("http://localhost:8087/MathServiceLibrary"); //WSHttpBinding binding = new WSHttpBinding(); //Type contract = typeof(IBasicMath); // add this endpoint //myHost.AddServiceEndpoint(contract, binding, address); // Opt in for default endpoints, rather than the manual wire-up above myHost.AddDefaultEndpoints(); // open the host myHost.Open(); }
static void Main(string[] args) { //没有通过配置文件的方式启动程序 //提供服务的主机 using (ServiceHost host = new ServiceHost(typeof(BookService))) { host.AddDefaultEndpoints(); //添加终结点 host.AddServiceEndpoint(typeof(IBookService), new WSHttpBinding(), "http://127.0.0.1:9658/BookService"); //判断控制服务元数据和相关信息的发布 if (host.Description.Behaviors.Find <ServiceMetadataBehavior>() == null) { //创建一个控制服务 ServiceMetadataBehavior metdata = new ServiceMetadataBehavior(); //控制服务元数据和相关信息的发布时使用Http/Get进行检索 metdata.HttpGetEnabled = true; //http/Get请求的位置 metdata.HttpGetUrl = new Uri("http://127.0.0.1:9658/BookService/wcf"); //添加到主机中 host.Description.Behaviors.Add(metdata); } //添加一个事件 host.Opened += delegate { Console.WriteLine("BookService已启动,按任意键终止服务!"); }; //启动服务 host.Open(); Console.ReadLine(); } }
static void Main(string[] args) { var bank = new Bank("ICICI", 12) { AccountRepository = new BasicAccountRepository(@"c:\temp\accounts.db") }; var host = new ServiceHost(bank, //new Uri("net.tcp://localhost:9000"), new Uri("http://localhost:8000") ); host.AddDefaultEndpoints(); host.Open(); foreach (var ep in host.Description.Endpoints) { Console.WriteLine(ep.Name); Console.WriteLine(ep.Address.Uri); Console.WriteLine(ep.Contract.ContractType); Console.WriteLine(ep.Binding); Console.WriteLine(); } Console.WriteLine("Hit Enter to Exit"); Console.ReadLine(); Console.WriteLine("Service Shutdown!"); }
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) { var host = new ServiceHost(Type.GetType(constructorString), baseAddresses); host.AddDefaultEndpoints(); return(host); }
static void AddDefaultAndHttpMexEndPoints(ServiceHost host) { ServiceMetadataBehavior serviceMetadataBehavior = (ServiceMetadataBehavior)host.Description.Behaviors[typeof(ServiceMetadataBehavior)]; serviceMetadataBehavior.HttpGetEnabled = true; host.AddDefaultEndpoints(); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); }
/* * protected override void OnStart(string[] args) { //not work yet * if(myHost != null) { * myHost.Close(); * myHost = null; * } * * myHost = new ServiceHost(typeof(MathService)); * * Uri address = new Uri("http://localhost:8080/MathServiceLib"); * WSHttpBinding binding = new WSHttpBinding(); * Type contract = typeof(IBasicMath); * * myHost.AddServiceEndpoint(contract, binding, address); * * myHost.Open(); * } */ protected override void OnStart(string[] args) { if (myHost != null) { myHost.Close(); } myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8080/MathServiceLib")); myHost.AddDefaultEndpoints(); myHost.Open(); }
protected override void OnStart(string[] args) { if (service1Host != null) { service1Host.Close(); } service1Host = new ServiceHost(typeof(Service1), new Uri("http://localhost:62415/")); service1Host.AddDefaultEndpoints(); service1Host.Open(); EventLog.WriteEntry("ServiceWCFWindows", "Service WCF démarré", EventLogEntryType.Information); }
private ServiceHost StartServiceEndpoint(object serviceInstance, ServiceEndpointAddress serviceAddress) { AnchorUtil.ThrowOnNullArgument(serviceInstance, "serviceInstance"); ServiceHost result; try { ServiceHost serviceHost = new ServiceHost(serviceInstance, serviceAddress.GetBaseUris()); serviceHost.AddDefaultEndpoints(); this.logger.Log(MigrationEventType.Verbose, "Opening service host for {0}, with service type {1} and namespace {2}.", new object[] { serviceHost.Description.Name, serviceHost.Description.ServiceType.FullName, serviceHost.Description.Namespace }); ServiceDebugBehavior serviceDebugBehavior = serviceHost.Description.Behaviors.Find <ServiceDebugBehavior>(); if (serviceDebugBehavior == null) { serviceDebugBehavior = new ServiceDebugBehavior(); serviceHost.Description.Behaviors.Add(serviceDebugBehavior); } serviceDebugBehavior.IncludeExceptionDetailInFaults = true; foreach (System.ServiceModel.Description.ServiceEndpoint serviceEndpoint in serviceHost.Description.Endpoints) { NetTcpBinding netTcpBinding = serviceEndpoint.Binding as NetTcpBinding; if (netTcpBinding != null) { netTcpBinding.MaxReceivedMessageSize = 10485760L; netTcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10.0); netTcpBinding.SendTimeout = TimeSpan.FromMinutes(10.0); } this.logger.LogVerbose("Using binging: {0} ({1})", new object[] { serviceEndpoint.Binding.Name, serviceEndpoint.Binding.MessageVersion }); LoadBalanceUtils.UpdateAndLogServiceEndpoint(this.logger, serviceEndpoint); } serviceHost.Open(); result = serviceHost; } catch (Exception exception) { this.logger.LogError(exception, "Failed to register endpoint for service {0}", new object[] { serviceInstance.GetType().Name }); throw; } return(result); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses); //note: these endpoints will not exist yet, if you are relying on the svc system to generate your endpoints for you // calling host.AddDefaultEndpoints provides you the endpoints you need to add the behavior we need. var endpoints = host.AddDefaultEndpoints(); foreach (var endpoint in endpoints) { endpoint.Behaviors.Add(new WcfUnkownUriBehavior()); } return(host); }
protected override void OnStart(string[] args) { if (_myHost != null) { _myHost.Close(); } // Create the host and specify a URL for an HTTP binding. _myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8080/MathServiceLibrary")); // Opt in for the default endpoints! _myHost.AddDefaultEndpoints(); // Open the host. _myHost.Open(); }
static void Main(string[] args) { var baseAddress = new Uri("http://localhost:8080/hello"); // Create the ServiceHost. using (var host = new ServiceHost(typeof(HelloWorldService.HelloWorldService), baseAddress)) { // Enable metadata publishing. var smb = new ServiceMetadataBehavior { HttpGetEnabled = true }; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; host.Description.Behaviors.Add(smb); //explicitly add an endpoint for MetaDataExchange host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mexBindingHttp"); //explicitly add some endpoints host.AddDefaultEndpoints(); // Open the ServiceHost to start listening for messages. If // no endpoints are explicitly configured, the runtime will create // one endpoint per base address for each service contract implemented // by the service. host.Open(); Console.WriteLine("The service is ready at {0}", baseAddress); //directly access an endpoint for interaction: var uri = host.BaseAddresses[0]; var binding = new BasicHttpBinding(BasicHttpSecurityMode.None); var endpoint = new EndpointAddress(uri); var factory = new ChannelFactory <HelloWorldService.IHelloWorldService>(binding, endpoint); var channel = factory.CreateChannel(); var response = channel.GetStandardGreeting(); Console.WriteLine("Press <Enter> to stop the service."); Console.ReadLine(); // Close the ServiceHost. host.Close(); } //test using wcftestclient.exe (developer command prompt) }
protected override void OnStart(string[] args) { _myHost?.Close(); // _myHost = new ServiceHost(typeof(MathServiceLibrary.MathService)); // var address = new Uri("http://localhost:8080/MathServiceLibrary"); // var binding = new WSHttpBinding(); // var contract = typeof(IBasicMath); // _myHost.AddServiceEndpoint(contract, binding, address); var address = new Uri("http://localhost:8080/MathServiceLibrary"); _myHost = new ServiceHost(typeof(MathServiceLibrary.MathService), address); _myHost.AddDefaultEndpoints(); _myHost.Open(); }
static void Main(string[] args) { if ((args.Length > 0 && args[0] == "seed")) { //create me 20 policies where the minimm age is 25 // var m25Policies = from i in Enumerable.Range(1, 20) // select new InsurancePolicy() // { // PolicyName = "Minimum 25 Policy " + i, // BasePrice = 10M + Convert.ToDecimal(i/10), // MaximumAge = 25 // }; // //create me 15 policies wehre the minimum age is 30 //var m30Policies = from i in Enumerable.Range(1, 15) // select new InsurancePolicy() // { // PolicyName = "Minimum 30 Policy " + i, // BasePrice = 10M + Convert.ToDecimal(i/10), // MaximumAge = 30 // }; //and last but not least a bunch of legacy data with no age limit var noAgePolicies = from i in Enumerable.Range(1, 10000000) select new InsurancePolicy() { PolicyName = "No Age Policy " + i, BasePrice = 10M + Convert.ToDecimal(i / 10), }; var policiesToInsert = noAgePolicies; var seeder = new DataSeeder(); seeder.InsertRows(); } else { _host = new ServiceHost(typeof(InsuranceQuoteService), new Uri("http://localhost:8088/ins")); _host.AddDefaultEndpoints(); _host.Open(); Console.WriteLine("Listening"); Console.ReadLine(); } }
protected override void OnStart(string[] args) { //Just to be really safe if (_myHost != null) { _myHost.Close(); _myHost = null; } //Create the host Ns specify a URL for an HTTP binding. _myHost = new ServiceHost(typeof(MathService), new Uri("http://localhost:8080/MathServiceLibrary")); //The ABC's in code! _myHost.AddDefaultEndpoints(); //Open the host _myHost.Open(); }
static void Main(string[] args) { Type serviceType = typeof(AccountService.AccountService); Uri serviceUri = new Uri("http://localhost:8080/"); ServiceHost host = new ServiceHost(serviceType, serviceUri); host.AddDefaultEndpoints(); ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(behavior); host.AddServiceEndpoint( typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "http://localhost:8080/mex"); host.Open(); Console.WriteLine("Server started. Press any key to exit."); Console.ReadKey(); }
static void Main() { Uri uri = new Uri(@"http://localhost:8001"); ServiceHost host = new ServiceHost(typeof(Service1), uri); ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find <ServiceMetadataBehavior>(); if (metadataBehavior == null) { metadataBehavior = new ServiceMetadataBehavior(); metadataBehavior.HttpGetEnabled = true; host.Description.Behaviors.Add(metadataBehavior); } host.AddDefaultEndpoints(); host.Open(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); host.Close(); }
static void Main(string[] args) { try { //IntPtr hwnd; //hwnd = GetConsoleWindow(); //ShowWindow(hwnd, SW_HIDE); using (var serviceHost = new ServiceHost(typeof(FileTransfer))) { serviceHost.AddDefaultEndpoints(); serviceHost.Open(); Console.WriteLine("Client Started.."); Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadKey(); } }
static void Main(string[] args) { string urlString = "http://localhost:8081/test"; using (var host = new ServiceHost(typeof(Service), new Uri(urlString))) { host.AddDefaultEndpoints(); host.Open(); var obj = new Class1 { PropA = "A", PropB = "B" }; var header = AddressHeader.CreateAddressHeader("Class1", "Class1", obj); var binding = new BasicHttpBinding(); var address = new EndpointAddress(new Uri(urlString), header); var channel = ChannelFactory <IService> .CreateChannel(binding, address); channel.DoWork(); Console.ReadLine(); } }
/// <summary> /// Creates a new <see cref="ServiceHost"/> from the URI. /// </summary> /// <param name="serviceType">Specifies the type of WCF service to host.</param> /// <param name="baseAddresses">An array of base addresses for the service.</param> /// <returns>New <see cref="ServiceHost"/>.</returns> protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { #if MONO throw new NotSupportedException("Not supported under Mono."); #else // Check security requirement. bool integratedSecurity = (Service.GetAuthenticationSchemes(baseAddresses[0]) & AuthenticationSchemes.Anonymous) != AuthenticationSchemes.Anonymous; // Create service host. ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses); // Enable meta-data publishing. if (m_publishMetadata) { ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find <ServiceMetadataBehavior>(); if (metadataBehavior == null) { metadataBehavior = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(metadataBehavior); } metadataBehavior.HttpGetEnabled = true; } // Enable security on the service. if (!m_disableSecurity) { ServiceAuthorizationBehavior authorizationBehavior = host.Description.Behaviors.Find <ServiceAuthorizationBehavior>(); if (authorizationBehavior == null) { authorizationBehavior = new ServiceAuthorizationBehavior(); host.Description.Behaviors.Add(authorizationBehavior); } authorizationBehavior.PrincipalPermissionMode = PrincipalPermissionMode.Custom; List <IAuthorizationPolicy> policies = new List <IAuthorizationPolicy>(); policies.Add((IAuthorizationPolicy)Activator.CreateInstance(m_authorizationPolicy)); authorizationBehavior.ExternalAuthorizationPolicies = policies.AsReadOnly(); } // Create endpoint and configure security. (Not supported on Mono) host.AddDefaultEndpoints(); if (string.IsNullOrEmpty(m_protocol)) { // Use the default endpoint. foreach (ServiceEndpoint endpoint in host.Description.Endpoints) { BasicHttpBinding basicBinding = endpoint.Binding as BasicHttpBinding; if (basicBinding != null) { // Default endpoint uses BasicHttpBinding. if (integratedSecurity) { // Enable security. basicBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; basicBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; } else { // Disable security. basicBinding.Security.Mode = BasicHttpSecurityMode.None; basicBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; } foreach (IEndpointBehavior behavior in m_endpointBehaviors ?? new List <IEndpointBehavior>()) { endpoint.Behaviors.Add(behavior); } } } } else { // Create endpoint using the specifics. host.Description.Endpoints.Clear(); Binding serviceBinding; ServiceEndpoint serviceEndpoint; serviceBinding = Service.CreateServiceBinding(ref m_protocol, integratedSecurity); if (serviceBinding != null) { // Binding created for the endpoint. Type contract = Service.GetServiceContract(serviceType); if (!string.IsNullOrEmpty(m_address)) { serviceEndpoint = host.AddServiceEndpoint(contract, serviceBinding, m_address); } else { serviceEndpoint = host.AddServiceEndpoint(contract, serviceBinding, string.Empty); } // Special handling for REST endpoint. if (serviceBinding is WebHttpBinding) { WebHttpBehavior restBehavior = new WebHttpBehavior(); //#if !MONO if (m_publishMetadata) { restBehavior.HelpEnabled = true; } //#endif serviceEndpoint.Behaviors.Add(restBehavior); serviceEndpoint.Behaviors.Add(new FormatSpecificationBehavior()); } foreach (IEndpointBehavior behavior in m_endpointBehaviors ?? new List <IEndpointBehavior>()) { serviceEndpoint.Behaviors.Add(behavior); } } } foreach (var behavior in ServiceBehaviors ?? new List <IServiceBehavior>()) { host.Description.Behaviors.Add(behavior); } return(host); #endif }