首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wcf双工业务问题

wcf双工业务问题
EN

Stack Overflow用户
提问于 2010-06-25 19:41:04
回答 2查看 1.5K关注 0票数 0

我是WCF的新手,我尝试创建一个双工服务,并得到了一个例外:"HTTP无法注册URL Addresses/42be316a-0c86-4678-a61a-fc6a5fd10599/,因为TCP端口80正在被另一个应用程序使用。“我会把所有的代码张贴在这里,我希望你有时间看看。我正在使用Windows。

服务

代码语言:javascript
复制
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);
    }
}

配置文件

代码语言:javascript
复制
<?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>

客户端

代码语言:javascript
复制
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();
        }
    }

使用->生成的配置文件svcutil.exe

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2010-06-25 19:45:23

这可能是因为其他东西已经在监听端口80而造成的。通常是IIS运行在同一台计算机上。如果您从Visual运行该程序,并且还创建了您自己的服务主机,那么它也可能是由正在启动的WCF测试客户机引起的。

请查看链接以获得一些提示。文章底部的评论也有一些建议。还有一个链接这里

票数 0
EN

Stack Overflow用户

发布于 2010-06-25 21:14:14

我没有将服务托管在控制台应用程序中,而是创建了一个wcf服务,并在客户端应用程序配置中为绑定添加了一个clientBaseAddress。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3120924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档