當前位置: 首頁>>代碼示例>>C#>>正文


C# HttpSelfHostServer.OpenAsync方法代碼示例

本文整理匯總了C#中System.Web.Http.SelfHost.HttpSelfHostServer.OpenAsync方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpSelfHostServer.OpenAsync方法的具體用法?C# HttpSelfHostServer.OpenAsync怎麽用?C# HttpSelfHostServer.OpenAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Web.Http.SelfHost.HttpSelfHostServer的用法示例。


在下文中一共展示了HttpSelfHostServer.OpenAsync方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

        static void Main(string[] args)
        {
            // Set up server configuration 
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration("http://localhost:8080");

            //Route Catches the GET PUT DELETE typical REST based interactions (add more if needed)
            config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional },
                new { httpMethod = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Put, HttpMethod.Delete) });

            //This allows POSTs to the RPC Style methods http://api/controller/action
            config.Routes.MapHttpRoute("API RPC Style", "api/{controller}/{action}",
                new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

            //Finally this allows POST to typeical REST post address http://api/controller/
            config.Routes.MapHttpRoute("API Default 2", "api/{controller}/{action}",
                new { action = "Post" },
                new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
            
            using (HttpSelfHostServer server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }
        }
開發者ID:flespinoza,項目名稱:StackOverflow,代碼行數:26,代碼來源:Program.cs

示例2: Start

 public void Start()
 {
     Log.Info("Starting");
     Server = new HttpSelfHostServer(Config);
     Server.OpenAsync().Wait();
     Log.Info("Started");
 }
開發者ID:Zhevranacci,項目名稱:OCTGN,代碼行數:7,代碼來源:ApiManager.cs

示例3: Main

        static void Main(string[] args)
        {
            Assembly.Load("WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            HttpSelfHostConfiguration configuration = new HttpSelfHostConfiguration("http://localhost/selfhost/tyz");//指定基地址
            try
            {
                using (HttpSelfHostServer httpServer = new HttpSelfHostServer(configuration))
                {
                    httpServer.Configuration.Routes.MapHttpRoute(
                     name: "DefaultApi",
                     routeTemplate: "api/{controller}/{id}",
                     defaults: new { id = RouteParameter.Optional });

                    httpServer.OpenAsync().Wait();
                    Console.Read();
                }
            }
            catch (Exception e)
            {
            }

            ////web API的SelfHost寄宿方式通過HttpSelfHostServer來完成
            //using (HttpSelfHostServer httpServer = new HttpSelfHostServer(configuration))
            //{
            //    httpServer.Configuration.Routes.MapHttpRoute(
            //     name: "DefaultApi",
            //     routeTemplate: "api/{controller}/{id}",
            //     defaults: new { id = RouteParameter.Optional });

            //    httpServer.OpenAsync();//開啟後,服務器開始監聽來自網絡的調用請求
            //    Console.Read();
            //}
        }
開發者ID:PaulineTang,項目名稱:GitTest,代碼行數:33,代碼來源:Program.cs

示例4: Given_command_server_runnig

        public void Given_command_server_runnig()
        {
            var config = ZazServer.ConfigureAsSelfHosted(URL);

            _host = new HttpSelfHostServer(config);
            _host.OpenAsync().Wait();
        }
開發者ID:JustApplications,項目名稱:zaz,代碼行數:7,代碼來源:When_connecting_to_server.cs

示例5: Main

        static void Main(string[] args)
        {
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var selfConfig = cfg.GetSection("SeifConfiguration") as SeifConfiguration;

            //var servConfig = new ProviderConfiguration();
            //servConfig.ApiDomain = "api.aaa.com";
            //servConfig.ApiIpAddress = "172.16.1.121";
            //servConfig.SerializeMode = "ssjson";
            //servConfig.Protocol = "Http";
            //servConfig.AddtionalFields.Add(AttrKeys.ApiGetEntrance, "api/get");
            //servConfig.AddtionalFields.Add(AttrKeys.ApiPostEntrance, "api/post");

            //var clientConfig = new ConsumerConfiguration();

            //var registryProvider = new RedisRegistryProvider();
            //var registry = new GenericRegistry(registryProvider, registryProvider);
            //var typeBuilder = new AutofacTypeBuilder();
            ////SeifApplication.Initialize(registry, servConfig, clientConfig, typeBuilder, new ISerializer[]{new ServiceStackJsonSerializer()});

            SeifApplication.Initialize(selfConfig);
            SeifApplication.ExposeService<IDemoService, DemoService>();
            //SeifApplication.ReferenceService<IEchoService>(new ProxyOptions(), new IInvokeFilter[0]);

            SeifApplication.AppEnv.TypeBuilder.Build();

            var config = new HttpSelfHostConfiguration("http://localhost:3333");
            config.Filters.Add(new ExceptionFilter());
            config.Routes.MapHttpRoute("default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
            var server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
            Console.WriteLine("Server is opened");
            Console.Read();
        }
開發者ID:tukzer,項目名稱:Seif,代碼行數:34,代碼來源:Program.cs

示例6: SendJsonGetJson

        public static JObject SendJsonGetJson(HttpMethod method, string routeTemp, string uri, string json)
        {
            JObject result;

            HttpSelfHostConfiguration config
                = new HttpSelfHostConfiguration("http://localhost/");
            config.Routes.MapHttpRoute(
                name: "Default",
                routeTemplate: routeTemp
                );

            using (HttpSelfHostServer server = new HttpSelfHostServer(config))
            using (HttpClient client = getJsonClient())
            {
                server.OpenAsync().Wait();
                using (HttpRequestMessage request =
                    getJsonRequest(method, uri, json))
                using (HttpResponseMessage response = client.SendAsync(request).Result)
                {
                    string responseJson = response.Content.ReadAsStringAsync().Result;
                    result = JObject.Parse(responseJson);
                }
                server.CloseAsync().Wait();
            }
            return result;
        }
開發者ID:pacholik,項目名稱:driving_scools_server,代碼行數:26,代碼來源:AssertHelper.cs

示例7: Start

        public void Start()
        {
            selfHostServer = new HttpSelfHostServer(httpSelfHostConfiguration);
            selfHostServer.OpenAsync();

            Logger.Info("Started management app host");
        }
開發者ID:jonnii,項目名稱:statr,代碼行數:7,代碼來源:ManagementBootstrapper.cs

示例8: Main

        static void Main(string[] args)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8999");
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            // using asp.net like hosting
            // HttpSelfHostServer is using a different constructor compared to the previous project (SelfHost1)
            var server = new HttpSelfHostServer(config);
            var task = server.OpenAsync();
            task.Wait();
            Console.WriteLine("Server is up and running");
            Console.WriteLine("Hit enter to call server with client");
            Console.ReadLine();

            // HttpSelfHostServer, derives from HttpMessageHandler => multiple level of inheritance
            var client = new HttpClient(server);
            client.GetAsync("http://localhost:8999/api/my").ContinueWith(t =>
            {
                var result = t.Result;
                result.Content.ReadAsStringAsync()
                    .ContinueWith(rt =>
                    {
                        Console.WriteLine("client got response" + rt.Result);
                    });
            });

            Console.ReadLine();
        }
開發者ID:randelramirez,項目名稱:IntroductionWebApi,代碼行數:31,代碼來源:Program.cs

示例9: WebApiTest

		public WebApiTest()
		{
			IOExtensions.DeleteDirectory("Test");
			NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(19079);
			Task.Factory.StartNew(() => // initialize in MTA thread
				                      {
					                      config = new HttpSelfHostConfiguration(Url)
						                               {
							                               MaxReceivedMessageSize = Int64.MaxValue,
							                               TransferMode = TransferMode.Streamed
						                               };
					                      var configuration = new InMemoryConfiguration();
					                      configuration.Initialize();
					                      configuration.DataDirectory = "~/Test";
					                      ravenFileSystem = new RavenFileSystem(configuration);
					                      ravenFileSystem.Start(config);
				                      })
			    .Wait();

			server = new HttpSelfHostServer(config);
			server.OpenAsync().Wait();

			WebClient = new WebClient
				            {
					            BaseAddress = Url
				            };
		}
開發者ID:hibernating-rhinos,項目名稱:RavenFS,代碼行數:27,代碼來源:WebApiTest.cs

示例10: Main

 static void Main(string[] args)
 {
     HttpSelfHostServer server = null;
     try {
         // Set up server configuration
         HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(BaseAddress);
         config.Routes.MapHttpRoute(
             name: "DefaultApi",
             routeTemplate: "api/{controller}/{id}",
             defaults: new { id = RouteParameter.Optional }
         );
         // Create server
         server = new HttpSelfHostServer(config);
         // Start listening
         server.OpenAsync().Wait();
         Console.WriteLine("Listening on " + BaseAddress);
         Console.ReadLine();
     }
     catch (Exception e) {
         Console.WriteLine("Could not start server: {0}", e.GetBaseException().Message);
         Console.WriteLine("Hit ENTER to exit...");
         Console.ReadLine();
     }
     finally {
         if (server != null) {
             // Stop listening
             server.CloseAsync().Wait();
         }
     }
 }
開發者ID:djsolid,項目名稱:win8-ws-samples,代碼行數:30,代碼來源:Program.cs

示例11: SetUp

        public void SetUp()
        {
            var config = new HttpSelfHostConfiguration(URL);

            ZazServer.Configure(config, Prefix1, new ServerConfiguration
            {
                Registry = new FooCommandRegistry(),
                Broker = new DelegatingCommandBroker((o, context) =>
                {
                    _api1Command = (FooCommand)o;
                    return Task.Factory.StartNew(() => { });
                }),
            });
            ZazServer.Configure(config, Prefix2, new ServerConfiguration
            {
                Registry = new FooCommandRegistry(),
                Broker = new DelegatingCommandBroker((o, context) =>
                {
                    _api2Command = (FooCommand)o;
                    return Task.Factory.StartNew(() => { });
                }),
            });

            _host = new HttpSelfHostServer(config);
            _host.OpenAsync().Wait();
        }
開發者ID:JustApplications,項目名稱:zaz,代碼行數:26,代碼來源:When_configuring_multiple_endpoints_within_1_webhost.cs

示例12: Main

        private static void Main(string[] args) {
            string prefix = "http://localhost:8080";
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(prefix);
            config.Routes.MapHttpRoute("Default", "{controller}/{action}");

            // Append our custom valueprovider to the list of value providers.
            config.Services.Add(typeof (ValueProviderFactory), new HeaderValueProviderFactory());

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();

            try {
                // HttpClient will make the call, but won't set the headers for you. 
                HttpClient client = new HttpClient();
                var response = client.GetAsync(prefix + "/ValueProviderTest/GetStuff?id=20").Result;

                // Browsers will set the headers. 
                // Loop. You can hit the request via: http://localhost:8080/Test2/GetStuff?id=40
                while (true) {
                    Thread.Sleep(1000);
                    Console.Write(".");
                }
            }
            finally {
                server.CloseAsync().Wait();
            }
        }
開發者ID:tugberkugurlu,項目名稱:ProWebAPI.Samples,代碼行數:27,代碼來源:Program.cs

示例13: Setup

 public void Setup()
 {
     BaseAddress = GetBaseAddress();
     HttpConfiguration = GetHttpConfiguration();
     Server = new HttpSelfHostServer(HttpConfiguration);
     Server.OpenAsync().Wait();
 }
開發者ID:pstephens,項目名稱:fbclient,代碼行數:7,代碼來源:SetupFixture.cs

示例14: Main

        static void Main(string[] args)
        {
            HttpSelfHostServer server = null;
            try
            {
                HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseAddress);
                config.HostNameComparisonMode = HostNameComparisonMode.Exact;

                // Register default route
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}"
                );

                server = new HttpSelfHostServer(config);
                server.OpenAsync().Wait();
                Console.WriteLine("Listening on " + baseAddress);
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not start server: {0}", e.GetBaseException().Message);
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
            finally
            {
                if (server != null)
                {
                    server.CloseAsync().Wait();
                }
            }
        }
開發者ID:ericziko,項目名稱:edge,代碼行數:34,代碼來源:Program.cs

示例15: Main

        static void Main(string[] args)
        {
            // classic
            var config = new HttpSelfHostConfiguration("http://localhost:18081");

            // HTTPS
            //var config = new HttpsSelfHostConfiguration("http://localhost:18081");

            // NTLM
            //var config = new NtlmHttpSelfHostConfiguration("http://localhost:18081");


            // BASIC AUTHENTICATION
            //var config = new BasicAuthenticationSelfHostConfiguration("http://localhost:18081",
            //    (un, pwd) => un == "johndoe" && pwd == "123456");



            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

            using (var server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }
        }
開發者ID:tugberkugurlu,項目名稱:ProWebAPI.Samples,代碼行數:29,代碼來源:Program.cs


注:本文中的System.Web.Http.SelfHost.HttpSelfHostServer.OpenAsync方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。