【问题标题】:WCF Self Host Service - Endpoints in C#WCF 自托管服务 - C# 中的端点
【发布时间】: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


【解决方案1】:

关于未添加默认端点的问题:

  • 首先,这是 WCF 4 的一项功能 - 它仅适用于 .NET 4
  • 接下来,只有在配置中没有明确定义端点并且不要在代码中自己添加端点时,才会将默认端点添加到您的服务主机!通过在代码中添加这两个端点,您将负责,WCF 4 运行时不会摆弄您的配置

查看这篇 MSDN 库文章,了解有关 What's new in WCF 4 for developers 的更多信息。除其他外,它显示了如何使用默认端点 - 您基本上为您的服务定义一个基地址并打开 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
}

我也 fonud this blog post here 非常有启发性 - Christopher 的博客充满了很好且非常有用的 WCF 帖子 - 强烈推荐。

【讨论】:

  • 谢谢马克,我一定会去看看那些书。不幸的是,我还不能使用 .NET4,但也感谢 chris 博客的链接,看来我前面有一些很好的阅读材料。我正在考虑改用 WebServiceHost,因为我真正需要做的就是执行通过 HTTP GET 请求请求的操作并返回少量数据。
【解决方案2】:

至于书籍 - 这是我的推荐:我一直推荐的在 WCF 中快速启动和运行的书籍是 Michele Leroux Bustamante 的 Learning WCF。她涵盖了所有必要的主题,并且以一种非常容易理解和平易近人的方式。这将教您编写高质量、有用的 WCF 服务所需的一切 - 基础知识、中级主题、安全性、事务控制等等。

Learning 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 编程的“圣经”。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 谢谢,我一直在找好书,我去看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多