public static void Main() { //var helloServiceModel = new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/hello"));//.AddExtensions(new GlobalExceptionHandlerBehaviour(typeof(GlobalExceptionHandler))); var throttlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = Environment.ProcessorCount * 16, MaxConcurrentSessions = (Environment.ProcessorCount * 16) + (Environment.ProcessorCount * 100), MaxConcurrentInstances = Environment.ProcessorCount * 100 }; var helloServiceModel = new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/hello")).AddExtensions(new GlobalExceptionHandlerBehaviour(typeof(GlobalExceptionHandler)), throttlingBehavior); helloServiceModel.OnCreated(host => { host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; host.Authorization.ExternalAuthorizationPolicies = new System.Collections.ObjectModel.ReadOnlyCollection <System.IdentityModel.Policy.IAuthorizationPolicy>(new List <IAuthorizationPolicy>() { new CustomAuthorizationPolicy() }); //var od = host.Description.Endpoints[0].Contract.Operations.Find("Handle"); //var serializerBehavior = od.Behaviors.Find<DataContractSerializerOperationBehavior>(); //if (serializerBehavior == null) //{ // serializerBehavior = new DataContractSerializerOperationBehavior(od); // od.Behaviors.Add(serializerBehavior); //} //serializerBehavior.DataContractResolver = new SharedTypeResolver(); }); var windsorContainer = new WindsorContainer().AddFacility <WcfFacility>(); windsorContainer.Register( Component.For <LoggingCallContextInitializer>(), Component.For <LoggingBehavior>(), Component.For <IConsoleService>().ImplementedBy <ConsoleService>().AsWcfService(new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/console"))), Component.For <IRequestHandlerService>().ImplementedBy <RequestHandlerService>().AsWcfService(new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/requestHandler"))), Component.For <IHelloService>().ImplementedBy <HelloService>().AsWcfService(helloServiceModel) ); var hostFactory = new DefaultServiceHostFactory(windsorContainer.Kernel); var helloHost = hostFactory.CreateServiceHost <IHelloService>(); var consoleHost = hostFactory.CreateServiceHost <IConsoleService>(); var requestHandlerHost = hostFactory.CreateServiceHost <IRequestHandlerService>(); try { Console.ReadLine(); } finally { helloHost.Close(); consoleHost.Close(); requestHandlerHost.Close(); } }
static void Main(string[] args) { var item = new DefaultServiceModel(); Bootstrapping.BootStrapper.Initialize(); ConsoleKey key; do { key = Answer(); } while (key != ConsoleKey.Escape); }
public static void Main() { //var helloServiceModel = new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/hello"));//.AddExtensions(new GlobalExceptionHandlerBehaviour(typeof(GlobalExceptionHandler))); var throttlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = Environment.ProcessorCount * 16, MaxConcurrentSessions = (Environment.ProcessorCount * 16) + (Environment.ProcessorCount * 100), MaxConcurrentInstances = Environment.ProcessorCount * 100 }; var helloServiceModel = new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/hello")).AddExtensions(new GlobalExceptionHandlerBehaviour(typeof(GlobalExceptionHandler)), throttlingBehavior ); helloServiceModel.OnCreated(host => { host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; host.Authorization.ExternalAuthorizationPolicies = new System.Collections.ObjectModel.ReadOnlyCollection<System.IdentityModel.Policy.IAuthorizationPolicy>(new List<IAuthorizationPolicy>() { new CustomAuthorizationPolicy() }); //var od = host.Description.Endpoints[0].Contract.Operations.Find("Handle"); //var serializerBehavior = od.Behaviors.Find<DataContractSerializerOperationBehavior>(); //if (serializerBehavior == null) //{ // serializerBehavior = new DataContractSerializerOperationBehavior(od); // od.Behaviors.Add(serializerBehavior); //} //serializerBehavior.DataContractResolver = new SharedTypeResolver(); }); var windsorContainer = new WindsorContainer().AddFacility<WcfFacility>(); windsorContainer.Register( Component.For<LoggingCallContextInitializer>(), Component.For<LoggingBehavior>(), Component.For<IConsoleService>().ImplementedBy<ConsoleService>().AsWcfService(new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/console"))), Component.For<IRequestHandlerService>().ImplementedBy<RequestHandlerService>().AsWcfService(new DefaultServiceModel(WcfEndpoint.BoundTo(new NetTcpBinding()).At("net.tcp://localhost:9101/requestHandler"))), Component.For<IHelloService>().ImplementedBy<HelloService>().AsWcfService(helloServiceModel) ); var hostFactory = new DefaultServiceHostFactory(windsorContainer.Kernel); var helloHost = hostFactory.CreateServiceHost<IHelloService>(); var consoleHost = hostFactory.CreateServiceHost<IConsoleService>(); var requestHandlerHost = hostFactory.CreateServiceHost<IRequestHandlerService>(); try { Console.ReadLine(); } finally { helloHost.Close(); consoleHost.Close(); requestHandlerHost.Close(); } }
/// <summary> /// Register a WCF service and implementation with the IoC /// </summary> /// <para> /// WCF configuration is finicky. If you want metadata published, you need to use BaseAddresses rather /// than configuring the endpoint with .At() -- which is the interface that most examples use. Also, /// metadata cannot be published over the Net.Tcp transport; it only works over Http. So if you are /// using Net.Tcp and want metadata published then you have to have an Http base address in addition /// to the Net.Tcp address. And in this case, the Http base address and the Net.Tcp address must /// have different port numbers (because you can't have two different transports bound to the /// same port). /// </para> /// <para> /// For self-hosted services, you must *not* use .Hosted() on the service model, as that indicates /// the services lives in IIS/WAS. Also, the service implementation must *not* have a ServiceBehaviour /// attribute, because that will cause .NET to try and create the service host and it demands a /// parameter-less constructor (which we don't have since we're trying to do dependency injection /// via constructor parameters). /// </para> /// <para> /// To register different endpoint behaviours the behaviour must be added to the endpoint instance as an extension (AddExtension). /// Adding the endpoint behaviours to the serviceModel directly fails if the same endpoint behaviour is registered for another service. /// </para> /// <para> /// Every service will be registered with a unique random name because registering 2 services with different (I) interfaces but same implementation (C) /// conflicts with Castle Windsor - it needs a unique name. /// The unique name is never been used to resolve the registered service. It is only needed to generate a unique key for Castle Windsor. /// Resolve the service with the proper interface (I). /// </para> /// </remarks> /// <typeparam name="I">interface for service</typeparam> /// <typeparam name="C">implementation class for service</typeparam> /// <param name="serviceName">service name (used in endpoint URL)</param> /// <param name="http">indicates whether to use the HTTP transport or Net.Tcp</param> /// <param name="streamed">whether the service needs a streamed protocol</param> /// <param name="port">the port number to use for the service</param> /// <param name="mexPort">the port for the mex endpoint (only used if http == false)</param> /// <param name="maxMsg">the max message size configuration parameter for WCF</param> /// <param name="sendTimeout">WCF service timeout to send a message</param> /// <param name="receiveTimeout">WCF service timeout to receive a message</param> /// <param name="useExternalEndpointBehaviour">Optional: Flag to add external endpoint behaviour (<c>true</c>) or the default endpoint behaviour. Default: false</param> public void RegisterWCFServiceImpl <I, C>(string serviceName, bool http, bool streamed, int port, int mexPort, int maxMsg, TimeSpan sendTimeout, TimeSpan receiveTimeout, bool useExternalEndpointBehaviour) where I : class where C : I { Func <int, string, string> GenAddr = GetServiceAddressNetTCP; if (http) { GenAddr = GetServiceAddressHTTP; } var serviceUri = GenAddr(port, serviceName); List <string> baseAddresses = new List <string>(); var endPoint = CreateEndpoint( GetEndpoint(serviceUri, streamed, maxMsg, sendTimeout, receiveTimeout), this.CreateEndpointBehaviour(useExternalEndpointBehaviour), baseAddresses, serviceUri); if (!http) { var mexUri = GetServiceAddressHTTP(mexPort, serviceName); baseAddresses.Add(mexUri); } try { var serviceModel = new DefaultServiceModel() .PublishMetadata(o => o.EnableHttpGet()) .AddBaseAddresses(baseAddresses.ToArray()); serviceModel.AddEndpoints(endPoint); string uniqueRegistrationName = Guid.NewGuid().ToString(); Kernel.Register(Component .For <I>() .ImplementedBy <C>() .AsWcfService(serviceModel) .Named(uniqueRegistrationName)); Logger.InfoFormat("START and REGISTER service '{0}' - listening at [{1}]: Endpoint-Behaviour-Count: {2}, Use-External-EndpointBehaviour = '{3}'.", serviceName, serviceUri, serviceModel.Extensions.Count(), useExternalEndpointBehaviour.ToString()); } catch (Exception ex) { var msg = string.Format("Error: Service registration failed for address = [{0}], Use-External-EndpointBehaviour = '{1}', Ex='{2}'", serviceUri, useExternalEndpointBehaviour.ToString(), ex.ToString()); Logger.Fatal(msg); IoCSetup.ShutdownAndExit(msg, 1); } }