7

我最初几次尝试创建自托管服务。试图编造一些可以接受查询字符串并返回一些文本但有一些问题的东西:

  • 如果在配置文件中找不到端点,所有文档都讨论了为每个基地址自动创建的端点。这对我来说似乎不是这样,我得到“服务有零应用程序端点......”异常。如下手动指定一个基本端点似乎可以解决这个问题:

    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

我想在跑步之前先走路,但这看起来就像在爬行......

4

3 回答 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 帖子——强烈推荐。

于 2010-05-11T05:26:55.133 回答
3

至于书籍 - 这是我的推荐:我一直推荐的在 WCF 中快速启动和运行的书是Michele Leroux Bustamante 的Learning 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

Juval Lowy的Programming WCF Services将涵盖更高级的主题和对 WCF 的更深入了解。他真正深入研究了所有技术细节和主题,并为 WCF 编程提供了“圣经”。

编程 WCF 服务

于 2010-05-11T05:19:14.200 回答
0

如果 IIS 托管您的 Web 服务,那么您将获得友好的“您已创建 Web 服务”页面,假设没有其他问题。您可能想尝试一些快速的 WCF 教程,可以在 Bustamente 的 Learning WCF 书中找到,它们速度很快并且解释了很多。

编辑: 这是一个 MSDN 页面,它显示了一种从您请求的服务调用中获取查询字符串参数的方法,很好的例子。它显示了 [WebGet] 属性的使用。如果您不想使用它,可以尝试使用OperationContext来获取传入请求的内部信息。

于 2010-05-11T01:46:34.543 回答