尝试启动第一个 WCF 服务
本文关键字:WCF 服务 第一个 启动 | 更新日期: 2023-09-27 17:56:03
我正在尝试启动我的第一个 WCF 服务。
我想指出的是,我已经完全理解了 WCF 体系结构和支柱(ABC:地址绑定和协定 = 终结点)。此外,我已经了解WCF哲学的许多要素,所以,我不仅仅是一个新手......
然而,撇开理论不谈,当有人把手放在真实的东西上时,真正的问题就会出现......
我有这三个文件:
文件IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
/// <summary>
/// This is the interface that specifies contract for the Sevice1 of this service application.
/// In this file the interface is specified in order to set the service operations that can be invoked by requestors.
/// </summary>
namespace EchoWcfLibrary {
/// <summary>
/// The interface specifies for those classes implementing it (services), the operation that the service will expose.
/// </summary>
[ServiceContract]
public interface IService1 {
// This does not use serialization (implicit serialization in considered: base types used).
[OperationContract]
string GetData(int value);
// This uses data contracts and serialization.
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
/// <summary>
/// The following class defines data contract for those operations managing with non primitive types and, for this reason, needing serialization support (explicit, not implicit)
/// </summary>
[DataContract]
public class CompositeType {
// Members not serialized
bool boolValue = true;
string stringValue = "Hello ";
// Serialized
[DataMember]
public bool BoolValue {
get { return boolValue; }
set { boolValue = value; }
}
// Serialized
[DataMember]
public string StringValue {
get { return stringValue; }
set { stringValue = value; }
}
}
}
文件Service1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
/// <summary>
/// This is the service host implementation. A class implementing the service is specified.
/// </summary>
namespace EchoWcfLibrary {
/// <summary>
/// This class implements the IService1 service.
/// </summary>
public class Service1 : IService1 {
// One operation.
public string GetData(int value) {
return string.Format("You entered: {0}", value);
}
// The other operation.
public CompositeType GetDataUsingDataContract(CompositeType composite) {
if (composite == null) {
throw new ArgumentNullException("composite");
}
if (composite.BoolValue) {
composite.StringValue += "Suffix";
}
return composite;
}
}
}
这些文件放置在名为EchoWcfLibrary
的项目中
主要:Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EchoWcfLibrary;
namespace WcfServiceApplication {
public static class Program {
static void Main(string[] args) {
// Setting endpoints and setting the service to start properly.
// Base address specified: http://localhost:8080/service1
using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8080/service1"))) {
host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc");
host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "net.tcp://localhost:8081/service1/tcpsvc");
host.Open();
System.Threading.Thread.Sleep(1000000);
host.Close();
}
}
}
}
最后一个文件位于一个名为 WcfServiceApplication
的单独项目中这两个项目存在于同一个解决方案中。当然,WcfServiceApplication
有一个链接到另一个项目。
我想启动这个服务,正如你所看到的,它是Visual Studio放在WCF库模板中的服务。
好吧,我第一次尝试运行它,并且在 http 命名空间保留方面遇到了一些问题,我使用 netsh 修复了它并为我的用户和指定的 http 命名空间添加了显式保留。
但是,我遇到的是以下内容:WCF 主机应用程序是一个小型应用程序,非常有用,显示当前托管的服务。只有一个托管的服务:我的,但它的状态是停止的,它在描述框中告诉我没有定义任何终结点!!
但我在Program.cs
中定义了它们...我不明白。。。我做错了什么?
谢谢
附言请注意,即使仅定义host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc")
(没有 tcp 端点)也会给出相同的结果。
还有一件事:我知道这种构建服务的方法不是很好......但是,除了使用自动生成的代码工具之外,我想首先了解如何从根目录创建和运行服务,然后如何使用更高级别的工具做到这一点......谢谢
问题似乎是您定义了两个端点,即"svc"(http)和"net.tcp://localhost:8081/service1/tcpsvc"(tcp),然后尝试使用第三个端点启动服务主机,该端点未在您配置的两个端点中的任何一个中定义。
我建议删除以编程方式创建绑定的代码,将 .config 文件添加到项目中,然后使用 Visual Studio 中内置的 WCF 服务配置编辑器(从 2008 年开始)为您完成繁重的工作。