【发布时间】:2011-05-11 22:02:44
【问题描述】:
我正在尝试启动我的第一个 WCF 服务。
好吧,我想指出的是,我已经完全了解了 WCF 架构和支柱(ABC:Address Binding and Contract = Endpoint)。此外,我已经了解 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中定义了它们...我不明白...
我做错了什么?
谢谢
PS
请注意,即使仅定义 host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc")(没有 tcp 端点)也会得到相同的结果......
还有一件事:我知道这种构建服务的方法不是很好......但是,我想先了解如何从根本上创建和运行服务,而不是使用自动生成代码工具,然后,如何使用更高级别的工具来做到这一点...谢谢
【问题讨论】: