【发布时间】:2010-05-11 01:34:33
【问题描述】:
我最初几次尝试创建自托管服务。试图编造一些可以接受查询字符串并返回一些文本但有一些问题的东西:
-
所有文档都讨论了如果在配置文件中找不到端点,则会为每个基地址自动创建端点。这对我来说似乎不是这样,我得到“服务有零应用程序端点......”异常。如下手动指定一个基本端点似乎可以解决这个问题:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; namespace TestService { [ServiceContract] public interface IHelloWorldService { [OperationContract] string SayHello(string name); } public class HelloWorldService : IHelloWorldService { public string SayHello(string name) { return string.Format("Hello, {0}", name); } } class Program { static void Main(string[] args) { string baseaddr = "http://localhost:8080/HelloWorldService/"; Uri baseAddress = new Uri(baseaddr); // Create the ServiceHost. using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) { // Enable metadata publishing. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; host.Description.Behaviors.Add(smb); host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), baseaddr); host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), baseaddr + "SayHello"); //for some reason a default endpoint does not get created here host.Open(); Console.WriteLine("The service is ready at {0}", baseAddress); Console.WriteLine("Press <Enter> to stop the service."); Console.ReadLine(); // Close the ServiceHost. host.Close(); } } } } 当这样请求时,我将如何设置它以返回 SayHello(string name) 中的 name 值:localhost:8080/HelloWorldService/SayHello?name=kyle
我想在跑步之前先走路,但这看起来就像在爬行......
【问题讨论】:
-
您使用的是 .NET 3.5 还是 .NET 4 ? “为基地址创建默认端点”功能是 .NET 4 中的新事物 - 在 3.5 中不起作用
标签: wcf console-application endpoints self-hosting