Exemplo n.º 1
0
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="singletonInstance">承载的服务的实例</param>
        public virtual void Start(TServiceContract singletonInstance)
        {
            if (singletonInstance == null)
            {
                throw new ArgumentNullException("singletonInstance");
            }

            if (ServiceHost != null && ServiceHost.State != CommunicationState.Closed)
            {
                throw new InvalidOperationException("The service host has been started.");
            }

            ServiceInstance = singletonInstance;

            ServiceHost = ServiceHostBuilder.GetServiceHost <TServiceContract>(ServiceInstance, ServiceInfo.Binding, ServiceInfo.Name, ServiceInfo.Port, ServiceInfo.Address);

            // add discovery endpoint and behavior
            IDiscoverableService discoverableService = ServiceInstance as IDiscoverableService;

            if (discoverableService != null)
            {
                DiscoverableServiceHelper.AddDiscoveryEndpointToServiceHost(ServiceHost);
                var endpoint = ServiceHost.Description.Endpoints.Find(typeof(TServiceContract));
                DiscoverableServiceHelper.AddDiscoveryBehaviorToServiceEndpoint(endpoint, typeof(TServiceContract));
            }

            ServiceHost.Open();
        }
Exemplo n.º 2
0
        private static async Task DiscoverService(IDiscoverableService service)
        {
            bool discovered = await service.DiscoverAsync();

            if (discovered)
            {
                Messenger.Default.Send(new ServiceDiscoveredMesage(service));
            }
        }
Exemplo n.º 3
0
 public ServiceDiscoveredMesage(IDiscoverableService service)
 {
     Service = service ?? throw new ArgumentNullException(nameof(service));
 }