WCF自托管服务 - C#中的终结点

7

我第一次尝试创建一个自托管服务。试图创建一个能够接受查询字符串并返回一些文本的东西,但遇到了一些问题:

  • All the documentation talks about endpoints being created automatically for each base address if they are not found in a config file. This doesn't seem to be the case for me, I get the "Service has zero application endpoints..." exception. Manually specifying a base endpoint as below seems to resolve this:

    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();
                }
             }
         }
    }
    
  • How would I go about setting this up to return the value of name in SayHello(string name) when requested thusly: localhost:8080/HelloWorldService/SayHello?name=kyle

我试图先学会走再去跑,但这感觉就像爬行一样...


你正在使用.NET 3.5还是.NET 4?“为基地址创建默认终结点”功能是.NET 4中的新功能 - 在3.5中无法使用。 - marc_s
3个回答

10

关于您的默认终结点未被添加的问题:

  • 首先,这是WCF 4的功能 - 它只能在.NET 4上工作
  • 其次,如果您在代码中自己添加终结点,那么默认终结点只会在您的服务主机中添加,如果在配置文件中没有显式定义终结点!通过在代码中添加这两个终结点,您将掌握主动权,WCF 4运行时将不会干预您的配置。

请查看此MSDN库文章以获取有关WCF 4开发人员新功能的更多信息。其中介绍了如何使用默认终结点等内容 - 您只需为服务定义基地址并打开ServiceHost即可!

string baseaddr = "http://localhost:8080/HelloWorldService/";
Uri baseAddress = new Uri(baseaddr);

// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
   //for some reason a default endpoint does not get created here
   host.Open();

   // here, you should now have one endpoint for each contract and binding
}

如果您希望这样做,您还可以在代码中显式添加默认端点。因此,如果您需要添加自己的端点,但是又想添加系统默认端点,您可以使用以下代码:

// define and add your own endpoints here

// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
   // add all the system default endpoints to your host
   host.AddDefaultEndpoints();

   //for some reason a default endpoint does not get created here
   host.Open();

   // here, you should now have your own endpoints, plus 
   // one endpoint for each contract and binding
}

我也发现这篇博客文章非常有启发性 - Christopher的博客充满了很好而且非常有帮助的WCF文章 - 强烈推荐。


谢谢Marc,我一定会查看这些书。不幸的是,我目前还不能使用.NET4,但是感谢您提供克里斯博客的链接,看起来我有一些好文章要读了。我正在研究使用WebServiceHost,因为我真正需要做的就是执行通过HTTP GET请求请求的操作并返回一些少量数据。 - Kyle

3

关于书籍——这是我的建议:我总是推荐《学习WCF》(作者Michele Leroux Bustamante)来快速入门WCF。她以非常易懂和亲切的方式涵盖了所有必要的主题。这本书将教您一切——基础知识、中级主题、安全性、事务控制等等,这些都是您需要了解的,以编写高质量、有用的WCF服务。

《学习WCF》 http://ecx.images-amazon.com/images/I/41wYa%2BNiPML._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg

更高级的主题和更深入地了解WCF将由Juval Lowy的Programming WCF Services涵盖。他真正深入探讨了所有技术细节和主题,并呈现出WCF编程的“圣经”。

Programming WCF Services


0
如果IIS托管您的Web服务,则会出现友好的“您已创建Web服务”页面,假设没有其他问题。您可能想尝试一些快速的WCF教程,可以在Bustamente的Learning WCF书中找到,它们进展迅速并且解释了很多内容。
编辑:这里有一个MSDN页面,显示了一种从所请求的服务调用中获取查询字符串参数的方法,是个不错的例子。它展示了使用[WebGet]属性的用法。如果您不想使用它,可以尝试使用OperationContext来访问传入请求的内部。

谢谢,我一直在寻找好的书籍,我会去看看的。 - Kyle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接