简体   繁体   English

wcf双工服务问题

[英]wcf duplex service issue

i am new to WCF, i try to create a duplex service and i get an exception with this messge "HTTP could not register URL http://+:80/Temporary_Listen_Addresses/42be316a-0c86-4678-a61a-fc6a5fd10599/ because TCP port 80 is being used by another application." 我是WCF的新手,我尝试创建双工服务,但此消息出现异常“ HTTP无法注册URL http:// +:80 / Temporary_Listen_Addresses / 42be316a-0c86-4678-a61a-fc6a5fd10599 /,因为TCP端口80正在被另一个应用程序使用。” I will post the whole code in here and i hope you have time to take a look. 我将在此处发布整个代码,希望您有时间看看。 I am using Windows XP. 我正在使用Windows XP。

Service 服务

namespace WcfService
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(RandomService));
            host.Open();
            Console.WriteLine("Service is running, press <ENTER> to stop it");
            Console.ReadLine();
            host.Close();
        }
    }
    public class RandomService : IRandomService
    {
        public void GenerateRandomNumber(int limit)
        {
            Random r = new Random();
            int genInteger = r.Next(limit);
            Thread.Sleep(3000);
            IRandomCallback callback = OperationContext.Current.GetCallbackChannel<IRandomCallback>();
            callback.ShowRandomNumber(genInteger);
        }
    }
    public interface IRandomCallback
    {
        [OperationContract(IsOneWay = true)]
        void ShowRandomNumber(int ranomNumber);
    }
    [ServiceContract(CallbackContract = typeof(IRandomCallback))]
    public interface IRandomService
    {
        [OperationContract(IsOneWay = true)]
        void GenerateRandomNumber(int limit);
    }
}

Config file 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WcfService.RandomService" behaviorConfiguration="randomConfig">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6789/random/"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsDualHttpBinding" contract="WcfService.IRandomService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="randomConfig">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Client 客户

class Program
    {
        static void Main(string[] args)
        {
            InstanceContext context = new InstanceContext(new RandomHandler());
            RandomServiceClient proxy = new RandomServiceClient(context);
            Console.WriteLine("Let's generate a random number");
            try
            {
                proxy.GenerateRandomNumber(100);
            }
            catch (AddressAlreadyInUseException exception)
            {
                Console.WriteLine(exception.Message);
            }
            Console.WriteLine("Press <ENTER> to exit");
            Console.ReadLine();
        }
    }
    public class RandomHandler : IRandomServiceCallback
    {
        public void ShowRandomNumber(int ranomNumber)
        {
            Console.WriteLine("Generated number:{0}", ranomNumber);
            Console.ReadLine();
        }
    }

Config file -> generated using svcutil.exe 配置文件->使用svcutil.exe生成

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IRandomService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:6789/random/" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IRandomService" contract="IRandomService"
                name="WSDualHttpBinding_IRandomService">
                <identity>
                    <userPrincipalName value="BOGUS\Bogdan" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

It's probably being caused because something else is already listening on port 80. Quite often it's IIS running on the same machine. 可能是由于其他端口已经在端口80上监听而引起的。通常是IIS在同一台计算机上运行。 It can also be caused by the WCF Test Client being started if you run the program from Visual Studio and have also created your own service host. 如果您从Visual Studio运行程序,并且还创建了自己的服务主机,则可能是由于启动WCF测试客户端引起的。

Take a look at this link for some hints. 查看链接以获得一些提示。 The comments at the bottom of this article also have some suggestions. 在底部的评论文章也有一些建议。 And one more link here . 还有一个链接在这里

我没有在控制台应用程序中托管该服务,而是创建了wcf服务,并且在客户端应用程序配置中,我添加了用于绑定的clientBaseAddress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM