【发布时间】:2017-12-05 01:25:02
【问题描述】:
我是 WCF 开发新手,我正在尝试创建托管在控制台应用程序中的 WCF 服务。
我已经创建了 WCF 服务并通过在 IIS Express 上运行它来测试它。这样做,WCF 服务将可以从http://localhost:5576/MyFirstService.svc 访问。在服务中,我定义了一个 GET 端点 /test/<param> 只是为了测试它是否有效。使用 Postman http://localhost:5576/MyFirstService.svc/test/123 访问网址后,它会回显 123。
另一方面,我托管 WCF 的控制台应用程序非常简单。我按照教程(http://www.topwcftutorials.net/2014/05/wcf-self-hosting-console-application.html)进行操作。相关代码如下:
Uri httpBaseAddress = new Uri("http://localhost:4321/StudentService");
//Instantiate ServiceHost
studentServiceHost = new ServiceHost(typeof(StudentService.StudentService), httpBaseAddress);
//Add Endpoint to Host
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService), new WSHttpBinding(), "");
//Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
//Open
studentServiceHost.Open();
Console.WriteLine("Service is live now at : {0}", httpBaseAddress);
Console.ReadKey();
当我启动控制台应用程序并访问http://localhost:4321/StudentService 时,我看到了谈论 wsdl 的标准页面。但是,如果我尝试访问 http://localhost:4321/StudentService/test/123,我会收到 400 bad request 错误。
我做对了吗?我应该使用什么路径到达我的端点?我尝试了 URL 的许多变体,但它似乎不起作用。
【问题讨论】:
标签: c# .net web-services wcf