[英]How to setup a self hosted WCF service
我已经通过WCF概念,阅读了很多内容并创建了SELF HOSTED服务。
我向解决方案“ CommunicationLibrary”添加了库,其中包含:
[ServiceContract]
interface ICommunication
{
[OperationContract]
string message();
}
和
public class Communication: ICommunication
{
public string message()
{
return "WCF Method Accessed";
}
}
然后我添加了控制台项目来托管它“ WCFCommunicationHosting”,其中包含
class WcfHost
{
static void Main(string[] args)
{
Uri baseadd = "sasas";
ServiceHost sh = new ServiceHost(typeof(CommunicationLibrary.Communication), baseadd);
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
sh.Description.Behaviors.Add(smb);
sh.Open();
Console.WriteLine("Host Running");
}
}
现在我不明白在baseAdd
传递什么? 我看到了许多类似CodeProject文章的示例,但我不明白它们为什么以及如何传递localhost://
。
这是一个工作示例。 它在vb.net中,但是您应该了解如何实现/实现什么。
服务地址是您将在使用该服务的客户端的连接/绑定属性中指定的地址。 这是它将找到您的Web服务并与之通信的地址。
Dim LocalIpAddress as string = "your pc ip"
Dim tcp_port as string = "the port you want to use"
Dim myServiceAddress As New Uri("http://" & localIpAddress & ":" & tcp_port & "/" & servicename)
myservicehost = New ServiceHost(GetType(yourwcfservicemethodsclass), myServiceAddress)
Dim BasicBinding As New BasicHttpBinding
BasicBinding.MaxReceivedMessageSize = 2147483647
myservicehost.AddServiceEndpoint(GetType(Iyourwcfservicemethodsclass), BasicBinding, myServiceAddress)
' Enable metadata publishing.
Dim smb As New ServiceMetadataBehavior()
smb.HttpGetEnabled = True
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
myservicehost.Description.Behaviors.Add(smb)
myservicehost.Open()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.