WCF自主机服务的端点在C#
-
25-09-2019 - |
题
我的前几次试图在建立一个自托管服务。试图做的东西了其将接受查询串和返回的一些文字,但有几个问题:
所有文件谈到终点正在建立自动为每个基地址,如果他们找不到在一个配置文件。这似乎不是这种情况对我来说,我得到的"服务有零应用程序的终结点..."例外。手动指定一个基端点如下似乎解决这个:
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)在请求时正是如此:localhost:8080/HelloWorldService/SayHello?name=凯尔
我想走之前在运行,但这似乎只是爬...
解决方案
对于您的问题,关于默认的终结点不正在增加:
- 首先,这是一个WCF4功能-它会的工作。净4只
- 下一个,默认的终结点才加入到你的主机服务如果你有没有明确的终结点中定义的配置,并且如果你 不添加终点自己的代码!通过加入这两个终点在代码,你在采取收费和周转基金的4个运行时将不会拨弄你的配置
看看这个MSDN library条的更多信息 What's new in WCF4对开发商.它表明,除其他事项外,如何使用默认的终点-你基本上定义的基地址为您服务和开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这篇文章在这里 是相当照亮-克里斯托弗博客是充满良好和非常有益的WCF员额的高度推荐。
其他提示
如果IIS承载你的网络服务,那么你得到友好的"你已经创建了一个网服务"的网页,假定什么是错误的。你也许想尝试一些快速WCF教程,可以发现在Bustamente的学习WCF书,他们走的快并解释了很多。
编辑: 这里有一个MSDN页 这显示一种方式得到查询串参数的要求的服务电话,好的例子。它显示出使用[WebGet]属性。如果你不想使用说,你可以尝试使用 OperationContext 获得的进入请求的内部。
不隶属于 StackOverflow