2010-06-25 128 views
0

我是新来的WCF,我尝试创建一个双工服务,我得到一个异常,这个问题“HTTP无法注册URL http://+:80/Temporary_Listen_Addresses/42be316a-0c86-4678-a61a-fc6a5fd10599/,因为TCP端口80正在被另一个应用程序使用。 我会在这里发布整个代码,我希望你有时间去看看。 我正在使用Windows XP。wcf双工服务问题

服务

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); 
    } 
} 

配置文件

<?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> 

客户

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的

产生
<?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> 

回答

0

这可能是由于其他东西已经在端口80上侦听而引起的。通常它是IIS在同一台机器上运行。如果您从Visual Studio运行程序并创建了自己的服务主机,则也可能导致WCF Test Client正在启动。

看看this链接的一些提示。 this文章底部的评论也有一些建议。还有一个链接here

+0

我同时托管服务和客户端在控制台应用程序我没有WCF服务项目。有没有办法得到这个工作因为我找不到任何解决方案? – boo 2010-06-25 20:30:49

0

而是在控制台应用程序托管服务的我创建了一个WCF服务,并在客户端应用程序的配置我增加了一个clientBaseAddress的结合