问题描述
我最初几次尝试创建自托管服务.尝试组成一些可以接受查询字符串并返回一些文本但有一些问题的东西:
My first few attempts at creating a self hosted service. Trying to make something up which will accept a query string and return some text but have have a few issues:
-
所有文档都讨论了如果未在配置文件中找到为每个基地址自动创建的端点.对我来说,情况似乎并非如此,我收到服务的应用程序端点为零..."异常.如下所示手动指定基本端点似乎可以解决此问题:
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();
}
}
}
}
我将如何设置它以便在如此请求时返回SayHello(字符串名称)中name的值:localhost:8080/HelloWorldService/SayHello?name = kyle
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
我想在跑步前先走,但这似乎就像在爬行...
I'm trying to walk before running, but this just seems like crawling...
推荐答案
关于未添加默认端点的问题:
For your question about default endpoints not being added:
- 首先,这是WCF 4功能-仅适用于.NET 4
- 接下来,仅当您在config中没有定义显式终结点,并且不要自己在代码中添加终结点时,默认终结点才会添加到您的服务主机中!通过在代码中添加这两个端点,您将可以负担费用,并且WCF 4运行时不会摆弄您的配置
- first of all, that's a WCF 4 feature - it will work on .NET 4 only
- next, the default endpoints are only added to your service host if you have no explicit endpoints defined in config, and if you do not add endpoints yourself in code! By adding those two endpoints in code, you're taking charge and the WCF 4 runtime will not fiddle with your config
有关MSDN库的更多信息,请参见 WCF 4中的新增功能开发人员.它显示了如何使用默认终结点-基本上就是为服务定义一个基地址并打开ServiceHost-仅此而已!
Check out this MSDN library article for more information on What's new in WCF 4 for developers. It shows, among other things, how to use default endpoints - you basically define a base address for your service and open the ServiceHost - that's all!
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
}
如果愿意,还可以通过代码显式添加默认端点.因此,如果您需要添加自己的端点,但是又想添加系统默认端点,则可以使用:
You can also add the default endpoints explicitly, in code, if you wish to do so. So if you need to add your own endpoints, but then you want to add the system default endpoints, you can use:
// 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的好文章,非常有帮助-强烈推荐.
I also fonud this blog post here to be quite illuminating - Christopher's blog is full of good and very helpful WCF posts - highly recommended.
这篇关于WCF自助服务-C#中的端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!