我想开始我的第一个WCF服务。尝试启动第一个WCF服务
嗯,我想指出的是,我已经完全理解了WCF架构和支柱(ABC:地址绑定和合同=端点)。此外,我已经明白了WCF理念的许多要素,所以,我并不很单纯newbye ...
然而,理论不谈,真正的问题出现时,有人把他的手放在现实的东西...
我有这三个文件:
文件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主机应用程序是一个非常有用的小应用程序,它显示了当前托管的服务。只有一个托管的服务:我的,但它的状态已停止,它告诉我,在描述框中,NO ENDPOINT已被定义!
但我在Program.cs
中定义了它们...我不明白... 我在做什么错了?
三江源
PS 注意,即使仅定义host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc")
(不带TCP端点)给出了相同的结果....
一两件事:据我了解,这种做法在建设服务不非常好...但是,相反,使用自动生成的代码工具,我想首先了解如何从根创建和运行服务,然后,如何使用更高级别的工具来实现这一点......谢谢
那么我明白你的建议,并感谢你,但是......我真的很讨厌让一个IDE做我的工作,或者一开始就更好,我想知道如何从理由开始服务......但是......第三个端点在哪里? :)谢谢 – Andry 2011-05-12 06:28:19
在你的代码,你问WCF来创建基于此端点的服务主机: 使用(ServiceHost的主机=新的ServiceHost(typeof运算(服务1),新的URI(“HTTP://本地主机:8080/service1“))) 在服务主机的配置中,您已经定义了一个http端点,如下所示(没有基址):) new BasicHttpBinding(),”svc“); 希望是有道理的。 – DaveRead 2011-05-12 07:41:18