如何设置自托管WCF服务

时间:2014-03-26 15:12:38

标签: c# .net web-services wcf

我已经通过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传递什么?我看到很多例如this CodeProject article但我不明白为什么以及如何传递localhost://

1 个答案:

答案 0 :(得分:0)

这是一个工作示例。它在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()